Problem Description

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
题意
给你1个以1为根节点的树,每个节点初始值为0,有下面两个操作

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).

题解
v'为v的子节点
a[v']+=x-(deep[v']-deep[v])*k
a[v']+=x+deep[v]*k-deep[v']*k
对于x+deep[v]*k可以直接更新[s[v],e[v]]
对于-deep[v']*k可以维护一个sumk[v']+=k,最后查询的时候*deep[v']
代码
 #include<cstdio>
#include<vector>
using namespace std; const int maxn=3e5+;
const int mod=1e9+; int s[maxn],e[maxn],deep[maxn],sum[maxn],sumk[maxn],tot,n;
vector<int>G[maxn];
void dfs(int u)
{
s[u]=++tot;
for(vector<int>::iterator v=G[u].begin();v!=G[u].end();v++)
deep[*v]=deep[u]+,
dfs(*v);
e[u]=tot;
}
void update(int x,int add)
{
for(int i=x;i<=n;i+=(i&-i))sum[i]=(sum[i]+add)%mod;
}
void update1(int x,int add)
{
for(int i=x;i<=n;i+=(i&-i))sumk[i]=(sumk[i]+add)%mod;
}
int query(int x,int y)
{
int ret=,ans=;
for(int i=x;i;i-=(i&-i))ret=(ret*1LL+sum[i])%mod;
for(int i=x;i;i-=(i&-i))ans=(ans*1LL+sumk[i])%mod;
ans=ans*1LL*deep[y]%mod;
return (ret+ans)%mod;
}
void init()
{
tot=;
for(int i=;i<=n;i++)
{
sum[i]=sumk[i]=;
G[i].clear();
}
}
int main()
{
int _,u,v,x,op,Q,k;
scanf("%d",&_);
while(_--)
{
init();
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&u);
G[u].push_back(i);
}
dfs();
scanf("%d",&Q);
for(int i=;i<Q;i++)
{
scanf("%d",&op);
if(op==)
{
scanf("%d%d%d",&v,&x,&k);
int ret=(x+deep[v]*1LL*k)%mod;
update(s[v],ret);
update(e[v]+,-ret+mod);
update1(s[v],-k+mod);
update1(e[v]+,k);
}
else
{
scanf("%d",&v);
printf("%d\n",query(s[v],v));
}
}
}
return ;
}

FZU 2277 Change(dfs序+树状数组)的更多相关文章

  1. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  2. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  3. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  5. 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  6. [BZOJ1103][POI2007]大都市meg dfs序+树状数组

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

  7. 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)

    传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...

  8. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  9. 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)

    题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...

随机推荐

  1. DOS 格式化日期时间输出

    if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)if &quo ...

  2. 页面中onclick事件引号问题

    第一种:html中onclick调用事件 <p id="txt" onclick="changeSize()">加括弧的changeSize()&l ...

  3. 微服务-springcloud

    感觉微服务都差不多概念,最近稍微看了下springcloud,感觉入门还是很简单的,框架用用就那么回事,深入的话需要很多时间投入了 学一个东西,我推荐首先从概念上了解到他是做什么的,什么时候需要,基本 ...

  4. 1047B_Cover Points

    B. Cover Points time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. 使用DDOS deflate抵御少量DDOS攻击

    DDoS-Deflate是一款非常小巧的防御和减轻DDoS攻击的工具,它可以通过监测netstat来跟踪来创建大量互联网连接的IP地址信息,通过APF或IPTABLES禁止或阻档这些非常IP地址. 工 ...

  6. 【剑指offer】从尾到头翻转打印单链表

    #include <iostream> #include <vector> #include <stack> using namespace std; struct ...

  7. [PHP]PHP定时任务的实现

    ---------------------------------------------------------------------------------------------------- ...

  8. .net上的 jpa

    还没试过,有空试试: NPersistence ORSQL

  9. thymeleaf 的内置对象

       

  10. java-学习8

    方法的声明及使用 public class function { public static void main(String[] args) { printInfo();//调用printInfo( ...