题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作,

第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 -(-v),依次。。。

第二种:2 x, 表示查询 x 结点的权值。

析:因为这是一棵树,很难维护,所以可以考虑先用 dfs 记录每个结点的开始和结束的时间戳,而位于开始和结束时间戳内的就是就是它的子孙结点,

这样就能维护两棵线段树,一棵是奇数层的, 一棵是偶数层的,每次执行 1 操作就把相应的结点的开始和结束作为一个区间进行更新,然后再执行

相反层的 -v 进行更新。当查询的时候,就直接输出相应层的输出再加原来权值即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int dfsnum[maxn], in[maxn];
int a[maxn], out[maxn], dep[maxn];
int sum[2][maxn<<2], addv[2][maxn<<2];
vector<int> G[maxn];
int cnt; void dfs(int u, int fa, int d){
dep[u] = d;
in[u] = ++cnt; for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(v == fa) continue;
dfs(v, u, d+1);
}
out[u] = cnt;
} void push_down(int *sum, int rt, int *addv){
if(addv[rt] == 0) return ;
int l = rt<<1, r = rt<<1|1;
sum[l] += addv[rt];
sum[r] += addv[rt];
addv[l] += addv[rt];
addv[r] += addv[rt];
addv[rt] = 0;
} void update(int *sum, int *addv, int L, int R, int val, int l, int r, int rt){
if(L <= l && r <= R){
addv[rt] += val;
sum[rt] += val;
return ;
}
push_down(sum, rt, addv);
int m = l + r >> 1;
if(L <= m) update(sum, addv, L, R, val, lson);
if(R > m) update(sum, addv, L, R, val, rson);
} int query(int *sum, int *addv, int M, int l, int r, int rt){
if(l == r) return sum[rt];
push_down(sum, rt, addv);
int m = l + r >> 1;
if(M <= m) return query(sum, addv, M, lson);
return query(sum, addv, M, rson);
} int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1, 1);
while(m--){
int x, c, op;
scanf("%d %d", &op, &x);
int t = dep[x]&1;
if(op == 1){
scanf("%d", &c);
update(sum[t], addv[t], in[x], out[x], c, 1, n, 1);
if(in[x] < out[x])
update(sum[t^1], addv[t^1], in[x]+1, out[x], -c, 1, n, 1);
}
else printf("%d\n", query(sum[t], addv[t], in[x], 1, n, 1) + a[x]);
}
return 0;
}

  

CodeForces 384E Propagating tree (线段树+dfs)的更多相关文章

  1. codeforces 383C Propagating tree 线段树

    http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...

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

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

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

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

  4. CF620E New Year Tree 线段树 dfs序

    luogu链接 题目大意: 有一个节点有颜色的树 操作1.修改子树的颜色 操作2.查询子树颜色的种类 注意,颜色种类小于60种 只有子树的操作,dfs序当然是最好的选择 dfs序列是什么,懒得讲了,自 ...

  5. CF620E New Year Tree 线段树+dfs序+bitset

    线段树维护 dfs 序是显然的. 暴力建 60 个线段树太慢,于是用 bitset 优化就好了 ~ code: #include <bits/stdc++.h> #define M 63 ...

  6. S - Query on a tree HDU - 3804 线段树+dfs序

    S - Query on a tree HDU - 3804   离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...

  7. HDU 5692 线段树+dfs序

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  8. Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

    题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB    总提交次数:196   AC次数:65   平均分: ...

  9. 【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序

    题目大意 ​ Bob有一棵\(n\)个点的有根树,其中\(1\)号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜 ...

随机推荐

  1. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一)

    最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...

  2. wpf和winform的那点区别

    一提起winform和wpf有啥不同,可能大部分人都会想到wpf拥有漂亮的界面.wpf中也可以使用winform,大部分情况下,都没有任何问题,极少的情况下,可能会出现问题. 我们就来看一下: voi ...

  3. GO语言list剖析

    GO语言list剖析 本节内容 使用方法 list提供的方法 源码剖析 1. 使用方法 在GO语言的标准库中,提供了一个container包,这个包中提供了三种数据类型,就是heap,list和rin ...

  4. 消息队列mq总结(重点看,比较了主流消息队列框架)

    转自:http://blog.csdn.net/konglongaa/article/details/52208273 http://blog.csdn.net/oMaverick1/article/ ...

  5. http之206状态码

    206状态码, 大概就是浏览器先不下载要下载的文件,而是弹窗告诉用户,该文件是什么,有多大.由用户自行决定是否下载. 在html中,加一个a标签,a标签的地址是一个文件,就可实现该效果. 具体可参考下 ...

  6. 第11篇 PSR-0 规范

    Mandatory A fully-qualified namespace and class must have the following structure \<Vendor Name&g ...

  7. immutable学习

    React 做性能优化时有一个避免重复渲染的大招,就是使用 shouldComponentUpdate(),但它默认返回 true,即始终会执行 render() 方法,然后做 Virtual DOM ...

  8. UVA548(二叉树遍历)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  9. [转载]Linux内核list_head学习(二)

    前一篇文章讨论了list_head 结构的基本结构和实现原理,本文主要介绍一下实例代码. 自己如果想在应用程序中使用list_head 的相应操作(当然应该没人使用了,C++ STL提供了list 用 ...

  10. Linux内核 - 定时器

    #include <linux/timer.h> //头文件 struct timer_list mytimer; //定义变量 static void my_timer(unsigned ...