Change FZU - 2277 毒瘤啊 毒瘤题目
There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.
Initially all the node’s value is 0.
We have q operations. There are two kinds of operations.
1 v x k : a[v]+=x , a[v’]+=x-k (v’ is child of v) , a[v’’]+=x-2*k (v’’ is child of v’) and so on.
2 v : Output a[v] mod 1000000007(10^9 + 7).
Input
First line contains an integer T (1 ≤ T ≤ 3), represents there are T test cases.
In each test case:
The first line contains a number n.
The second line contains n-1 number, p2,p3,…,pn . pi is the father of i.
The third line contains a number q.
Next q lines, each line contains an operation. (“1 v x k” or “2 v”)
1 ≤ n ≤ 3*10^5
1 ≤ pi < i
1 ≤ q ≤ 3*10^5
1 ≤ v ≤ n; 0 ≤ x < 10^9 + 7; 0 ≤ k < 10^9 + 7
Output
For each operation 2, outputs the answer.
Sample Input
1
3
1 1
3
1 1 2 1
2 1
2 2
Sample Output
2
1
题意
节点u加x,节点u的儿子u′加x−k,节点u的孙子u′′加x−2×k,依次类推。 看数据范围标准的线段树
但是这个k不好处理 这个k和深度有关系
所以这题在初始化处理的时候 1LL * x + 1LL * dep[v]*k
这样就便于我们查询了
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define rtl rt<<1
#define rtr rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define FIN freopen("DATA.txt","r",stdin)
#define read(x) scanf("%d", &x)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
int t, n, q, tot, dfscnt, head[maxn], L[maxn], R[maxn], dep[maxn];
struct Edge {
int v, nxt;
} edge[maxn << ];
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa, int d) {
L[u] = ++dfscnt;
dep[u] = d;
for (int i = head[u] ; ~i ; i = edge[i].nxt) {
int v = edge[i].v;
if (v != fa) dfs(v, u, d + );
}
R[u] = dfscnt;
}
struct node {
int l, r;
LL num, k;
int mid() {
return (l + r) >> ;
}
} tree[maxn << ];
void build(int l, int r, int rt) {
tree[rt].l = l, tree[rt].r = r;
tree[rt].num = tree[rt].k = ;
if (l == r) return ;
int m = (l + r) >> ;
build(l, m, rtl);
build(m + , r, rtr);
}
void pushdown(int rt) {
if (tree[rt].num != ) {
tree[rtl].num = (tree[rt].num + tree[rtl].num) % mod;
tree[rtr].num = (tree[rt].num + tree[rtr].num) % mod;
tree[rt].num = ;
}
if (tree[rt].k != ) {
tree[rtl].k = (tree[rt].k + tree[rtl].k) % mod;
tree[rtr].k = (tree[rt].k + tree[rtr].k) % mod;
tree[rt].k = ;
}
} void update(LL a, LL b, int L, int R, int rt) {
if (L <= tree[rt].l && tree[rt].r <= R) {
tree[rt].num = ((tree[rt].num + a) % mod + mod) % mod;
tree[rt].k = ((tree[rt].k + b) % mod + mod) % mod;
return ;
}
pushdown(rt);
int mid = tree[rt].mid();
if (R <= mid) update(a, b, L, R, rtl);
else if(L > mid) update(a, b, L, R, rtr);
else {
update(a, b, L, mid, rtl);
update(a, b, mid + , R, rtr);
}
}
LL query(int pos, int d, int rt) {
if (tree[rt].l == tree[rt].r) {
return ((tree[rt].num - d * tree[rt].k % mod) % mod + mod) % mod;
}
pushdown(rt);
int mid = tree[rt].mid();
if (pos <= mid) return query(pos, d, rtl);
return query(pos, d, rtr);
}
int main() {
int op, v, x, k;
sf(t);
while(t--) {
sf(n);
for (int i = ; i <= n ; i++) head[i] = -;
tot = ;
for (int i = ; i <= n ; i++) {
sf(v);
add(v, i);
add(i, v);
}
dfscnt = ;
dfs(, -, );
build(, n, );
sf(q);
while(q--) {
sf(op);
if (op == ) {
sfff(v, x, k);
update(1LL * x + 1LL * dep[v]*k, k, L[v], R[v], );
} else {
sf(v);
printf("%lld\n", query(L[v], dep[v], ));
}
}
}
return ;
}
Change FZU - 2277 毒瘤啊 毒瘤题目的更多相关文章
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- FZU 2277 Change(dfs序+树状数组)
Problem Description There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each no ...
- 【FZU 2277】Change
题意 有一颗有n个节点的有根树,根节点编号时1,每个结点都有一个值ai,开始的时候,所有节点的值都是0. 我们有q个操作,操作只有两种类型 1 v x k,a[v]+=x,a[v']+=x-k,a[v ...
- 【HNOI 2018】毒瘤
Problem Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(例如给一个区间内的数同时加上 \(c ...
- [bzoj5287] [HNOI2018]毒瘤
题目描述 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和.毒瘤考虑了n ...
- UOJ67 新年的毒瘤【Tarjan,割点】
Online Judge:#uoj 67 Label:Tarjan,割点,细节 题目描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树.这个长着毒瘤的树可以用\(n\)个结点 ...
- [HNOI2018]毒瘤
Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和 ...
- @loj - 2496@ 「AHOI / HNOI2018」毒瘤
目录 @description@ @solution@ @accepted code@ @details@ @description@ 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的 ...
- fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...
随机推荐
- Vuejs 实现简易 todoList 功能 与 组件
todoList 结合之前 Vuejs 基础与语法 使用 v-model 双向绑定 input 输入内容与数据 data 使用 @click 和 methods 关联事件 使用 v-for 进行数据循 ...
- sql server存储特殊字符解决办法
好久没来院子了,最近在学java了,再加上项目比较紧,最近都没怎么上,其实这几天在项目中学到不少东西,都能写下来,但是久而久之就忘了,还是得养成及时总结的好习惯啊,还有有时间一定要把那个小项目整理下来 ...
- LeetCode 142——环形链表 II
1. 题目 2. 解答 2.1 方法 1 定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针一定会相遇. 当快慢指针相遇时,我们让慢指针指向头节点,快指针不变,然后每次快 ...
- ElasticSearch 2.0以后的改动导致旧的资料和书籍需要订正的部分
id原先是可以通过path指定字段的 "thread": { "_id" : { "path" : "thread_id" ...
- Python中的相对导入语法
Python中支持相对导入语法,即可以相对于某一个package进行导入,具体语法如下: # 导入"./dir2/spam.py", .表示当前目录 from .dir2 impo ...
- C中的除法,商和余数的大小、符号如何确定
对于C中的除法,商和余数的大小.符号是如何确定的呢?在C89中,只规定了如果两个数为正整数,那么余数的符号为正,并且商的值是接近真实值的最大整数.比如5 / 2,那么商就是2,余数就是1.但是,C89 ...
- Mybatis实现
简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 ...
- (三)java字符串
不可变字符串 Java没有字符串类型,而是提供了一个预定义类String. java中的字符串是不可变字符串,因此无法更改某一个字符串变量的内容. 优点:编译器可以让字符串共享.当复制一个字符串时,原 ...
- Qt应用程序图标
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt应用程序图标 本文地址:http://techieliang.com/2017/1 ...
- idea dubbo jar error:cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明
声明: 出现这个错误的情形是,在idea开发环境里面运行是没有问题的,使用哦idea自带的打包工具生成jar之后,运行jar的时候报的这个错误,如果不是这个情况,这篇文章可能不适用. 主要的原因是sp ...