Codeforces 383C Propagating tree(树状数组)
来源:互联网 发布:隐藏电脑录像软件 编辑:程序博客网 时间:2024/05/16 12:28
题目大意:
有一棵树,对这个树有两种操作:1:表示为(1 x val),在编号为x的节点上加上val,然后给x节点的每个儿子加上- val,再给每个儿子的儿子加上-(- val),一直加到没有儿子为止。2:表示为(2 x)查询x节点上的值。
思路:
首先通过dfs将树抽象成树状数组,然后以和根节点的层数关系作为d,开两个树状数组分别计算添加值和减少值。
#include<bits/stdc++.h>using namespace std;#define N 200005int bit[2][N], cnt, n;struct Node{ int l, r, val, d;}node[N];vector<int> g[N];void dfs(int u, int fa, int d){ node[u].l=++cnt;node[u].d=d; for(int i=0; i<g[u].size(); i++) { int v=g[u][i]; if(v==fa) continue; dfs(v, u, 1-d); } node[u].r=cnt;}void modify(int x, int val, int *sum){ while(x<=n) { sum[x]+=val; x+=(x&(-x)); }}int getsum(int x, int *sum){ int ret=0; while(x>0) { ret+=sum[x]; x-=(x&(-x)); } return ret;}int main(){ int m, u, v, op; while(scanf("%d%d", &n, &m)!=EOF) { cnt=0; memset(bit, 0, sizeof(bit)); for(int i=1; i<=n; i++) { scanf("%d", &node[i].val); g[i].clear(); } for(int i=1; i<n; i++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, -1, 0); for(int i=1; i<=m; i++) { scanf("%d", &op); if(op==1) { scanf("%d%d", &u, &v); modify(node[u].l, v, bit[node[u].d]); modify(node[u].r+1, -v, bit[node[u].d]); modify(node[u].l, -v, bit[1-node[u].d]); modify(node[u].r+1, v, bit[1-node[u].d]); } else { scanf("%d", &u); printf("%d\n", node[u].val+getsum(node[u].l, bit[node[u].d])); } } } return 0;}
阅读全文
0 0
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C . Propagating tree【树状数组,dfs】
- CodeForces 383C Propagating tree 树上哈希+树状数组
- Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)
- CF 383C Propagating tree [想法+树状数组]
- CodeForces 383C Propagating tree
- codeforces 383C Propagating tree
- CODEFORCES, 383C,Propagating tree
- CodeForces - 383C Propagating tree(dfs + 线段树)
- 【CodeForces】383C Propagating tree 线段树
- Codeforces 383C Propagating tree 题解&代码
- Codeforces 383C Propagating tree DFS序+BIT
- CodeForces 384E Propagating tree 树状数组dfs序组合使用
- Codeforce 383C. Propagating tree
- Codeforces 396C On Changing Tree(树状数组)
- 阿里云首推免费人脸识别SDK 让每个APP轻松拥有短视频AR特效
- angular.js中window.onload(),$(document).ready()的写法
- hdu2039
- JAVA导入导出Excel【POI技术】
- Linux spi驱动框架之执行流程
- Codeforces 383C Propagating tree(树状数组)
- android studio打开从别地拷过来的项目报错
- 关于log4j超赞的文章
- java实现定时任务的三种方法
- 奇虎360scribe日志采集系统
- iOS判断对象是否为空
- GTC大会上“省钱”不亦乐乎,阿里云GPU人脸识别实在好玩!
- 光荣的梦想 (树状数组求逆序数和)
- (6)学习SpringBoot之 热部署