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不小于 ...
随机推荐
- 【Python学习之六】高阶函数2(map、reduce、filter、sorted)
3.filter filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素.相当于一 ...
- 爬虫之Scrapy和分页
下一页和详情页的处理 xpath提取时 注意: 结合网页源代码一起查找 不用框架的爬取 获取下一页 自带href属性 1)首页有下一页 next_url = element.xpath('.//a[t ...
- 利用Windbg深入理解变量的存储模型
下面的是一个简单的测试程序,基本包括了所有的变量类型,包括静态的,常量的,全局的,本地的,还有new出来的 #include <iostream> using namespace std; ...
- Linux性能查看
1.TOP top 登录后默认按进程的CPU使用情况排序, 按M则按内存使用排序 2. vmstat 2 2 显示系统负载 3. free -m 查看内存使用情况 4.抓包 tcpdump -i ...
- python 学习分享-字典篇
python字典(Dictionary) dict是无序的 key必须是唯一切不可变的 a={'key1':'value1','key2':'value2'} 字典的增删改查 a['key3']='v ...
- df和du显示的磁盘空间使用情况不一致问题
背景介绍: dba同事删除了mysql /datao目录下的文件,通过du –sh查看空间使用700G,df -h查看空间使用1T,没有重启mysql服务. 另一个表现出du与df命令不同之处的例子如 ...
- [python学习篇][廖雪峰][1]高级特性 ---迭代
由于字符串也是可迭代对象,因此,也可以作用于for循环: >>> for ch in 'ABC': ... print ch ... A B C 所以,当我们使用for循环时,只要作 ...
- AtCoder Grand Contest 021
A - Digit Sum 2 Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Find t ...
- API生命周期第三阶段:API实施:使用swagger codegen生成可部署工程,择取一个作为mock service
在分享培训了swagger对于API的设计之后,有一些人问我说:你看,现在咱们前端使用web_API做为mock data在进行测试,后端也有mock 测试.然后我们再进行联调,这之中肯定会出现一些偏 ...
- python操redis
Python操作redis python连接方式:点击 下面介绍详细使用 1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redi ...