AC日记——Propagating tree Codeforces 383c
2 seconds
256 megabytes
standard input
standard output
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1.
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.
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.
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.
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
3
3
0
The values of the nodes are [1, 2, 1, 1, 2] at the beginning.
Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1, - 2, - 1].
Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are[3, 3, - 1, 0, 1].
You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)
思路;
dfs序同时处理深度,然后搞搞就a了;
来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 200005
#define LL long long using namespace std; struct EdgeType {
int v,e;
};
struct EdgeType edge[maxn<<]; struct TreeNodeType {
int l,r,dis,mid,flag;
};
struct TreeNodeType tree[maxn<<]; int if_z,n,m,dis[maxn],head[maxn];
int cnt,f[maxn],li[maxn],ri[maxn];
int id[maxn],dis_[maxn],type,x,ans; bool deep[maxn]; char Cget; inline void in(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} void search_1(int now,int fa)
{
deep[now]=!deep[fa],f[now]=fa;
id[now]=++cnt,li[now]=cnt,dis_[cnt]=dis[now];
if(!deep[now]) dis_[cnt]*=-;
for(int i=head[now];i;i=edge[i].e)
{
if(edge[i].v==f[now]) continue;
search_1(edge[i].v,now);
}
ri[now]=cnt;
} void tree_build(int now,int l,int r)
{
tree[now].l=l,tree[now].r=r;
if(l==r)
{
tree[now].dis=dis_[l];
return ;
}
tree[now].mid=(l+r)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
} void tree_do(int now,int l,int r)
{
if(tree[now].l==l&&tree[now].r==r)
{
if(type==)
{
tree[now].flag+=x;
if(l==r) tree[now].dis+=x;
}
else ans+=tree[now].dis;
return ;
}
if(tree[now].flag)
{
tree[now<<].flag+=tree[now].flag;
tree[now<<|].flag+=tree[now].flag;
if(tree[now<<].l==tree[now<<].r) tree[now<<].dis+=tree[now].flag;
if(tree[now<<|].l==tree[now<<|].r) tree[now<<|].dis+=tree[now].flag;
tree[now].flag=;
}
if(l>tree[now].mid) tree_do(now<<|,l,r);
else if(r<=tree[now].mid) tree_do(now<<,l,r);
else
{
tree_do(now<<,l,tree[now].mid);
tree_do(now<<|,tree[now].mid+,r);
}
} int main()
{
in(n),in(m);int u,v;
for(int i=;i<=n;i++) in(dis[i]);
for(int i=;i<n;i++)
{
in(u),in(v);
edge[++cnt].v=v,edge[cnt].e=head[u],head[u]=cnt;
edge[++cnt].v=u,edge[cnt].e=head[v],head[v]=cnt;
}
cnt=,search_1(,);
tree_build(,,n);
while(m--)
{
in(type),in(u);
if(type==)
{
in(x);
if(!deep[u]) x*=-;
tree_do(,li[u],ri[u]);
}
else
{
ans=;
tree_do(,li[u],li[u]);
if(!deep[u]) ans*=-;
printf("%d\n",ans);
}
}
return ;
}
AC日记——Propagating tree Codeforces 383c的更多相关文章
- AC日记——825G - Tree Queries
825G - Tree Queries 思路: 神题,路径拆成半链: 代码: #include <cstdio> #include <cstring> #include < ...
- AC日记——Cards Sorting codeforces 830B
Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Card Game codeforces 808f
F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——T-Shirt Hunt codeforces 807b
T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- AC日记——Broken BST codeforces 797d
D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
随机推荐
- 面试:如何把xxx.sh使用/etc/init.d/xxx.sh start启动,并且可以用chkconfig配置开机自启动
chkconfig原理: 1.脚本放到/etc/init.d下面,并且可执行(/etc/init.d/sshd) 需要被chkconfig管理,需要添加进去chkconfig --add sshd ...
- python数据类型之集合(set)和其常用方法
集合是一个无序的,不重复的数据组合 作用(集合的重点):1.去重,把一个列表变成集合就自动去重了2.关系测试,测试两组数据库之前的交集.差集.并集等关系 s = {1, 1, 2, 2, 3, 4, ...
- sublime text3 安装ctags实现函数跟踪跳转
来源:http://blog.csdn.net/menglongfc/article/details/51141084 本人试用平台如下:sublime text3,和谐版 在source insig ...
- debian使用ibus
$ sudo apt-get install ibus ibus-pinyin 点击右上角的键盘图标,设置拼音输入法
- 离线安装eclipse maven插件
最近用到maven,所以按照官网http://www.eclipse.org/m2e/的教程http://download.eclipse.org/technology/m2e/releases/,在 ...
- flask_关注者
表的模型实现 class Follow(db.Model): __tablename__ = 'follows' follower_id = db.Column(db.Integer,db.Forei ...
- fiddler 抓包数据不会自动下拉解决方法
选中 view 里面的 AutoScroll Session List 即可
- Python的深浅copy
27.简述Python的深浅拷贝以及应用场景? 深浅拷贝的原理 深浅拷贝用法来自copy模块. 导入模块:import copy 浅拷贝:copy.copy 深拷贝:copy.deepcopy 字面理 ...
- Ubuntu 14.04 LTS 安装和配置Bochs
Ubuntu 14.04 LTS 安装和配置Bochs 系统是:Ubuntu 14.04 LTS 64位 安装的是:bochs-2.6.8 Bochs 需要在 X11 环境下运行,因此你的 ...
- hnust 不爱学习的小w
问题 C: 不爱学习的小W 时间限制: 2 Sec 内存限制: 64 MB提交: 1431 解决: 102[提交][状态][讨论版] 题目描述 “叮铃铃”上课了,同学们都及时到了教室坐到了座位上, ...