May Holidays

题目传送门

树链剖分+分块

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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> g[maxn];
int sz[maxn], fa[maxn], dep[maxn], son[maxn];
int top[maxn], dfn[maxn];
int cnt = 0;
void dfs1(int u, int father) {
dep[u] = dep[father] + 1;
fa[u] = father;
sz[u] = 1;
son[u] = -1;
for(auto v : g[u]) {
if(v == father) continue;
dfs1(v, u);
sz[u] += sz[v];
if(son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int t) {
top[u] = t;
dfn[u] = ++cnt;
if(son[u] == -1) return;
dfs2(son[u], t);
for(auto v : g[u]) {
if(v == son[u] || v == fa[u]) continue;
dfs2(v, v);
}
}
int a[maxn], fd[maxn];
int fl[405], fr[405], tag[405], ans = 0;
bool off[maxn];
unsigned char fp[405][maxn << 1];
void add(int k, int l, int r, int v) {
for(int i = l;i <= r; i++) {
if(off[i]) {
a[i] += v;
continue;
}
if(a[i] + tag[k] < maxn) ans--;
fp[k][a[i]]--;
a[i] += v;
if(a[i] + tag[k] < maxn) ans++;
fp[k][a[i]]++;
}
}
void add(int x, int v) {
off[x] ^= 1;
ans += v * (a[x] + tag[fd[x]] < maxn);
fp[fd[x]][a[x]] += v;
}
void update(int l, int r, int v) {
if(fd[l] == fd[r]) add(fd[l], l, r, v);
else {
add(fd[l], l, fr[fd[l]], v);
add(fd[r], fl[fd[r]], r, v);
for(int i = fd[l] + 1;i < fd[r]; i++) {
if(v < 0) ans += fp[i][maxn - tag[i]];
tag[i] += v;
if(v > 0) ans -= fp[i][maxn - tag[i]];
}
}
}
void modify(int u, int v, int val) {
int x = top[u], y = top[v];
while(x != y) {
int &w = dep[x] > dep[y] ? u : v;
int &z = dep[x] > dep[y] ? x : y;
update(dfn[z], dfn[w], val);
w = fa[z];
z = top[w];
}
u = dfn[u];
v = dfn[v];
if(u > v) swap(u, v);
update(u, v, val);
}
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, m, i, j, u;
scanf("%d%d", &n, &m);
for(i = 2; i <= n; i++) {
scanf("%d", &u);
g[u].push_back(i);
}
dfs1(1, 0);
dfs2(1, 1);
for(i = 1; i <= n; i++) scanf("%d", &a[dfn[i]]);
for(i = 1; i <= n; i++) a[i] += maxn;
fl[1] = 1;
for(i = j = u = 1; i <= n; i++, u++) {
if(u == 333) {
j++;
u = 1;
fl[j] = i;
}
fd[i] = j;
fr[j] = i;
fp[j][a[i]]++;
}
for(i = 0;i < m; i++) {
scanf("%d", &u);
if(u > 0) {
add(dfn[u], -1);
modify(u, 1, -1);
} else {
add(dfn[-u], 1);
modify(-u, 1, 1);
}
printf("%d%c", ans, " \n"[i + 1 == m]);
}
return 0;
}