Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)
来源:互联网 发布:海文网络班班代 编辑:程序博客网 时间:2024/06/05 18:35
题目链接:http://codeforces.com/contest/383/problem/C
Description
This tree has a special property: when a value val is added to a value of node i, the value -val is added to values of all the children of node i. Note that when you add value -val to a child of node i, you also add -(-val) to all children of the child of node i and so on. Look an example explanation to understand better how it works.
This tree supports two types of queries:
"1 x val" — val is added to the value of node x;
"2 x" — print the current value of node x.
In order to help Iahub understand the tree better, you must answer m queries of the preceding type.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 200000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000). Each of the next n–1 lines contains two integers vi and ui (1 ≤ vi, ui ≤ n), meaning that there is an edge between nodes vi and ui.
Each of the next m lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1 ≤ x ≤ n, 1 ≤ val ≤ 1000.
Output
For each query of type two (print the value of node x) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input.
Sample Input
5 5
1 2 1 1 2
1 2
1 3
2 4
2 5
1 2 3
1 1 2
2 1
2 2
2 4
Sample Output
3
3
0
题意:给出一颗有n个节点,且1为根节点的树,每个节点有它的权值,现在进行m次操作,操作分为添加和查询,当一个节点的权值添加v,则它的孩子节点的权值要添加-v。
题解:DFS标号后,按照深度分出奇偶层,当奇数(偶数)层+v时,对应偶数(奇数)层-v,两个线段树跑一跑就好。
线段树:
//#include <bits/stdc++.h>//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define bug cout<<"bug"<<endlconst int MAXN = 2e5+7;int n,m,index;int val[MAXN],lazy[2][MAXN<<2];vector<int> node[MAXN];int deep,L[MAXN],R[MAXN],flag[MAXN],id[MAXN];void dfs(int u, int fa, int poi){ L[u]=++deep; id[deep]=u; flag[u]=poi; int siz=node[u].size(); for(int i=0; i<siz; ++i) { int v=node[u][i]; if(v==fa)continue; dfs(v,u,poi^1); } R[u]=deep;}/*void build(int l, int r, int poi){ if(l==r){lazy[0][poi]=lazy[1][poi]=val[id[l]];return;} int mid=(l+r)>>1; build(l,mid,poi<<1); build(mid+1,r,poi<<1^1);}*/void push_data(int poi){ if(lazy[0][poi]) { lazy[0][poi<<1]+=lazy[0][poi]; lazy[0][poi<<1^1]+=lazy[0][poi]; } if(lazy[1][poi]) { lazy[1][poi<<1]+=lazy[1][poi]; lazy[1][poi<<1^1]+=lazy[1][poi]; } lazy[0][poi]=lazy[1][poi]=0;}void update(int l, int r, int a, int b, int poi, int f, int add){ if(a<=l && r<=b){lazy[f][poi]+=add;lazy[f^1][poi]-=add;return;} push_data(poi); int mid=(l+r)>>1; if(mid>=a)update(l,mid,a,b,poi<<1,f,add); if(mid<b)update(mid+1,r,a,b,poi<<1^1,f,add);}int query(int l, int r, int poi, int f, int p){ if(l==r)return lazy[f][poi]; push_data(poi); int mid=(l+r)>>1; if(mid>=p)return query(l,mid,poi<<1,f,p); return query(mid+1,r,poi<<1^1,f,p);}int main(){ scanf("%d%d",&n,&m); index=0; for(int i=1; i<=n; ++i) scanf("%d",&val[i]); int a,b,c; for(int i=0; i<n-1; ++i) { scanf("%d%d",&a,&b); node[a].push_back(b); node[b].push_back(a); } deep=0; dfs(1,0,0);// build(1,n,1); for(int i=0; i<m; ++i) { scanf("%d%d",&a,&b); if(a==1) { scanf("%d",&c); update(1,n,L[b],R[b],1,flag[b],c); } else printf("%d\n",val[b]+query(1,n,1,flag[b],L[b])); } return 0;}/*5 51 2 1 1 21 21 32 42 51 2 31 1 22 12 22 4*/
树状数组:
//#include <bits/stdc++.h>//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define bug cout<<"bug"<<endl#define lowbit(a) a&(-a)const int MAXN = 2e5+7;int n,m,index;int val[MAXN],lazy[2][MAXN<<2];vector<int> node[MAXN];int deep,L[MAXN],R[MAXN],flag[MAXN],id[MAXN];void dfs(int u, int fa, int poi){ L[u]=++deep; id[deep]=u; flag[u]=poi; int siz=node[u].size(); for(int i=0; i<siz; ++i) { int v=node[u][i]; if(v==fa)continue; dfs(v,u,poi^1); } R[u]=deep;}void add_data(int p, int f, int data){ int i=p; while(i<=n) { lazy[f][i]+=data; lazy[f^1][i]-=data; i+=lowbit(i); }}int query(int p, int f){ int i=p,ans=0; while(i>0) { ans+=lazy[f][i]; i-=lowbit(i); } return ans;}int main(){ scanf("%d%d",&n,&m); index=0; for(int i=1; i<=n; ++i) scanf("%d",&val[i]); int a,b,c; for(int i=0; i<n-1; ++i) { scanf("%d%d",&a,&b); node[a].push_back(b); node[b].push_back(a); } deep=0; dfs(1,0,0); for(int i=0; i<m; ++i) { scanf("%d%d",&a,&b); if(a==1) { scanf("%d",&c); add_data(L[b],flag[b],c); add_data(R[b]+1,flag[b],-c); } else printf("%d\n",val[b]+query(L[b],flag[b])); } return 0;}/*5 51 2 1 1 21 21 32 42 51 2 31 1 22 12 22 4*/
- Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)
- Codeforces Round #225 (Div. 1)-C. Propagating tree(线段树)
- Codeforces Round#225 div.2E Propagating tree 线段树
- codeforces#225-C - Propagating tree-dfs序(奇偶)+线段树
- Codeforces Round #225 (Div. 1) C 树状数组 || 线段树
- CodeForces - 383C Propagating tree(dfs + 线段树)
- Codeforces 383C . Propagating tree【树状数组,dfs】
- Codeforces Round 232 Div 1 C On Changing Tree 树状数组 或 线段树
- Codeforces Round #225 (Div. 1)C(dfs+线段树)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces Round #225 (Div. 2)---E. Propagating tree(时间戳+线段树)
- Codeforces 383C Propagating tree(树状数组)
- Codeforces 383C Propagating tree(树状数组)
- CF 383C Propagating tree(dfs序+线段树)
- 【CodeForces】383C Propagating tree 线段树
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序+线段树)
- CodeForces 384E Propagating tree 树状数组dfs序组合使用
- 一些有用的函数
- TOEFL writing
- iOS 缓存清除方法
- 排序算啊之选择排序
- stack or queue
- Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)
- JPA中注解的应用(学习笔记)
- numpy学习笔记二:numpy基础
- Linux屏幕录像
- angular $location服务
- linux基本命令(40)——wc命令
- Git安装及常用命令简介
- Learning OpenCV:cvCvtColor and cvCanny
- Android官方开发文档Training系列课程中文版:动画视图之转场框架介绍