FZU 2277 Change(dfs序+树状数组)
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
Sample Output
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).
#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序+树状数组)的更多相关文章
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- 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 ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- [BZOJ1103][POI2007]大都市meg dfs序+树状数组
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)
题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...
随机推荐
- UI5-学习篇-12-Eclipse同步ABAP服务器UI5应用
如何在ABAP服务器上部署和运行SAPUI5应用程序 https://blogs.sap.com/2013/06/15/how-to-deploy-and-run-sapui5-application ...
- 跨域(四)——document.domain
浏览器有一个合法的性质:一个页面可以设置document.domain为当前子域或比当前子域更高级的域.一般顶级就到了根域,如果设置为其他域,浏览器就会报权限错误. 利用这个性质,我们可以通过设置do ...
- linux thread_info 与thread_struct
有个同事看3.10代码中,看着两个结构,会混淆,所以我简单答复了一下. thread_info是和内核栈放一块的,网上到处都是thread_info的资料,但thread_struct的资料比较少,在 ...
- Winform自定义控件实例
本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...
- JAVA字符串格式化-String.format()的使用【转】
原文地址:https://blog.csdn.net/lonely_fireworks/article/details/7962171 常规类型的格式化 String类的format()方法用于创建格 ...
- Glide4 高效加载图片的配置【转】
原文: Glide4 高效加载图片的配置 https://www.jianshu.com/p/a079713f280f
- css3的2D和3D的转换
一:2D转换: 通过 CSS3 transform转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. 2D移动:translate().使用translate()函数,你可以把元素从原来的位置移 ...
- tomcat生成调试日志配置
创建文件logging.properties 文件存放于应用WEB-INF/classes下 文件内容如下: org.apache.juli.FileHandler.prefix = error-d ...
- 认识 Thymeleaf
- Ubuntu中更改所有子文件和子目录所有者权限
转自:http://www.linuxidc.com/Linux/2015-03/114695.htm Ubuntu中有两个修改命令可以用到,「change mode」&「change own ...