Bzoj 4034(树链剖分)

BZOJ 4034

树剖后线段树维护。
此题要修改子树的权,根据树剖性质子树是连续的一段,运用时间戳思想即可。

注意开long long,第一次没开就WA了(痛不欲生)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, j, k) for (i=(j);i<=(k);i++)
#define fd(i, k, j) for (i=(k);i>=(j);i--)
#define fe(i, u) for (i=head[u];i!=-1;i=e[i].next)
#define rd(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d%d", &a, &b)
#define rd3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define FN2 "bzoj4034"
using namespace std;
const int MAXN = 100000 + 5;
int n, m, head[MAXN], wi[MAXN], cnt;
struct data{int to, next;}e[MAXN*2];
int p[MAXN], top[MAXN], son[MAXN], siz[MAXN], fa[MAXN], dep[MAXN], pre, ll[MAXN], rr[MAXN];
void dfs1(int u, int pa) {
int i;
fa[u] = pa, dep[u] = dep[pa] + 1, siz[u] = 1;
fe (i, u) {
int v = e[i].to;
if (v!=pa) {
dfs1(v, u);
siz[u] += siz[v];
if (son[u]==-1||siz[son[u]]<siz[v]) son[u] = v;
}
}
}
void dfs2(int u, int chain) {
int i;
p[u] = ++pre, top[u] = chain, ll[u] = pre;
if (son[u]!=-1) {
dfs2(son[u], chain);
fe (i, u) {
int v = e[i].to;
if (v!=fa[u]&&v!=son[u]) dfs2(v, v);
}
}
rr[u] = pre;
}
LL sumv[MAXN*4], addv[MAXN*4];
#define lc (o<<1)
#define rc (o<<1|1)
#define M ((l+r)>>1)
void pushup(LL o) {
sumv[o] = sumv[lc] + sumv[rc];
}
void pushdown(int o,int len) {
if (addv[o]) {
addv[lc] += addv[o], addv[rc] += addv[o];
sumv[lc] += addv[o] * (len-len/2), sumv[rc] += addv[o] * (len/2);
addv[o] = 0;
}
}
void update(int o, int l, int r, int x, int y, LL v) {
if (x<=l&&r<=y) {
addv[o] += v;
sumv[o] += (r-l+1) * v;
return ;
}
pushdown(o, r-l+1);
if (x<=M) update(lc,l,M,x,y,v);
if (M<y) update(rc,M+1,r,x,y,v);
pushup(o);
}
LL query(int o, int l, int r, int x, int y) {
LL ret = 0;
if (x<=l&&r<=y) {
return sumv[o];
}
pushdown(o, r-l+1);
if (x<=M) ret += query(lc,l,M,x,y);
if (M<y) ret += query(rc,M+1,r,x,y);
return ret;
}
LL findSUM(int u, int v) {
int f1 = top[u], f2 = top[v]; LL ret = 0;
while (f1!=f2) {
if (dep[f1]<dep[f2]) swap(f1, f2), swap(u, v);
ret += query(1,1,n,p[f1],p[u]);
u = fa[f1], f1 = top[u];
}
if (dep[u]<dep[v]) swap(u, v);
return ret+query(1,1,n,p[v],p[u]);
}
void ins(int u, int v) {
cnt++, e[cnt].to = v, e[cnt].next = head[u], head[u] = cnt;
cnt++, e[cnt].to = u, e[cnt].next = head[v], head[v] = cnt;
}
void init() {
int i; cnt = pre = 0;
fo (i, 1, n) rd(wi[i]);
fo (i, 1, n) head[i] = -1, p[i] = 0, top[i] = 0, son[i] = -1, siz[i] = 0, fa[i] = 0, dep[i] = 0, ll[i] = rr[i] = 0;
fo (i, 1, n*2) e[i].to = 0, e[i].next = -1;
fo (i, 1, n*4) sumv[i] = addv[i] = 0;
fo (i, 1, n-1) {
int u, v; rd2(u, v);
ins(u, v);
}
}
void solve() {
int i;
dfs1(1, 0), dfs2(1, 1);
fo (i, 1, n) update(1,1,n,p[i],p[i],wi[i]);
fo (i, 1, m) {
int opt, x, a; rd(opt);
if (opt==1) {
rd2(x, a);
update(1,1,n,p[x],p[x],a);
} else if (opt==2) {
rd2(x, a);
update(1,1,n,ll[x],rr[x],a);
} else if (opt==3) {
rd(x);
printf("%lld\n", findSUM(x, 1));
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen(FN2".in","r",stdin);freopen("1.out","w",stdout);
#endif
while (rd2(n, m)==2) init(), solve();
return 0;
}

------ 本文结束 ------