#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int B = 320;
const int LOG = 18;
const int N = 1e5 + 5;
int n, m, q;
int c[N];
int dep[N];
int idd, id[N];
vector<int> g[N];
int spa[LOG][N * 2];
void dfs(int v, int p) {
id[v] = idd;
spa[0][idd++] = v;
for (int u : g[v]) {
if (u == p) {
continue;
}
dep[u] = dep[v] + 1;
dfs(u, v);
spa[0][idd++] = v;
}
}
int get(int l, int r) {
int lg = 31 - __builtin_clz(r - l + 1);
int u = spa[lg][l], v = spa[lg][r - (1 << lg) + 1];
return dep[u] < dep[v] ? u : v;
}
int distance(int u, int v) {
int l = id[u], r = id[v];
if (l > r) {
swap(l, r);
}
int lca = get(l, r);
int ret = dep[u] + dep[v] - 2 * dep[lca];
return ret;
}
int cnt[B][N];
int in_id[N * 2];
long long ans[N];
long long res[B];
int pre[B][N], nxt[B][N];
vector<pair<int, int>> qs[N];
void preprocess() {//B * N
for (int ver = 0; ver * B < m; ++ver) {//M / B (B)
for (int r = ver * B; r < m; ++r) {//M
int v = c[r];
cnt[ver][v]++;
in_id[id[v]] = v;
}
vector<int> ord;
for (int i = 0; i < idd; ++i) {//2 * N
if (in_id[i] != 0) {
ord.push_back(in_id[i]);
in_id[i] = 0;
}
}
//debug(ord);
//cerr << '\n';
for (int i = 1; i < (int) ord.size(); ++i) {//N
int u = ord[i - 1], v = ord[i];
res[ver] += distance(u, v);
pre[ver][v] = u;
nxt[ver][u] = v;
}
nxt[ver][ord.back()] = ord[0];
pre[ver][ord[0]] = ord.back();
res[ver] += distance(ord[0], ord.back());
}
}
void back(int r) {
//debug(r);
for (int ver = 0; ver * B <= r; ++ver) {//M / B
int v = c[r];
if (--cnt[ver][v] > 0) {
continue;
}
//int old = res[ver];
int cpre = pre[ver][v];
int cnxt = nxt[ver][v];
if (cpre != 0) {
res[ver] -= distance(v, cpre);
}
if (cnxt != 0) {
res[ver] -= distance(v, cnxt);
}
if (cpre != 0 && cnxt != 0) {
nxt[ver][cpre] = cnxt;
pre[ver][cnxt] = cpre;
res[ver] += distance(cpre, cnxt);
}
//debug(ver, v, cpre, cnxt, old, res[ver]);
}
//cerr << '\n';
}
int cnt2[N];
vector<int> clean;
int curpre[N], curnxt[N];
void check(int v) {
if (curpre[v] == 0 && curnxt[v] == 0) {
clean.push_back(v);
}
}
void rem(int ver, int cl, long long& ret) {
int v = c[cl];
check(v);
if (cnt2[v] == 0) {
cnt2[v] = cnt[ver][v];
}
if (curpre[v] == 0) {
curpre[v] = pre[ver][v];
}
if (curnxt[v] == 0) {
curnxt[v] = nxt[ver][v];
}
if (--cnt2[v] != 0) {
return;
}
int cpre = curpre[v];
int cnxt = curnxt[v];
if (cpre != 0) {
ret -= distance(v, cpre);
}
if (cnxt != 0) {
ret -= distance(v, cnxt);
}
if (cpre != 0 && cnxt != 0) {
check(cpre);
check(cnxt);
curpre[cnxt] = cpre;
curnxt[cpre] = cnxt;
ret += distance(cpre, cnxt);
}
}
long long qry(int l) {
int ver = l / B;
long long ret = res[ver];
int cl = ver * B;
assert(l - cl < B);
while (cl < l) {
rem(ver, cl, ret);
cl++;
}
return ret;
}
void cleanup() {
for (int v : clean) {
cnt2[v] = curpre[v] = curnxt[v] = 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> q;
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
for (int j = 1; j < LOG; ++j) {
for (int i = 0; i + (1 << j) <= idd; ++i) {
int u = spa[j - 1][i], v = spa[j - 1][i + (1 << (j - 1))];
spa[j][i] = (dep[u] < dep[v] ? u : v);
}
}
for (int i = 0; i < m; ++i) {
cin >> c[i];
}
preprocess();
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
qs[r].push_back({l, i});
}
for (int r = m - 1; r >= 0; --r) {
for (auto [ql, qi] : qs[r]) {
ans[qi] = (qry(ql) + 2) / 2;
cleanup();
}
back(r);
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5980 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
6 ms |
6236 KB |
Output is correct |
8 |
Correct |
4 ms |
5208 KB |
Output is correct |
9 |
Correct |
9 ms |
5468 KB |
Output is correct |
10 |
Correct |
15 ms |
5468 KB |
Output is correct |
11 |
Correct |
9 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5364 KB |
Output is correct |
14 |
Correct |
4 ms |
5184 KB |
Output is correct |
15 |
Correct |
9 ms |
5464 KB |
Output is correct |
16 |
Correct |
15 ms |
5612 KB |
Output is correct |
17 |
Correct |
14 ms |
5468 KB |
Output is correct |
18 |
Correct |
11 ms |
5464 KB |
Output is correct |
19 |
Correct |
13 ms |
5468 KB |
Output is correct |
20 |
Correct |
8 ms |
5592 KB |
Output is correct |
21 |
Correct |
8 ms |
5468 KB |
Output is correct |
22 |
Correct |
8 ms |
5548 KB |
Output is correct |
23 |
Correct |
9 ms |
5584 KB |
Output is correct |
24 |
Correct |
9 ms |
5468 KB |
Output is correct |
25 |
Correct |
9 ms |
5468 KB |
Output is correct |
26 |
Correct |
8 ms |
5468 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5192 KB |
Output is correct |
29 |
Correct |
10 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5980 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
6 ms |
6236 KB |
Output is correct |
8 |
Correct |
4 ms |
5208 KB |
Output is correct |
9 |
Correct |
9 ms |
5468 KB |
Output is correct |
10 |
Correct |
15 ms |
5468 KB |
Output is correct |
11 |
Correct |
9 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5364 KB |
Output is correct |
14 |
Correct |
4 ms |
5184 KB |
Output is correct |
15 |
Correct |
9 ms |
5464 KB |
Output is correct |
16 |
Correct |
15 ms |
5612 KB |
Output is correct |
17 |
Correct |
14 ms |
5468 KB |
Output is correct |
18 |
Correct |
11 ms |
5464 KB |
Output is correct |
19 |
Correct |
13 ms |
5468 KB |
Output is correct |
20 |
Correct |
8 ms |
5592 KB |
Output is correct |
21 |
Correct |
8 ms |
5468 KB |
Output is correct |
22 |
Correct |
8 ms |
5548 KB |
Output is correct |
23 |
Correct |
9 ms |
5584 KB |
Output is correct |
24 |
Correct |
9 ms |
5468 KB |
Output is correct |
25 |
Correct |
9 ms |
5468 KB |
Output is correct |
26 |
Correct |
8 ms |
5468 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5192 KB |
Output is correct |
29 |
Correct |
10 ms |
5468 KB |
Output is correct |
30 |
Correct |
235 ms |
7796 KB |
Output is correct |
31 |
Correct |
414 ms |
8652 KB |
Output is correct |
32 |
Correct |
593 ms |
8068 KB |
Output is correct |
33 |
Correct |
598 ms |
8028 KB |
Output is correct |
34 |
Correct |
638 ms |
7884 KB |
Output is correct |
35 |
Correct |
86 ms |
7276 KB |
Output is correct |
36 |
Correct |
93 ms |
7120 KB |
Output is correct |
37 |
Correct |
87 ms |
7244 KB |
Output is correct |
38 |
Correct |
614 ms |
8776 KB |
Output is correct |
39 |
Correct |
543 ms |
8904 KB |
Output is correct |
40 |
Correct |
543 ms |
8872 KB |
Output is correct |
41 |
Correct |
80 ms |
7396 KB |
Output is correct |
42 |
Correct |
83 ms |
7372 KB |
Output is correct |
43 |
Correct |
97 ms |
7332 KB |
Output is correct |
44 |
Correct |
528 ms |
8920 KB |
Output is correct |
45 |
Correct |
546 ms |
10940 KB |
Output is correct |
46 |
Correct |
532 ms |
8936 KB |
Output is correct |
47 |
Correct |
79 ms |
7004 KB |
Output is correct |
48 |
Correct |
88 ms |
6480 KB |
Output is correct |
49 |
Correct |
88 ms |
6352 KB |
Output is correct |
50 |
Correct |
590 ms |
7880 KB |
Output is correct |
51 |
Correct |
499 ms |
8816 KB |
Output is correct |
52 |
Correct |
515 ms |
8648 KB |
Output is correct |
53 |
Correct |
503 ms |
8692 KB |
Output is correct |
54 |
Correct |
523 ms |
8656 KB |
Output is correct |
55 |
Correct |
572 ms |
8644 KB |
Output is correct |
56 |
Correct |
6 ms |
5976 KB |
Output is correct |
57 |
Correct |
3 ms |
6236 KB |
Output is correct |
58 |
Correct |
342 ms |
8748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
8 ms |
5404 KB |
Output is correct |
4 |
Execution timed out |
5096 ms |
215860 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5024 KB |
Output is correct |
2 |
Correct |
969 ms |
131684 KB |
Output is correct |
3 |
Correct |
1674 ms |
219096 KB |
Output is correct |
4 |
Correct |
1300 ms |
191336 KB |
Output is correct |
5 |
Correct |
1340 ms |
392764 KB |
Output is correct |
6 |
Correct |
1294 ms |
394168 KB |
Output is correct |
7 |
Correct |
1189 ms |
394300 KB |
Output is correct |
8 |
Correct |
1196 ms |
394240 KB |
Output is correct |
9 |
Correct |
1191 ms |
393916 KB |
Output is correct |
10 |
Correct |
1237 ms |
394956 KB |
Output is correct |
11 |
Correct |
1480 ms |
395448 KB |
Output is correct |
12 |
Correct |
2778 ms |
398592 KB |
Output is correct |
13 |
Execution timed out |
5054 ms |
398592 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
7 ms |
5212 KB |
Output is correct |
4 |
Execution timed out |
5087 ms |
217616 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5980 KB |
Output is correct |
5 |
Correct |
4 ms |
5980 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
6 ms |
6236 KB |
Output is correct |
8 |
Correct |
4 ms |
5208 KB |
Output is correct |
9 |
Correct |
9 ms |
5468 KB |
Output is correct |
10 |
Correct |
15 ms |
5468 KB |
Output is correct |
11 |
Correct |
9 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5364 KB |
Output is correct |
14 |
Correct |
4 ms |
5184 KB |
Output is correct |
15 |
Correct |
9 ms |
5464 KB |
Output is correct |
16 |
Correct |
15 ms |
5612 KB |
Output is correct |
17 |
Correct |
14 ms |
5468 KB |
Output is correct |
18 |
Correct |
11 ms |
5464 KB |
Output is correct |
19 |
Correct |
13 ms |
5468 KB |
Output is correct |
20 |
Correct |
8 ms |
5592 KB |
Output is correct |
21 |
Correct |
8 ms |
5468 KB |
Output is correct |
22 |
Correct |
8 ms |
5548 KB |
Output is correct |
23 |
Correct |
9 ms |
5584 KB |
Output is correct |
24 |
Correct |
9 ms |
5468 KB |
Output is correct |
25 |
Correct |
9 ms |
5468 KB |
Output is correct |
26 |
Correct |
8 ms |
5468 KB |
Output is correct |
27 |
Correct |
3 ms |
5208 KB |
Output is correct |
28 |
Correct |
4 ms |
5192 KB |
Output is correct |
29 |
Correct |
10 ms |
5468 KB |
Output is correct |
30 |
Correct |
235 ms |
7796 KB |
Output is correct |
31 |
Correct |
414 ms |
8652 KB |
Output is correct |
32 |
Correct |
593 ms |
8068 KB |
Output is correct |
33 |
Correct |
598 ms |
8028 KB |
Output is correct |
34 |
Correct |
638 ms |
7884 KB |
Output is correct |
35 |
Correct |
86 ms |
7276 KB |
Output is correct |
36 |
Correct |
93 ms |
7120 KB |
Output is correct |
37 |
Correct |
87 ms |
7244 KB |
Output is correct |
38 |
Correct |
614 ms |
8776 KB |
Output is correct |
39 |
Correct |
543 ms |
8904 KB |
Output is correct |
40 |
Correct |
543 ms |
8872 KB |
Output is correct |
41 |
Correct |
80 ms |
7396 KB |
Output is correct |
42 |
Correct |
83 ms |
7372 KB |
Output is correct |
43 |
Correct |
97 ms |
7332 KB |
Output is correct |
44 |
Correct |
528 ms |
8920 KB |
Output is correct |
45 |
Correct |
546 ms |
10940 KB |
Output is correct |
46 |
Correct |
532 ms |
8936 KB |
Output is correct |
47 |
Correct |
79 ms |
7004 KB |
Output is correct |
48 |
Correct |
88 ms |
6480 KB |
Output is correct |
49 |
Correct |
88 ms |
6352 KB |
Output is correct |
50 |
Correct |
590 ms |
7880 KB |
Output is correct |
51 |
Correct |
499 ms |
8816 KB |
Output is correct |
52 |
Correct |
515 ms |
8648 KB |
Output is correct |
53 |
Correct |
503 ms |
8692 KB |
Output is correct |
54 |
Correct |
523 ms |
8656 KB |
Output is correct |
55 |
Correct |
572 ms |
8644 KB |
Output is correct |
56 |
Correct |
6 ms |
5976 KB |
Output is correct |
57 |
Correct |
3 ms |
6236 KB |
Output is correct |
58 |
Correct |
342 ms |
8748 KB |
Output is correct |
59 |
Correct |
2 ms |
5980 KB |
Output is correct |
60 |
Correct |
2 ms |
5980 KB |
Output is correct |
61 |
Correct |
8 ms |
5404 KB |
Output is correct |
62 |
Execution timed out |
5096 ms |
215860 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |