(tag)没开够(WA)了一发。。。
求出(dfs)序,然后按深度分类更新与查询。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("
----------
")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 200007;
struct Edge{
int nxt, pre;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v){
e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
int dep[N], L[N], R[N], dfnIndex;
inline void DFS(int u, int fa){
dep[u] = dep[fa] + 1, L[u] = ++dfnIndex;
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue;
DFS(v, u);
}
R[u] = dfnIndex;
}
int val[N];
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
int t[N << 2], tag[N << 3];
inline void Pushup(int rt){
t[rt] = t[rt << 1] + t[rt << 1 | 1];
}
inline void Pushdown(int rt, int l, int r){
if(!tag[rt]) return;
tag[rt << 1] += tag[rt];
tag[rt << 1 | 1] += tag[rt];
int mid = (l + r) >> 1;
t[rt << 1] += (mid - l + 1) * tag[rt];
t[rt << 1 | 1] += (r - mid) * tag[rt];
tag[rt] = 0;
}
inline void Updata(int rt, int l, int r, int L, int R, int w){
if(L <= l && r <= R){
tag[rt] += w;
t[rt] += (r - l + 1) * w;
return;
}
Pushdown(rt, l, r);
int mid = (l + r) >> 1;
if(L <= mid)
Updata(lson, L, R, w);
if(R > mid)
Updata(rson, L, R, w);
Pushup(rt);
}
inline int Query(int rt, int l, int r, int x){
if(l == r){
return t[rt];
}
Pushdown(rt, l, r);
int mid = (l + r) >> 1;
if(x <= mid)
return Query(lson, x);
else
return Query(rson, x);
}
int main(){
//FileOpen();
int n, m;
io >> n >> m;
R(i,1,n){
io >> val[i];
}
R(i,2,n){
int u, v;
io >> u >> v;
add(u, v);
add(v, u);
}
DFS(1, 0);
while(m--){
int opt;
io >> opt;
if(opt == 1){
int x, w;
io >> x >> w;
Updata(1, 1, n, L[x], R[x], dep[x] & 1 ? w : -w);
}
else{
int x;
io >> x;
printf("%d
", val[x] + Query(1, 1, n, L[x]) * (dep[x] & 1 ? 1 : -1));
}
}
return 0;
}