Bzoj 4196(树链剖分)

BZOJ 4196

树链剖分支持修改查找子树和到根的路径权值和即可解决,注意pushdown放前面注意lc,rc会爆的情况

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#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 FN2 "bzoj4196"
using namespace std;
const int MAXN = 100000 + 500;
int n, head[MAXN], cnt;
int p[MAXN], dep[MAXN], fa[MAXN], top[MAXN], son[MAXN], siz[MAXN], pre, ll[MAXN], rr[MAXN];
struct data{int to, next;}e[MAXN*2];
void dfs1(int u, int pa) {
int i;
dep[u] = dep[pa] + 1, fa[u] = pa, 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;
}
int sumv[MAXN*4], lazy[MAXN*4];
#define lc (o<<1)
#define rc (o<<1|1)
#define M ((l+r)>>1)
void pushup(int o) {
sumv[o] = sumv[lc] + sumv[rc];
}
void pushdown(int o, int len) {
if (len==1) return ;
if (lazy[o]!=-1) {
lazy[lc] = lazy[rc] = lazy[o];
sumv[lc] = lazy[o] * (len-len/2);
sumv[rc] = lazy[o] * (len/2);
lazy[o] = -1;
}
}
void update(int o, int l, int r, int x, int y, int v) {
pushdown(o, r-l+1);
if (x<=l&&r<=y) {
lazy[o] = v;
sumv[o] = v * (r-l+1);
return ;
}
if (x<=M) update(lc,l,M,x,y,v);
if (M<y) update(rc,M+1,r,x,y,v);
pushup(o);
}
int query(int o, int l, int r, int x, int y) {
pushdown(o, r-l+1);
int ret = 0;
if (x<=l&&r<=y) {
return sumv[o];
}
if (x<=M) ret += query(lc,l,M,x,y);
if (M<y) ret += query(rc,M+1,r,x,y);
return ret;
}
int findRoute(int u, int v) {
int f1 = top[u], f2 = top[v], ret = 0, tot = 0;
while (f1!=f2) {
if (dep[f1]<dep[f2]) swap(f1, f2), swap(u, v);
ret += query(1,1,n,p[f1],p[u]);
tot += p[u] - p[f1] + 1;
u = fa[f1];
f1 = top[u];
}
if (dep[u]<dep[v]) swap(u, v);
ret += query(1,1,n,p[v],p[u]);
tot += p[u] - p[v] + 1;
return tot - ret;
}
int changeRoute(int u, int v, int c) {
int f1 = top[u], f2 = top[v];
while (f1!=f2) {
if (dep[f1]<dep[f2]) swap(f1, f2), swap(u, v);
update(1,1,n,p[f1],p[u],c);
u = fa[f1];
f1 = top[u];
}
if (dep[u]<dep[v]) swap(u, v);
update(1,1,n,p[v],p[u],c);
}
int findSub(int u) {
int ret = query(1,1,n,ll[u],rr[u]);
return ret;
}
int changeSub(int u, int c) {
update(1,1,n,ll[u],rr[u],c);
}
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; pre = cnt = 0;
fo (i, 1, n) head[i] = -1, p[i] = 0, dep[i] = 0, fa[i] = 0, top[i] = 0, son[i] = -1, siz[i] = 0, ll[i] = rr[i] = 0;
fo (i, 1, 2*n) e[i].to = 0, e[i].next = -1;
fo (i, 1, 4*n) sumv[i] = 0, lazy[i] = -1;
fo (i, 1, n-1) {
int a; rd(a); ins(a+1, i+1);
}
}
void solve() {
int i;
dfs1(1, 0), dfs2(1, 1);
int q, x; rd(q);
char ch[20];
fo (i, 1, q) {
scanf("%s", ch);
if (ch[0]=='i') {
rd(x);
printf("%d\n", findRoute(x+1, 1));
changeRoute(x+1,1,1);
} else {
rd(x);
printf("%d\n", findSub(x+1));
changeSub(x+1,0);
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen(FN2".in","r",stdin);freopen("1.out","w",stdout);
#endif
while (rd(n)==1) init(), solve();
return 0;
}
------ 本文结束 ------