CodeForces 383C
来源:互联网 发布:linux查看swap大小 编辑:程序博客网 时间:2024/06/05 11:34
题目大意:
有一颗树,两个操作:
1.x,val 将点x加上val,x的子节点,如果与x的距离为偶数加上val,否则减去val。
2.x 求点x的数值。
解决方法:
建立两个树状数组,分别代表每次修改对于奇数层与偶数层的影响,然后求解是奇数层在奇数树状数组中求,偶数层在偶数树状数组中求。
我的代码:
#include <cstdio>#include <iostream>#include <cstring>#include <vector>#include <algorithm>#define maxn 500000using namespace std;int tree1[maxn],tree2[maxn];vector <int> po[maxn];int l[maxn],r[maxn];int d[maxn];int num[maxn];int n,m,totl;int dfs(int v,int fa,int dd){ d[v]=dd; l[v]=totl++; for (int i=0;i<po[v].size();i++) if (po[v][i]!=fa){ dfs(po[v][i],v,1-dd); } r[v]=totl-1; return 0;}int add1(int x,int v){ //cout<<x<<" "<<v<<" *"<<endl; for (int i=x;i<=n;i+=i&(-i)) tree1[i]+=v; return 0;}int add2(int x,int v){ //cout<<x<<" "<<v<<" *"<<endl; for (int i=x;i<=n;i+=i&(-i)) tree2[i]+=v; return 0;}int sum1(int x){ int ans=0; for (int i=x;i>0;i-=i&(-i)) ans+=tree1[i]; return ans;}int sum2(int x){ int ans=0; for (int i=x;i>0;i-=i&(-i)) ans+=tree2[i]; return ans;}int main (){ //freopen("test.in","r",stdin); while (~scanf("%d%d",&n,&m)){ memset(tree1,0,sizeof(tree1)); memset(tree2,0,sizeof(tree2)); for (int i=1;i<=n;i++) scanf("%d",&num[i]),po[i].clear(); for (int i=1;i<=n-1;i++){ int a,b;scanf("%d%d",&a,&b); po[a].push_back(b); po[b].push_back(a); } totl=1;dfs(1,0,0); for (int i=1;i<=m;i++){ int po,v;scanf("%d%d",&po,&v); if (po==1){ int val;scanf("%d",&val); // cout<<"val v "<<val<<" "<<v<<" "<<l[v]<<" "<<r[v]<<endl; if (d[v]){ add1(l[v],val); add1(r[v]+1,-1*val); add2(l[v],-1*val); add2(r[v]+1,val); } else { add1(l[v],-1*val); add1(r[v]+1,val); add2(l[v],val); add2(r[v]+1,-1*val); } } else { if (d[v]) cout<<sum1(l[v])+num[v]<<endl; else cout<<sum2(l[v])+num[v]<<endl; } } } return 0;}
0 0
- CodeForces 383C
- CodeForces 383C Propagating tree
- codeforces 383C Propagating tree
- CODEFORCES, 383C,Propagating tree
- Codeforces 383C Propagating tree(树状数组)
- 【CodeForces】383C Propagating tree 线段树
- Codeforces 383C Propagating tree 题解&代码
- codeforces Round #383 (Div.2) C
- Codeforces 383C Propagating tree(树状数组)
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- ArcSDE for Oracle在大数据量执行创建统计信息(Analyze)耗时长的问题
- byte类型取值范围以及溢出详解
- LeetCode | Copy List with Random Pointer(赋值带有随机指针的链表)
- Java Set集合之Treeset
- OGEngine:Java开发者的手游开发引擎
- CodeForces 383C
- 自比毕加索,揭秘神秘的“苹果大学”
- java.util.vector中的vector的详细用法
- 斐波拉切字符串统计个数 Fibonacci String
- Use Archive Utility preferences for control over archives
- jqGrid中rowNum关于返回值的设定
- jQuery 取值、赋值的基本方法整理
- Avoiding Memory Leaks
- soap,wsdl,uddi