#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 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};
in[pos] = cnt;
for (auto j : adj[pos]) {
if (j != par) {
dfs(j, pos, dep + 1);
sparse[0][++cnt] = {dep, pos};
}
}
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 |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12748 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
22872 KB |
Output is correct |
9 |
Correct |
5 ms |
22876 KB |
Output is correct |
10 |
Correct |
5 ms |
22876 KB |
Output is correct |
11 |
Correct |
7 ms |
22872 KB |
Output is correct |
12 |
Correct |
8 ms |
22876 KB |
Output is correct |
13 |
Correct |
9 ms |
22876 KB |
Output is correct |
14 |
Correct |
8 ms |
22876 KB |
Output is correct |
15 |
Correct |
6 ms |
22876 KB |
Output is correct |
16 |
Correct |
6 ms |
22872 KB |
Output is correct |
17 |
Correct |
5 ms |
22996 KB |
Output is correct |
18 |
Correct |
5 ms |
22876 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
6 ms |
22984 KB |
Output is correct |
21 |
Correct |
6 ms |
22876 KB |
Output is correct |
22 |
Correct |
5 ms |
22964 KB |
Output is correct |
23 |
Correct |
5 ms |
23128 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22872 KB |
Output is correct |
26 |
Correct |
5 ms |
22952 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
4 ms |
22872 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12748 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
22872 KB |
Output is correct |
9 |
Correct |
5 ms |
22876 KB |
Output is correct |
10 |
Correct |
5 ms |
22876 KB |
Output is correct |
11 |
Correct |
7 ms |
22872 KB |
Output is correct |
12 |
Correct |
8 ms |
22876 KB |
Output is correct |
13 |
Correct |
9 ms |
22876 KB |
Output is correct |
14 |
Correct |
8 ms |
22876 KB |
Output is correct |
15 |
Correct |
6 ms |
22876 KB |
Output is correct |
16 |
Correct |
6 ms |
22872 KB |
Output is correct |
17 |
Correct |
5 ms |
22996 KB |
Output is correct |
18 |
Correct |
5 ms |
22876 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
6 ms |
22984 KB |
Output is correct |
21 |
Correct |
6 ms |
22876 KB |
Output is correct |
22 |
Correct |
5 ms |
22964 KB |
Output is correct |
23 |
Correct |
5 ms |
23128 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22872 KB |
Output is correct |
26 |
Correct |
5 ms |
22952 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
4 ms |
22872 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
43 ms |
25176 KB |
Output is correct |
31 |
Correct |
62 ms |
25432 KB |
Output is correct |
32 |
Correct |
87 ms |
25192 KB |
Output is correct |
33 |
Correct |
90 ms |
25176 KB |
Output is correct |
34 |
Correct |
85 ms |
25176 KB |
Output is correct |
35 |
Correct |
258 ms |
25176 KB |
Output is correct |
36 |
Correct |
256 ms |
25192 KB |
Output is correct |
37 |
Correct |
253 ms |
25180 KB |
Output is correct |
38 |
Correct |
89 ms |
25180 KB |
Output is correct |
39 |
Correct |
89 ms |
25180 KB |
Output is correct |
40 |
Correct |
87 ms |
25308 KB |
Output is correct |
41 |
Correct |
256 ms |
25328 KB |
Output is correct |
42 |
Correct |
260 ms |
25424 KB |
Output is correct |
43 |
Correct |
255 ms |
25292 KB |
Output is correct |
44 |
Correct |
86 ms |
25180 KB |
Output is correct |
45 |
Correct |
91 ms |
25180 KB |
Output is correct |
46 |
Correct |
90 ms |
25252 KB |
Output is correct |
47 |
Correct |
258 ms |
25180 KB |
Output is correct |
48 |
Correct |
261 ms |
25180 KB |
Output is correct |
49 |
Correct |
258 ms |
25240 KB |
Output is correct |
50 |
Correct |
82 ms |
25204 KB |
Output is correct |
51 |
Correct |
84 ms |
25200 KB |
Output is correct |
52 |
Correct |
84 ms |
25180 KB |
Output is correct |
53 |
Correct |
88 ms |
25180 KB |
Output is correct |
54 |
Correct |
83 ms |
25180 KB |
Output is correct |
55 |
Correct |
82 ms |
25176 KB |
Output is correct |
56 |
Correct |
19 ms |
8536 KB |
Output is correct |
57 |
Correct |
8 ms |
24924 KB |
Output is correct |
58 |
Correct |
8 ms |
25040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
8624 KB |
Output is correct |
3 |
Correct |
19 ms |
8540 KB |
Output is correct |
4 |
Execution timed out |
5067 ms |
42396 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
53 ms |
36308 KB |
Output is correct |
3 |
Correct |
66 ms |
36900 KB |
Output is correct |
4 |
Incorrect |
65 ms |
37204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
19 ms |
8540 KB |
Output is correct |
4 |
Execution timed out |
5085 ms |
37732 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12748 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
22872 KB |
Output is correct |
9 |
Correct |
5 ms |
22876 KB |
Output is correct |
10 |
Correct |
5 ms |
22876 KB |
Output is correct |
11 |
Correct |
7 ms |
22872 KB |
Output is correct |
12 |
Correct |
8 ms |
22876 KB |
Output is correct |
13 |
Correct |
9 ms |
22876 KB |
Output is correct |
14 |
Correct |
8 ms |
22876 KB |
Output is correct |
15 |
Correct |
6 ms |
22876 KB |
Output is correct |
16 |
Correct |
6 ms |
22872 KB |
Output is correct |
17 |
Correct |
5 ms |
22996 KB |
Output is correct |
18 |
Correct |
5 ms |
22876 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
6 ms |
22984 KB |
Output is correct |
21 |
Correct |
6 ms |
22876 KB |
Output is correct |
22 |
Correct |
5 ms |
22964 KB |
Output is correct |
23 |
Correct |
5 ms |
23128 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22872 KB |
Output is correct |
26 |
Correct |
5 ms |
22952 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
4 ms |
22872 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
43 ms |
25176 KB |
Output is correct |
31 |
Correct |
62 ms |
25432 KB |
Output is correct |
32 |
Correct |
87 ms |
25192 KB |
Output is correct |
33 |
Correct |
90 ms |
25176 KB |
Output is correct |
34 |
Correct |
85 ms |
25176 KB |
Output is correct |
35 |
Correct |
258 ms |
25176 KB |
Output is correct |
36 |
Correct |
256 ms |
25192 KB |
Output is correct |
37 |
Correct |
253 ms |
25180 KB |
Output is correct |
38 |
Correct |
89 ms |
25180 KB |
Output is correct |
39 |
Correct |
89 ms |
25180 KB |
Output is correct |
40 |
Correct |
87 ms |
25308 KB |
Output is correct |
41 |
Correct |
256 ms |
25328 KB |
Output is correct |
42 |
Correct |
260 ms |
25424 KB |
Output is correct |
43 |
Correct |
255 ms |
25292 KB |
Output is correct |
44 |
Correct |
86 ms |
25180 KB |
Output is correct |
45 |
Correct |
91 ms |
25180 KB |
Output is correct |
46 |
Correct |
90 ms |
25252 KB |
Output is correct |
47 |
Correct |
258 ms |
25180 KB |
Output is correct |
48 |
Correct |
261 ms |
25180 KB |
Output is correct |
49 |
Correct |
258 ms |
25240 KB |
Output is correct |
50 |
Correct |
82 ms |
25204 KB |
Output is correct |
51 |
Correct |
84 ms |
25200 KB |
Output is correct |
52 |
Correct |
84 ms |
25180 KB |
Output is correct |
53 |
Correct |
88 ms |
25180 KB |
Output is correct |
54 |
Correct |
83 ms |
25180 KB |
Output is correct |
55 |
Correct |
82 ms |
25176 KB |
Output is correct |
56 |
Correct |
19 ms |
8536 KB |
Output is correct |
57 |
Correct |
8 ms |
24924 KB |
Output is correct |
58 |
Correct |
8 ms |
25040 KB |
Output is correct |
59 |
Correct |
2 ms |
12636 KB |
Output is correct |
60 |
Correct |
2 ms |
8624 KB |
Output is correct |
61 |
Correct |
19 ms |
8540 KB |
Output is correct |
62 |
Execution timed out |
5067 ms |
42396 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |