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 线段树的更多相关文章

  1. Codeforces 383C Propagating tree, 线段树, 黑白染色思想

    按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...

  2. Codeforces 383C . Propagating tree【树阵,dfs】

    标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...

  3. CodeForces 384E Propagating tree (线段树+dfs)

    题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...

  4. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  5. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  6. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  7. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  8. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  9. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

随机推荐

  1. An Data-Scientist Prepares 《数据科学家的自我修养》

    从今天开始,博主将用大概1000天的时间记录自己学习并成为初级数据科学家(数据分析师)的心路历程. 包括数据科学家所必需的的基础知识:数学,统计,计算机,商业,沟通能力等. 希望博主能够在2017前完 ...

  2. sqlserver 2008 局域网跨服务器T-SQL操作(二)

    --判断是否开启远程操作服务,如果关闭,则开启,用完之后关闭 DECLARE @value SQL_VARIANT SELECT @value=VALUE from sys.configuration ...

  3. asp.net mvc 生成条形码

    using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...

  4. tabbedApliction

    一.手动创建UITabBarController 最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLa ...

  5. Object-C 重载

    方法重载要保证三个条件 1在同一个类中 2.方法参数类型相同 名称相同 3.方法的参数不同 请看下面的例子 @interface whgMyObject : NSObject -(void)print ...

  6. iOS设计模式解析(三)适配器模式

    适配器模式:将一个类的借口转换成客户端希望的另一个接口 有一个很直观的图: 例如      :电源适配器(将110V电压转换成220V电压,其中Traget是220V电压,adaptee就是110V电 ...

  7. Intellij IDEA开发第一个android应用教程

    用惯eclipse的同学们可以试试通过Intellij IDEA来开发一个android应用.下面是具体的教程. 首先:下载Intellij IDEA.最新版本是12.官方提供两个版本.一个是Comm ...

  8. VS 2012 插件卸载(删除自己安装的插件)

    给VS 装了一个插件,装完之后感觉别扭,所以想卸载,[工具]--> [扩展和更新]-->[找到想要卸载的插件点击一下就会出现禁用或卸载]

  9. YBC中国国际青年创业计划

    YBC中国国际青年创业计划 中国青年创业国际计划(简称YBC)是共青团中央.中华全国青年联合会.中华全国工商业联合会共同倡导发起的青年创业教育项目.该项目参考总部在英国的青年创业国际计划( Youth ...

  10. Clojure 学习入门(19)—— 数组

    1.创建数组 1.1 从集合创建数组 into-array into-array (into-array aseq) (into-array type aseq) 演示样例: user=> (i ...