//Billions of bilious blue blistering barnacles in a thundering typhoon!
//Yesterday is history, tomorrow is a mystery, today is a gift of God, which is why we call it the present.
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> adj(n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v), adj[v].push_back(u);
}
vector<int> c(m + 1);
for (int i = 1; i <= m; i++) {
cin >> c[i];
}
vector<vector<pair<int, int>>> qu(m + 1);
vector<int> ans(q + 1);
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
if (l == r) ans[i] = 1;
else qu[r].push_back({l, i});
}
vector<int> csz(n + 1), top(n + 1), pr(n + 1), nxt(n + 1), sz(n + 1), dep(n + 1), chain(n + 1), num(n + 1);
function<void(int, int)> dfs = [&](int v, int par) {
pr[v] = par;
nxt[v] = 0;
sz[v] = 1;
for (int u: adj[v]) {
if (u == par) continue;
dep[u] = dep[v] + 1;
dfs(u, v);
if (sz[u] > sz[nxt[v]]) nxt[v] = u;
sz[v] += sz[u];
}
};
dfs(1, 0);
int cnt = 1, all = 0;
function<void(int, int)> hld = [&](int v, int par) {
chain[v] = cnt;
num[v] = ++all;
if (!csz[cnt]) top[cnt] = v;
csz[cnt]++;
if (nxt[v]) hld(nxt[v], v);
for (int u: adj[v]) {
if (u == par || u == nxt[v]) continue;
cnt++;
hld(u, v);
}
};
hld(1, 0);
set<array<int, 3>> se;
se.insert({1, n, 1});
vector<int> bit(m + 1);
auto update = [&](int i, int val) {
while (i <= m) {
bit[i] += val;
i += i & -i;
}
};
update(1, n);
auto query = [&](int l, int r) {
int ret = 0;
while (r > 0) {
ret += bit[r];
r -= r & -r;
}
l--;
while (l > 0) {
ret -= bit[l];
l -= l & -l;
}
return ret;
};
auto change = [&](int l, int r, int val) {
auto cur = se.lower_bound({l, l, 0});
if (cur != se.begin()) cur--;
vector<array<int, 3>> add, rem;
add.push_back({l, r, val});
while (cur != se.end() && (*cur)[0] <= r) {
auto [l1, r1, v1] = *cur;
if (r1 < l) {
cur++;
continue;
}
rem.push_back(*cur);
if (l1 < l) {
add.push_back({l1, l - 1, v1});
}
if (r1 > r) {
add.push_back({r + 1, r1, v1});
}
cur++;
}
for (auto b: rem) {
se.erase(b);
auto [l1, r1, v1] = b;
update(v1, -(r1 - l1 + 1));
}
for (auto b: add) {
se.insert(b);
auto [l1, r1, v1] = b;
update(v1, (r1 - l1 + 1));
}
};
function<void(int, int, int)> upd = [&](int u, int v, int val) {
while (chain[u] != chain[v]) {
if (dep[top[chain[u]]] < dep[top[chain[v]]]) swap(u, v);
int start = top[chain[u]];
change(num[start], num[u], val);
u = pr[start];
}
if (dep[u] > dep[v]) swap(u, v);
change(num[u], num[v], val);
};
for (int i = 1; i <= m; i++) {
if (i > 1) {
upd(c[i - 1], c[i], i);
}
for (auto [l, j]: qu[i]) {
ans[j] = query(l + 1, i);
}
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << "\n";
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
376 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
376 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
5 ms |
636 KB |
Output is correct |
32 |
Correct |
6 ms |
860 KB |
Output is correct |
33 |
Correct |
6 ms |
860 KB |
Output is correct |
34 |
Correct |
7 ms |
860 KB |
Output is correct |
35 |
Correct |
6 ms |
832 KB |
Output is correct |
36 |
Correct |
6 ms |
856 KB |
Output is correct |
37 |
Correct |
6 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
860 KB |
Output is correct |
39 |
Correct |
3 ms |
856 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
856 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
4 ms |
856 KB |
Output is correct |
45 |
Correct |
4 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
856 KB |
Output is correct |
47 |
Correct |
3 ms |
604 KB |
Output is correct |
48 |
Correct |
3 ms |
604 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
51 |
Correct |
3 ms |
856 KB |
Output is correct |
52 |
Correct |
3 ms |
856 KB |
Output is correct |
53 |
Correct |
3 ms |
856 KB |
Output is correct |
54 |
Correct |
4 ms |
1112 KB |
Output is correct |
55 |
Correct |
3 ms |
896 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
5 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
93 ms |
18368 KB |
Output is correct |
5 |
Correct |
70 ms |
21844 KB |
Output is correct |
6 |
Correct |
90 ms |
24912 KB |
Output is correct |
7 |
Correct |
111 ms |
28832 KB |
Output is correct |
8 |
Correct |
124 ms |
28976 KB |
Output is correct |
9 |
Correct |
118 ms |
29008 KB |
Output is correct |
10 |
Correct |
114 ms |
28900 KB |
Output is correct |
11 |
Correct |
114 ms |
29012 KB |
Output is correct |
12 |
Correct |
112 ms |
28244 KB |
Output is correct |
13 |
Correct |
107 ms |
28500 KB |
Output is correct |
14 |
Correct |
108 ms |
28496 KB |
Output is correct |
15 |
Correct |
38 ms |
22100 KB |
Output is correct |
16 |
Correct |
96 ms |
26448 KB |
Output is correct |
17 |
Correct |
37 ms |
7320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
253 ms |
10044 KB |
Output is correct |
3 |
Correct |
425 ms |
12244 KB |
Output is correct |
4 |
Correct |
323 ms |
12368 KB |
Output is correct |
5 |
Correct |
520 ms |
18260 KB |
Output is correct |
6 |
Correct |
524 ms |
18004 KB |
Output is correct |
7 |
Correct |
511 ms |
18256 KB |
Output is correct |
8 |
Correct |
517 ms |
18252 KB |
Output is correct |
9 |
Correct |
534 ms |
18276 KB |
Output is correct |
10 |
Correct |
503 ms |
18260 KB |
Output is correct |
11 |
Correct |
530 ms |
18260 KB |
Output is correct |
12 |
Correct |
513 ms |
18328 KB |
Output is correct |
13 |
Correct |
513 ms |
18516 KB |
Output is correct |
14 |
Correct |
522 ms |
19540 KB |
Output is correct |
15 |
Correct |
579 ms |
19908 KB |
Output is correct |
16 |
Correct |
496 ms |
18512 KB |
Output is correct |
17 |
Correct |
523 ms |
18516 KB |
Output is correct |
18 |
Correct |
513 ms |
18692 KB |
Output is correct |
19 |
Correct |
316 ms |
14340 KB |
Output is correct |
20 |
Correct |
294 ms |
14164 KB |
Output is correct |
21 |
Correct |
305 ms |
14376 KB |
Output is correct |
22 |
Correct |
300 ms |
14252 KB |
Output is correct |
23 |
Correct |
290 ms |
14420 KB |
Output is correct |
24 |
Correct |
304 ms |
14396 KB |
Output is correct |
25 |
Correct |
304 ms |
14216 KB |
Output is correct |
26 |
Correct |
303 ms |
14416 KB |
Output is correct |
27 |
Correct |
312 ms |
14456 KB |
Output is correct |
28 |
Correct |
310 ms |
14172 KB |
Output is correct |
29 |
Correct |
310 ms |
14392 KB |
Output is correct |
30 |
Correct |
306 ms |
14672 KB |
Output is correct |
31 |
Correct |
311 ms |
14764 KB |
Output is correct |
32 |
Correct |
315 ms |
15188 KB |
Output is correct |
33 |
Correct |
330 ms |
16208 KB |
Output is correct |
34 |
Correct |
308 ms |
16208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
667 ms |
15880 KB |
Output is correct |
5 |
Correct |
699 ms |
16084 KB |
Output is correct |
6 |
Correct |
825 ms |
20308 KB |
Output is correct |
7 |
Correct |
856 ms |
22732 KB |
Output is correct |
8 |
Correct |
863 ms |
22592 KB |
Output is correct |
9 |
Correct |
835 ms |
22608 KB |
Output is correct |
10 |
Correct |
823 ms |
22612 KB |
Output is correct |
11 |
Correct |
828 ms |
22864 KB |
Output is correct |
12 |
Correct |
809 ms |
22636 KB |
Output is correct |
13 |
Correct |
826 ms |
22664 KB |
Output is correct |
14 |
Correct |
38 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
376 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
5 ms |
636 KB |
Output is correct |
32 |
Correct |
6 ms |
860 KB |
Output is correct |
33 |
Correct |
6 ms |
860 KB |
Output is correct |
34 |
Correct |
7 ms |
860 KB |
Output is correct |
35 |
Correct |
6 ms |
832 KB |
Output is correct |
36 |
Correct |
6 ms |
856 KB |
Output is correct |
37 |
Correct |
6 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
860 KB |
Output is correct |
39 |
Correct |
3 ms |
856 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
856 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
4 ms |
856 KB |
Output is correct |
45 |
Correct |
4 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
856 KB |
Output is correct |
47 |
Correct |
3 ms |
604 KB |
Output is correct |
48 |
Correct |
3 ms |
604 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
51 |
Correct |
3 ms |
856 KB |
Output is correct |
52 |
Correct |
3 ms |
856 KB |
Output is correct |
53 |
Correct |
3 ms |
856 KB |
Output is correct |
54 |
Correct |
4 ms |
1112 KB |
Output is correct |
55 |
Correct |
3 ms |
896 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
5 ms |
612 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
93 ms |
18368 KB |
Output is correct |
63 |
Correct |
70 ms |
21844 KB |
Output is correct |
64 |
Correct |
90 ms |
24912 KB |
Output is correct |
65 |
Correct |
111 ms |
28832 KB |
Output is correct |
66 |
Correct |
124 ms |
28976 KB |
Output is correct |
67 |
Correct |
118 ms |
29008 KB |
Output is correct |
68 |
Correct |
114 ms |
28900 KB |
Output is correct |
69 |
Correct |
114 ms |
29012 KB |
Output is correct |
70 |
Correct |
112 ms |
28244 KB |
Output is correct |
71 |
Correct |
107 ms |
28500 KB |
Output is correct |
72 |
Correct |
108 ms |
28496 KB |
Output is correct |
73 |
Correct |
38 ms |
22100 KB |
Output is correct |
74 |
Correct |
96 ms |
26448 KB |
Output is correct |
75 |
Correct |
37 ms |
7320 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
253 ms |
10044 KB |
Output is correct |
78 |
Correct |
425 ms |
12244 KB |
Output is correct |
79 |
Correct |
323 ms |
12368 KB |
Output is correct |
80 |
Correct |
520 ms |
18260 KB |
Output is correct |
81 |
Correct |
524 ms |
18004 KB |
Output is correct |
82 |
Correct |
511 ms |
18256 KB |
Output is correct |
83 |
Correct |
517 ms |
18252 KB |
Output is correct |
84 |
Correct |
534 ms |
18276 KB |
Output is correct |
85 |
Correct |
503 ms |
18260 KB |
Output is correct |
86 |
Correct |
530 ms |
18260 KB |
Output is correct |
87 |
Correct |
513 ms |
18328 KB |
Output is correct |
88 |
Correct |
513 ms |
18516 KB |
Output is correct |
89 |
Correct |
522 ms |
19540 KB |
Output is correct |
90 |
Correct |
579 ms |
19908 KB |
Output is correct |
91 |
Correct |
496 ms |
18512 KB |
Output is correct |
92 |
Correct |
523 ms |
18516 KB |
Output is correct |
93 |
Correct |
513 ms |
18692 KB |
Output is correct |
94 |
Correct |
316 ms |
14340 KB |
Output is correct |
95 |
Correct |
294 ms |
14164 KB |
Output is correct |
96 |
Correct |
305 ms |
14376 KB |
Output is correct |
97 |
Correct |
300 ms |
14252 KB |
Output is correct |
98 |
Correct |
290 ms |
14420 KB |
Output is correct |
99 |
Correct |
304 ms |
14396 KB |
Output is correct |
100 |
Correct |
304 ms |
14216 KB |
Output is correct |
101 |
Correct |
303 ms |
14416 KB |
Output is correct |
102 |
Correct |
312 ms |
14456 KB |
Output is correct |
103 |
Correct |
310 ms |
14172 KB |
Output is correct |
104 |
Correct |
310 ms |
14392 KB |
Output is correct |
105 |
Correct |
306 ms |
14672 KB |
Output is correct |
106 |
Correct |
311 ms |
14764 KB |
Output is correct |
107 |
Correct |
315 ms |
15188 KB |
Output is correct |
108 |
Correct |
330 ms |
16208 KB |
Output is correct |
109 |
Correct |
308 ms |
16208 KB |
Output is correct |
110 |
Correct |
0 ms |
344 KB |
Output is correct |
111 |
Correct |
0 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
348 KB |
Output is correct |
113 |
Correct |
667 ms |
15880 KB |
Output is correct |
114 |
Correct |
699 ms |
16084 KB |
Output is correct |
115 |
Correct |
825 ms |
20308 KB |
Output is correct |
116 |
Correct |
856 ms |
22732 KB |
Output is correct |
117 |
Correct |
863 ms |
22592 KB |
Output is correct |
118 |
Correct |
835 ms |
22608 KB |
Output is correct |
119 |
Correct |
823 ms |
22612 KB |
Output is correct |
120 |
Correct |
828 ms |
22864 KB |
Output is correct |
121 |
Correct |
809 ms |
22636 KB |
Output is correct |
122 |
Correct |
826 ms |
22664 KB |
Output is correct |
123 |
Correct |
38 ms |
7516 KB |
Output is correct |
124 |
Correct |
455 ms |
21332 KB |
Output is correct |
125 |
Correct |
359 ms |
18348 KB |
Output is correct |
126 |
Correct |
532 ms |
22184 KB |
Output is correct |
127 |
Correct |
560 ms |
22188 KB |
Output is correct |
128 |
Correct |
545 ms |
22356 KB |
Output is correct |
129 |
Correct |
529 ms |
22352 KB |
Output is correct |
130 |
Correct |
528 ms |
22296 KB |
Output is correct |
131 |
Correct |
113 ms |
27192 KB |
Output is correct |
132 |
Correct |
111 ms |
28804 KB |
Output is correct |
133 |
Correct |
121 ms |
23376 KB |
Output is correct |
134 |
Correct |
330 ms |
18520 KB |
Output is correct |
135 |
Correct |
335 ms |
18468 KB |
Output is correct |
136 |
Correct |
331 ms |
18356 KB |
Output is correct |
137 |
Correct |
219 ms |
23756 KB |
Output is correct |
138 |
Correct |
203 ms |
23752 KB |
Output is correct |
139 |
Correct |
208 ms |
23668 KB |
Output is correct |
140 |
Correct |
201 ms |
23752 KB |
Output is correct |
141 |
Correct |
204 ms |
23760 KB |
Output is correct |
142 |
Correct |
207 ms |
23772 KB |
Output is correct |
143 |
Correct |
43 ms |
11200 KB |
Output is correct |
144 |
Correct |
549 ms |
19776 KB |
Output is correct |