2019 NanChan ICPC K.Tree

  • $v[i]+v[j]=2v[lca(i,j)]$
  • $dis(i,j)\leq k$
  • $i,j$不在一条链上。

由于条件$3$,无法用点分治。

考虑$dsu \ on \ tree$。

只需要每次计算单独链计算,并且计算完添加进去。

开个$Segment$,$rt[i]$表示$v[x]=i$的所有$dep$,动态开点,动态删除,加个回收就$ok$了。

一遍过。

代码
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216



#include <bits/stdc++.h>
#include <set>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
ll res = 0;
struct Segment
{
int ls[N << 4], rs[N << 4], siz[N << 4];
queue<int> q;
int scnt = 0;
int Newpos()
{
int x;
if (!q.empty())
{
x = q.front();
q.pop();
}
else
x = ++scnt;
return x;
}
void pushup(int pos)
{
siz[pos] = siz[ls[pos]] + siz[rs[pos]];
}
void insert(int &pos, int w, int l, int r)
{
if (!pos)
pos = Newpos();
if (l == r)
{
siz[pos]++;
return;
}
int mid = (l + r) >> 1;
if (w <= mid)
insert(ls[pos], w, l, mid);
else
insert(rs[pos], w, mid + 1, r);
pushup(pos);
}
void clear(int &pos, int w, int l, int r)
{

if (l == r)
{
siz[pos]--;
return;
}
int mid = (l + r) >> 1;
if (w <= mid)
clear(ls[pos], w, l, mid);
else
clear(rs[pos], w, mid + 1, r);

pushup(pos);

// if (siz[pos] == 0)
// {
// q.push(pos);
// pos = 0;
// }
}
int query(int ql, int qr, int pos, int l, int r)
{
if (siz[pos] == 0 || pos == 0)
return 0;
if (ql <= l && r <= qr)
{
return siz[pos];
}
int mid = (l + r) >> 1;
int ans = 0;
if (ql <= mid)
ans += query(ql, qr, ls[pos], l, mid);
if (qr > mid)
ans += query(ql, qr, rs[pos], mid + 1, r);
return ans;
}
} t;
int n, k;
vector<int> g[N];
int wi[N];
int rt[N];
int dep[N], siz[N], son[N];
void dfs1(int x, int fa)
{
dep[x] = dep[fa] + 1;
siz[x] = 1;

for (int to : g[x])
{
if (to == fa)
continue;
dfs1(to, x);

siz[x] += siz[to];
if (siz[son[x]] < siz[to])
son[x] = to;
}
}

int Nowson;

vector<int> p;
void dfs3(int x, int fa)
{
p.push_back(x);
for (int to : g[x])
{
if (to == fa)
continue;
dfs3(to, x);
}
}
void count(int x, int fa)
{

for (int to : g[x])
{
if (to == fa || to == Nowson)
continue;
p.clear();

dfs3(to, x);

for (int i : p)
{
int o = 2 * dep[x] + k - dep[i];
int z = 2 * wi[x] - wi[i];

if (z >= 0 && z <= n)
{
res += t.query(1, min(o, n), rt[z], 1, n);
//cout << t.query(1, min(o, n), rt[z], 1, n) << endl;
}
}
for (int i : p)
{

t.insert(rt[wi[i]], dep[i], 1, n);
}
}

t.insert(rt[wi[x]], dep[x], 1, n);
}
void clear(int x, int fa)
{
p.clear();
p.push_back(x);
for (int to : g[x])
{
if (to == fa)
continue;
dfs3(to, x);
}
for (int i : p)
{
t.clear(rt[wi[i]], dep[i], 1, n);
}
}
void dfs2(int x, int fa, bool op)
{

for (int to : g[x])
{
if (to == fa || to == son[x])
continue;
dfs2(to, x, 0);
}
if (son[x])
dfs2(son[x], x, 1);
Nowson = son[x];
count(x, fa);
Nowson = 0;
if (!op)
clear(x, fa);
}
int main()
{
n = read(), k = read();
for (int i = 1; i <= n; i++)
wi[i] = read();
for (int i = 2; i <= n; i++)
{
int x = read();
g[x].push_back(i);
}

dfs1(1, 0);
dfs2(1, 0, 0);
cout << res * 2 << endl;
}