CodeForces 384E Propagating tree 树状数组dfs序组合使用
来源:互联网 发布:io1.1鼠标淘宝哪家最好 编辑:程序博客网 时间:2024/06/05 19:14
Propagating tree
CodeForces - 384E1. 输入 1 x val 表示把第 x 个点权值加上一个 val,对于每个点,它权值改变了,val,则它的儿子权值改变-val
一直更新权值直到叶子节点
2.输入2 x ,表示查询 第 x 个点的权值
对于操作一,会发现,它是分层进行操作的,把层分为奇偶层,如果当前操作为奇数层+val,那么它下面的所有奇数层都是+val,偶数层都是-val。
先说说怎么把一棵树转化为线性来使用树状数组。
由于对一个点进行操作的时候只需要对其子树也进行操作,所以这个线性关系就需要从父节点进入到儿子节点的,也就是一个先序遍历的关系,所以用一个dfs进去搜,然后给编号,对于每个点有两个编号,左编号就是自己的下标,右编号指的是自己最后的叶子节点的下标,所有 左编号到右编号就表示是从自己到自己的所有儿子都遍历到了,所以对这个点操作的时候就可以用那个编号在树状数组里进行维护了。
所以就进行一次 DFS,同时对层数奇偶进行赋值
void dfs(int x,int pre,int dep){tree[x].le = cnt++;tree[x].dep = dep;for(int i = 0;i < edge[x].size();i++){if(edge[x][i] == pre)continue;dfs(edge[x][i],x,1 - dep); }tree[x].ri = cnt++;}然后再说怎样让它实现奇 偶层不同的赋值,答案是并不这么做,那太难实现了,我们不如分成两个树状数组,全部赋值为0,一个代表奇数层,一个代表偶数层。在进行1操作的时候,先判断它是奇数层还是偶数层,然后进去对应的层的树状数组里,在里面就不分奇偶地直接 + val 。我们对奇偶的区别在询问的时候再去做手脚。
我们已经做完了1操作,现在要进行查询操作的时候,假如它是奇数层的,那么它的价值应该要加上 奇数树状数组里的值,但是它得减去偶数树状数组里的值,因为偶数层操作的时候,它作为奇数层应该是要减的,所以这时候这样操作就可以了,也就是说我们先不把每个点的值放进树状数组,而是用数组数组记录该点的值的改变,最后来计算就可以了。
不可思议
#include<iostream>#include<cstdio>#include<vector>#include<algorithm>#include<cstring>using namespace std;#define maxn 200005#define lowbit(x) (x & (-x))#define mem(a,x) memset(a,x,sizeof(a))struct node{int le,ri,val,dep; // le表示左标签,ri为右标签,dep记录层数奇偶}tree[maxn];int n,m,cnt,bit[2][maxn * 2];vector<int>edge[maxn];void Update(int x,int val,int *t){while(x <= 2 * n){t[x] += val;x += lowbit(x);}}int getSum(int x,int *t){int ans = 0;while(x > 0){ans += t[x];x -= lowbit(x);}return ans;}void dfs(int x,int pre,int dep){tree[x].le = cnt++;tree[x].dep = dep;for(int i = 0;i < edge[x].size();i++){if(edge[x][i] == pre)continue;dfs(edge[x][i],x,1 - dep); }tree[x].ri = cnt++;}void init(){cnt = 1;mem(bit,0);for(int i = 0;i < maxn;i++)edge[i].clear();}int main(){init();scanf("%d %d",&n,&m);for(int i = 1;i <= n;i++){scanf("%d",&tree[i].val);}int a,b,x;for(int i = 1;i < n;i++){scanf("%d %d",&a,&b);edge[a].push_back(b);edge[b].push_back(a);}dfs(1,-1,0);for(int i = 0;i < m;i++){scanf("%d",&x);if(x == 1){scanf("%d %d",&a,&b);Update(tree[a].le,b,bit[tree[a].dep]); //上传的时候是从 该点一直刷到最后的,所以要反刷个 -b 把后面多刷的补回来 Update(tree[a].ri + 1,-b,bit[tree[a].dep]);}else{scanf("%d",&a);printf("%d\n",tree[a].val + getSum(tree[a].le,bit[tree[a].dep]) - getSum(tree[a].le,bit[1 - tree[a].dep]));} //由于做标签就是它的下标,所以可以直接拿tree[a].le 表示 }return 0;}
阅读全文
1 0
- CodeForces 384E Propagating tree 树状数组dfs序组合使用
- CodeForces 384E Propagating tree (dfs序)
- Codeforces 383C . Propagating tree【树状数组,dfs】
- Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- CodeForces 383C Propagating tree 树上哈希+树状数组
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree DFS序+BIT
- Codeforces 570D TREE REQUESTS dfs序+树状数组
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
- 【codeforces 384E】Propagating tree 中文题意&题解&代码(c++)
- Codeforces Round #225 (Div. 2) E. Propagating tree
- Codeforces Round#225 div.2E Propagating tree 线段树
- codeforces#225-C - Propagating tree-dfs序(奇偶)+线段树
- CodeForces - 383C Propagating tree(dfs + 线段树)
- CF 383C Propagating tree [想法+树状数组]
- 机器学习笔记-第四章 神经网络
- C++在构造函数中防止资源泄露(9)---《More Effective C++》
- 单例模式和静态类
- 排序算法--直接插入排序、折半插入排序、希尔排序
- localStorage和sessionstorage
- CodeForces 384E Propagating tree 树状数组dfs序组合使用
- HDU
- 深度学习视觉领域常用的数据集汇总
- js 有关语法
- 动态规划求最长公共序列
- spring 连接oracle数据库的代码
- PAT 1066. Root of AVL Tree (25) 回レ!雪月AVL
- iOS 动态隐藏状态栏
- 洛谷 P3390 【模板】矩阵快速幂