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不小于 ...
随机推荐
- (75)zabbix_server.conf配置文件详解
############ GENERAL PARAMETERS ################# ### Option: ListenPort# Listen port for trapper.# ...
- (69)zabbix监控惠普打印机
假设公司有多个楼层或者分布在不同楼,打印机自然分布很广泛,打印机缺少油墨或者卡纸了,都需要员工找IT部门.我们使用zabbix对打印机进行监控,一旦缺少油墨,zabbix发出报警,it人员能够及时更换 ...
- Springboot @Autowired 无法注入问题
特别提醒:一定要注意文件结构 WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入. 1. 添加工具类获取在 Sprin ...
- vsftpd服务安装与虚拟用户配置
vsftpd的全名是“Very secure FTP Daemon” 一.安装vsftpd安装db4-util用于生成认证文件 yum -y install db4-utils 安装vsftpd yu ...
- matplotlib学习记录 四
# 绘制3月每天最高温和10月每天最高温散点图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...
- SQL_4_函数
在SQL的函数中可以执行一些诸如对某一些进行汇总或将一个字符串中的字符转换为大写的操作等: 函数有:汇总函数.日期与时间函数.数学函数.字符函数.转换函数与其他函数. 汇总函数 这是一组函数,它们返回 ...
- selenium2-框架思想介绍
为什么要有框架? 可维护性 提高编写脚本效率 提高脚本的可读性 框架的几大要素: 1. driver管理 2. 脚本 3. 数据 4. 元素对象 5. Log 6. 报告 7. 运行机制 8. 失败用 ...
- 一个Work Stealing Pool线程池的实现
一.一般来说实现一个线程池主要包括以下几个组成部分: 1)线程管理器 :用于创建并管理线程池 . 2)工作线程 :线程池中实际执行任务的线程 . 在初始化线程时会预先创建好固定数目的线程在池中 ,这些 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it
链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...
- 大数相减 C语言
#include <stdio.h> #include <string.h> using namespace std; ],b[]; void Sub() { ; if(a = ...