题意:题意很简单么,给定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; }