codeforces 383C Propagating tree 线段树
http://codeforces.com/problemset/problem/383/C
题目就是说, 给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val........这样类推, 给一系列操作,2是查询一个节点的值, 1是将一个节点的值+val。
首先,<dfs一遍求出dfs序以及每个点的深度, 然后建两颗线段树, 深度为奇数的建一棵, 偶数建一棵。 改变一个节点的值, 就把与他奇偶相同的那颗树全都+val, 不同的全都-val。 具体看代码>.....
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int in[maxn], out[maxn], num, val[maxn], head[maxn], dis[maxn], dfs_clock;
int cnt[maxn<<][];
struct node
{
int to, nextt;
}e[maxn*];
void add(int u, int v) {
e[num].to = v;
e[num].nextt = head[u];
head[u] = num++;
}
void dfs(int u, int fa) {
dis[u] = dis[fa]+;
in[u] = ++dfs_clock;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs(v, u);
}
out[u] = dfs_clock;
}
void pushDown(int rt, int sign) {
if(cnt[rt][sign]) {
cnt[rt<<][sign] += cnt[rt][sign];
cnt[rt<<|][sign] += cnt[rt][sign];
cnt[rt][sign] = ;
}
}
void update(int L, int R, int l, int r, int rt, int val, int sign) {
if(L<=l && R>=r) {
cnt[rt][sign] += val;
return ;
}
pushDown(rt, sign);
int m = l+r>>;
if(L<=m)
update(L, R, lson, val, sign);
if(R>m)
update(L, R, rson, val, sign);
}
int query(int p, int l, int r, int rt, int sign) {
if(l == r) {
return cnt[rt][sign];
}
pushDown(rt, sign);
int m = l+r>>;
int ret = ;
if(p<=m)
return query(p, lson, sign);
else
return query(p, rson, sign);
}
int main()
{
int n, m, x, y, z;
cin>>n>>m;
for(int i = ; i<=n; i++) {
scanf("%d", &val[i]);
}
mem1(head);
for(int i = ; i<n-; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dis[] = ;
dfs(, );
while(m--) {
scanf("%d", &z);
if(z == ) {
scanf("%d", &x);
printf("%d\n", query(in[x], , n, , dis[x]&)+val[x]);
} else {
scanf("%d%d", &x, &y);
update(in[x], out[x], , n, , y, dis[x]&);
update(in[x], out[x], , n, , -y, !(dis[x]&));
}
}
}
codeforces 383C Propagating tree 线段树的更多相关文章
- Codeforces 383C Propagating tree, 线段树, 黑白染色思想
按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...
- Codeforces 383C . Propagating tree【树阵,dfs】
标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...
- CodeForces 384E Propagating tree (线段树+dfs)
题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
随机推荐
- 详解Linq to SQL
第一部分,什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并 ...
- c# 自定义位数生成激活码
Random random = new Random(~unchecked((int)DateTime.Now.Ticks));private string CreateAndCheckCode(Ra ...
- linux通过文件查找依赖关系
通过文件查找安装包安装缺少libstdc++6这个文件在ls /usr/lib/libstd*下有两个文件/usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6 ...
- ipa 重签
IPA 重签名 时间 2014-03-03 10:28:36 txx's blog原文 http://blog.rpplusplus.me/blog/2014/03/03/ipa-re-codes ...
- static 和 extern
外部函数:定义的函数能被本文件和其他文件访问,默认所有的情况都是外部函数,不允许有同名的外部函数 >>extern定义和声明一个外部函数(可以省略) 内部函数:定义的函数只能被本文件访问, ...
- WireShark抓包软件的使用
在大学的时候学习的计算机网络的具体的知识,也使用过wireshark去抓取一些网络的封包用来分析,但是都没能系统的写一篇博客,今天总结一下吧. wireshark介绍 wireshark的官方下载网站 ...
- python-摩尔斯电码查询器
主程序代码:morse_query.py #-*- coding: UTF-8 -*- ' __date__ = '2016/4/5' from Tkinter import * class mors ...
- SQL Server带游标的SQL
DECLARE test_cursor CURSOR FOR SELECT ID FROM dbo.T_BD_Restaurant WHERE id <> '0AAB2E55-79F8-4 ...
- web本地存储-UserData
userData,IE中持久化用户数据的方法. 使用userData用户数据首先必须使用css指定userData行为.代码示例: var ud = document.createElement(&q ...
- centos6.5 eclipse C/C++开发环境