#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 25;
int in[MAXN], out[MAXN], depth[MAXN];
pair <int, int> sparse[__lg(MAXN)][MAXN];
vector <int> adj[MAXN];
int n, m, q;
int cnt = 0;
void dfs (int pos, int par, int dep = 1) {
depth[pos] = dep;
sparse[0][++cnt] = {dep, pos};
assert(cnt < MAXN);
in[pos] = cnt;
for (auto j : adj[pos]) {
if (j != par) {
dfs(j, pos, dep + 1);
sparse[0][++cnt] = {dep, pos};
assert(cnt < MAXN);
}
}
out[pos] = dep;
}
int query (int l, int r) {
if (l > r) swap(l, r);
if (l == r) return sparse[0][l].second;
int x = __lg(r - l + 1);
return min(sparse[x][l], sparse[x][r - (1 << x) + 1]).second;
}
int arr[MAXN];
int main () {
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
for (int i = 1; i <= __lg(cnt); i++) {
for (int j = 1; j <= cnt; j++) {
sparse[i][j] = min(sparse[i - 1][j], sparse[i - 1][j + (1 << (i - 1))]);
}
}
for (int i = 1; i <= m; i++) cin >> arr[i];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
vector <int> g;
for (int j = l; j <= r; j++) g.push_back(arr[j]);
sort(g.begin(), g.end(), [&] (int a, int b) {
return in[a] < in[b];
});
int ans = depth[g[0]], lca = g[0];
for (int j = 1; j < (int)g.size(); j++) {
ans += depth[g[j]] - depth[query(in[g[j]], in[g[j - 1]])];
lca = query(in[lca], in[g[j]]);
}
ans -= depth[lca]; ans++;
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26968 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
29020 KB |
Output is correct |
4 |
Correct |
10 ms |
37212 KB |
Output is correct |
5 |
Correct |
7 ms |
37212 KB |
Output is correct |
6 |
Correct |
7 ms |
37352 KB |
Output is correct |
7 |
Correct |
7 ms |
37212 KB |
Output is correct |
8 |
Correct |
7 ms |
39260 KB |
Output is correct |
9 |
Correct |
8 ms |
39408 KB |
Output is correct |
10 |
Correct |
8 ms |
39260 KB |
Output is correct |
11 |
Correct |
8 ms |
39260 KB |
Output is correct |
12 |
Correct |
11 ms |
39260 KB |
Output is correct |
13 |
Correct |
10 ms |
39260 KB |
Output is correct |
14 |
Correct |
10 ms |
39360 KB |
Output is correct |
15 |
Correct |
9 ms |
39260 KB |
Output is correct |
16 |
Correct |
8 ms |
39260 KB |
Output is correct |
17 |
Correct |
8 ms |
39260 KB |
Output is correct |
18 |
Correct |
8 ms |
39260 KB |
Output is correct |
19 |
Correct |
8 ms |
39260 KB |
Output is correct |
20 |
Correct |
8 ms |
39256 KB |
Output is correct |
21 |
Correct |
8 ms |
39432 KB |
Output is correct |
22 |
Correct |
8 ms |
39304 KB |
Output is correct |
23 |
Correct |
8 ms |
39452 KB |
Output is correct |
24 |
Correct |
9 ms |
39260 KB |
Output is correct |
25 |
Correct |
8 ms |
39256 KB |
Output is correct |
26 |
Correct |
8 ms |
39256 KB |
Output is correct |
27 |
Correct |
4 ms |
20824 KB |
Output is correct |
28 |
Correct |
6 ms |
39260 KB |
Output is correct |
29 |
Correct |
6 ms |
39260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26968 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
29020 KB |
Output is correct |
4 |
Correct |
10 ms |
37212 KB |
Output is correct |
5 |
Correct |
7 ms |
37212 KB |
Output is correct |
6 |
Correct |
7 ms |
37352 KB |
Output is correct |
7 |
Correct |
7 ms |
37212 KB |
Output is correct |
8 |
Correct |
7 ms |
39260 KB |
Output is correct |
9 |
Correct |
8 ms |
39408 KB |
Output is correct |
10 |
Correct |
8 ms |
39260 KB |
Output is correct |
11 |
Correct |
8 ms |
39260 KB |
Output is correct |
12 |
Correct |
11 ms |
39260 KB |
Output is correct |
13 |
Correct |
10 ms |
39260 KB |
Output is correct |
14 |
Correct |
10 ms |
39360 KB |
Output is correct |
15 |
Correct |
9 ms |
39260 KB |
Output is correct |
16 |
Correct |
8 ms |
39260 KB |
Output is correct |
17 |
Correct |
8 ms |
39260 KB |
Output is correct |
18 |
Correct |
8 ms |
39260 KB |
Output is correct |
19 |
Correct |
8 ms |
39260 KB |
Output is correct |
20 |
Correct |
8 ms |
39256 KB |
Output is correct |
21 |
Correct |
8 ms |
39432 KB |
Output is correct |
22 |
Correct |
8 ms |
39304 KB |
Output is correct |
23 |
Correct |
8 ms |
39452 KB |
Output is correct |
24 |
Correct |
9 ms |
39260 KB |
Output is correct |
25 |
Correct |
8 ms |
39256 KB |
Output is correct |
26 |
Correct |
8 ms |
39256 KB |
Output is correct |
27 |
Correct |
4 ms |
20824 KB |
Output is correct |
28 |
Correct |
6 ms |
39260 KB |
Output is correct |
29 |
Correct |
6 ms |
39260 KB |
Output is correct |
30 |
Correct |
48 ms |
43352 KB |
Output is correct |
31 |
Correct |
67 ms |
43584 KB |
Output is correct |
32 |
Correct |
90 ms |
43612 KB |
Output is correct |
33 |
Correct |
90 ms |
43612 KB |
Output is correct |
34 |
Correct |
88 ms |
43800 KB |
Output is correct |
35 |
Correct |
261 ms |
43864 KB |
Output is correct |
36 |
Correct |
261 ms |
43612 KB |
Output is correct |
37 |
Correct |
255 ms |
43608 KB |
Output is correct |
38 |
Correct |
94 ms |
43712 KB |
Output is correct |
39 |
Correct |
90 ms |
43612 KB |
Output is correct |
40 |
Correct |
89 ms |
43608 KB |
Output is correct |
41 |
Correct |
260 ms |
43612 KB |
Output is correct |
42 |
Correct |
262 ms |
44116 KB |
Output is correct |
43 |
Correct |
264 ms |
43612 KB |
Output is correct |
44 |
Correct |
94 ms |
43660 KB |
Output is correct |
45 |
Correct |
94 ms |
43608 KB |
Output is correct |
46 |
Correct |
93 ms |
43608 KB |
Output is correct |
47 |
Correct |
261 ms |
43652 KB |
Output is correct |
48 |
Correct |
264 ms |
43648 KB |
Output is correct |
49 |
Correct |
259 ms |
43652 KB |
Output is correct |
50 |
Correct |
85 ms |
43612 KB |
Output is correct |
51 |
Correct |
88 ms |
43612 KB |
Output is correct |
52 |
Correct |
92 ms |
43620 KB |
Output is correct |
53 |
Correct |
84 ms |
43612 KB |
Output is correct |
54 |
Correct |
86 ms |
43608 KB |
Output is correct |
55 |
Correct |
85 ms |
43608 KB |
Output is correct |
56 |
Correct |
21 ms |
20828 KB |
Output is correct |
57 |
Correct |
10 ms |
43352 KB |
Output is correct |
58 |
Correct |
11 ms |
43356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
20940 KB |
Output is correct |
3 |
Correct |
21 ms |
20824 KB |
Output is correct |
4 |
Execution timed out |
5051 ms |
71324 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
57 ms |
65924 KB |
Output is correct |
3 |
Correct |
72 ms |
68116 KB |
Output is correct |
4 |
Correct |
78 ms |
72452 KB |
Output is correct |
5 |
Correct |
103 ms |
86276 KB |
Output is correct |
6 |
Correct |
103 ms |
85840 KB |
Output is correct |
7 |
Correct |
99 ms |
85620 KB |
Output is correct |
8 |
Correct |
105 ms |
85868 KB |
Output is correct |
9 |
Correct |
102 ms |
85588 KB |
Output is correct |
10 |
Correct |
123 ms |
85648 KB |
Output is correct |
11 |
Correct |
101 ms |
85588 KB |
Output is correct |
12 |
Correct |
121 ms |
85632 KB |
Output is correct |
13 |
Correct |
113 ms |
85688 KB |
Output is correct |
14 |
Correct |
148 ms |
85984 KB |
Output is correct |
15 |
Correct |
296 ms |
86868 KB |
Output is correct |
16 |
Correct |
116 ms |
86100 KB |
Output is correct |
17 |
Correct |
119 ms |
86136 KB |
Output is correct |
18 |
Correct |
117 ms |
86100 KB |
Output is correct |
19 |
Correct |
98 ms |
86112 KB |
Output is correct |
20 |
Correct |
97 ms |
86036 KB |
Output is correct |
21 |
Correct |
98 ms |
85832 KB |
Output is correct |
22 |
Correct |
97 ms |
85584 KB |
Output is correct |
23 |
Correct |
95 ms |
85500 KB |
Output is correct |
24 |
Correct |
95 ms |
85588 KB |
Output is correct |
25 |
Correct |
97 ms |
85644 KB |
Output is correct |
26 |
Correct |
97 ms |
85584 KB |
Output is correct |
27 |
Correct |
96 ms |
85580 KB |
Output is correct |
28 |
Correct |
106 ms |
85652 KB |
Output is correct |
29 |
Correct |
98 ms |
85588 KB |
Output is correct |
30 |
Correct |
104 ms |
85724 KB |
Output is correct |
31 |
Correct |
110 ms |
85840 KB |
Output is correct |
32 |
Correct |
135 ms |
85844 KB |
Output is correct |
33 |
Correct |
175 ms |
86284 KB |
Output is correct |
34 |
Correct |
250 ms |
87052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
26972 KB |
Output is correct |
2 |
Correct |
5 ms |
20932 KB |
Output is correct |
3 |
Correct |
23 ms |
20828 KB |
Output is correct |
4 |
Execution timed out |
5029 ms |
67272 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26968 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
5 ms |
29020 KB |
Output is correct |
4 |
Correct |
10 ms |
37212 KB |
Output is correct |
5 |
Correct |
7 ms |
37212 KB |
Output is correct |
6 |
Correct |
7 ms |
37352 KB |
Output is correct |
7 |
Correct |
7 ms |
37212 KB |
Output is correct |
8 |
Correct |
7 ms |
39260 KB |
Output is correct |
9 |
Correct |
8 ms |
39408 KB |
Output is correct |
10 |
Correct |
8 ms |
39260 KB |
Output is correct |
11 |
Correct |
8 ms |
39260 KB |
Output is correct |
12 |
Correct |
11 ms |
39260 KB |
Output is correct |
13 |
Correct |
10 ms |
39260 KB |
Output is correct |
14 |
Correct |
10 ms |
39360 KB |
Output is correct |
15 |
Correct |
9 ms |
39260 KB |
Output is correct |
16 |
Correct |
8 ms |
39260 KB |
Output is correct |
17 |
Correct |
8 ms |
39260 KB |
Output is correct |
18 |
Correct |
8 ms |
39260 KB |
Output is correct |
19 |
Correct |
8 ms |
39260 KB |
Output is correct |
20 |
Correct |
8 ms |
39256 KB |
Output is correct |
21 |
Correct |
8 ms |
39432 KB |
Output is correct |
22 |
Correct |
8 ms |
39304 KB |
Output is correct |
23 |
Correct |
8 ms |
39452 KB |
Output is correct |
24 |
Correct |
9 ms |
39260 KB |
Output is correct |
25 |
Correct |
8 ms |
39256 KB |
Output is correct |
26 |
Correct |
8 ms |
39256 KB |
Output is correct |
27 |
Correct |
4 ms |
20824 KB |
Output is correct |
28 |
Correct |
6 ms |
39260 KB |
Output is correct |
29 |
Correct |
6 ms |
39260 KB |
Output is correct |
30 |
Correct |
48 ms |
43352 KB |
Output is correct |
31 |
Correct |
67 ms |
43584 KB |
Output is correct |
32 |
Correct |
90 ms |
43612 KB |
Output is correct |
33 |
Correct |
90 ms |
43612 KB |
Output is correct |
34 |
Correct |
88 ms |
43800 KB |
Output is correct |
35 |
Correct |
261 ms |
43864 KB |
Output is correct |
36 |
Correct |
261 ms |
43612 KB |
Output is correct |
37 |
Correct |
255 ms |
43608 KB |
Output is correct |
38 |
Correct |
94 ms |
43712 KB |
Output is correct |
39 |
Correct |
90 ms |
43612 KB |
Output is correct |
40 |
Correct |
89 ms |
43608 KB |
Output is correct |
41 |
Correct |
260 ms |
43612 KB |
Output is correct |
42 |
Correct |
262 ms |
44116 KB |
Output is correct |
43 |
Correct |
264 ms |
43612 KB |
Output is correct |
44 |
Correct |
94 ms |
43660 KB |
Output is correct |
45 |
Correct |
94 ms |
43608 KB |
Output is correct |
46 |
Correct |
93 ms |
43608 KB |
Output is correct |
47 |
Correct |
261 ms |
43652 KB |
Output is correct |
48 |
Correct |
264 ms |
43648 KB |
Output is correct |
49 |
Correct |
259 ms |
43652 KB |
Output is correct |
50 |
Correct |
85 ms |
43612 KB |
Output is correct |
51 |
Correct |
88 ms |
43612 KB |
Output is correct |
52 |
Correct |
92 ms |
43620 KB |
Output is correct |
53 |
Correct |
84 ms |
43612 KB |
Output is correct |
54 |
Correct |
86 ms |
43608 KB |
Output is correct |
55 |
Correct |
85 ms |
43608 KB |
Output is correct |
56 |
Correct |
21 ms |
20828 KB |
Output is correct |
57 |
Correct |
10 ms |
43352 KB |
Output is correct |
58 |
Correct |
11 ms |
43356 KB |
Output is correct |
59 |
Correct |
5 ms |
26972 KB |
Output is correct |
60 |
Correct |
4 ms |
20940 KB |
Output is correct |
61 |
Correct |
21 ms |
20824 KB |
Output is correct |
62 |
Execution timed out |
5051 ms |
71324 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |