#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100'005;
vector<int> adj[N];
int h[N], st[N], en[N], t, anc[17][N];
void dfs(int i, int p, int d) {
anc[0][i] = p;
for (int j = 1; j < 17; j++) {
anc[j][i] = anc[j - 1][anc[j - 1][i]];
}
h[i] = d;
st[i] = t++;
for (auto nxt : adj[i]) {
if (nxt == p) continue;
dfs(nxt, i, d + 1);
}
en[i] = t - 1;
}
int lca(int u, int v) {
if (st[u] > st[v]) swap(u, v);
if (st[u] <= st[v] && en[v] <= en[u]) {
return u;
}
for (int j = 16; j >= 0; j--) {
if (!(st[anc[j][u]] <= st[v] && en[v] <= en[anc[j][u]])) {
u = anc[j][u];
}
}
return anc[0][u];
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1, 1);
int a[m];
for (int i = 0; i < m; i++) {
cin >> a[i];
}
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
vector<int> cur;
for (int i = l; i <= r; i++) {
cur.push_back(a[i]);
}
sort(cur.begin(), cur.end(), [&] (int x, int y) {return st[x] < st[y];});
int ans = h[cur[0]] - h[lca(cur[0], cur.back())] + 1;
for (int i = 1; i < cur.size(); i++) {
ans += h[cur[i]] - h[lca(cur[i], cur[i - 1])];
}
cout << ans << '\n';
}
return 0;
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:57:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 1; i < cur.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10444 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
4 ms |
10332 KB |
Output is correct |
10 |
Correct |
4 ms |
10332 KB |
Output is correct |
11 |
Correct |
4 ms |
10332 KB |
Output is correct |
12 |
Correct |
7 ms |
10448 KB |
Output is correct |
13 |
Correct |
7 ms |
10332 KB |
Output is correct |
14 |
Correct |
9 ms |
10968 KB |
Output is correct |
15 |
Correct |
3 ms |
10500 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
3 ms |
10332 KB |
Output is correct |
18 |
Correct |
3 ms |
10332 KB |
Output is correct |
19 |
Correct |
3 ms |
10332 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
21 |
Correct |
5 ms |
10332 KB |
Output is correct |
22 |
Correct |
4 ms |
10476 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
5 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10332 KB |
Output is correct |
27 |
Correct |
2 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10468 KB |
Output is correct |
29 |
Correct |
2 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10444 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
4 ms |
10332 KB |
Output is correct |
10 |
Correct |
4 ms |
10332 KB |
Output is correct |
11 |
Correct |
4 ms |
10332 KB |
Output is correct |
12 |
Correct |
7 ms |
10448 KB |
Output is correct |
13 |
Correct |
7 ms |
10332 KB |
Output is correct |
14 |
Correct |
9 ms |
10968 KB |
Output is correct |
15 |
Correct |
3 ms |
10500 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
3 ms |
10332 KB |
Output is correct |
18 |
Correct |
3 ms |
10332 KB |
Output is correct |
19 |
Correct |
3 ms |
10332 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
21 |
Correct |
5 ms |
10332 KB |
Output is correct |
22 |
Correct |
4 ms |
10476 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
5 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10332 KB |
Output is correct |
27 |
Correct |
2 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10468 KB |
Output is correct |
29 |
Correct |
2 ms |
10332 KB |
Output is correct |
30 |
Correct |
58 ms |
10332 KB |
Output is correct |
31 |
Correct |
83 ms |
10544 KB |
Output is correct |
32 |
Correct |
121 ms |
10568 KB |
Output is correct |
33 |
Correct |
120 ms |
10580 KB |
Output is correct |
34 |
Correct |
125 ms |
10580 KB |
Output is correct |
35 |
Correct |
329 ms |
10568 KB |
Output is correct |
36 |
Correct |
328 ms |
10588 KB |
Output is correct |
37 |
Correct |
325 ms |
10588 KB |
Output is correct |
38 |
Correct |
70 ms |
10452 KB |
Output is correct |
39 |
Correct |
69 ms |
10656 KB |
Output is correct |
40 |
Correct |
70 ms |
10664 KB |
Output is correct |
41 |
Correct |
218 ms |
10672 KB |
Output is correct |
42 |
Correct |
219 ms |
10588 KB |
Output is correct |
43 |
Correct |
224 ms |
10644 KB |
Output is correct |
44 |
Correct |
70 ms |
10612 KB |
Output is correct |
45 |
Correct |
73 ms |
10588 KB |
Output is correct |
46 |
Correct |
72 ms |
10588 KB |
Output is correct |
47 |
Correct |
225 ms |
10836 KB |
Output is correct |
48 |
Correct |
223 ms |
10604 KB |
Output is correct |
49 |
Correct |
222 ms |
10588 KB |
Output is correct |
50 |
Correct |
144 ms |
10588 KB |
Output is correct |
51 |
Correct |
141 ms |
10588 KB |
Output is correct |
52 |
Correct |
144 ms |
10588 KB |
Output is correct |
53 |
Correct |
137 ms |
10588 KB |
Output is correct |
54 |
Correct |
139 ms |
10612 KB |
Output is correct |
55 |
Correct |
138 ms |
10588 KB |
Output is correct |
56 |
Correct |
14 ms |
10332 KB |
Output is correct |
57 |
Correct |
3 ms |
10332 KB |
Output is correct |
58 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
14 ms |
10500 KB |
Output is correct |
4 |
Execution timed out |
5050 ms |
18412 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
29 ms |
13548 KB |
Output is correct |
3 |
Correct |
40 ms |
13916 KB |
Output is correct |
4 |
Correct |
36 ms |
14028 KB |
Output is correct |
5 |
Correct |
51 ms |
16592 KB |
Output is correct |
6 |
Correct |
59 ms |
16212 KB |
Output is correct |
7 |
Correct |
65 ms |
16076 KB |
Output is correct |
8 |
Correct |
56 ms |
15956 KB |
Output is correct |
9 |
Correct |
60 ms |
15768 KB |
Output is correct |
10 |
Correct |
58 ms |
15964 KB |
Output is correct |
11 |
Correct |
55 ms |
15772 KB |
Output is correct |
12 |
Correct |
57 ms |
15956 KB |
Output is correct |
13 |
Correct |
68 ms |
16088 KB |
Output is correct |
14 |
Correct |
60 ms |
16320 KB |
Output is correct |
15 |
Correct |
72 ms |
17292 KB |
Output is correct |
16 |
Correct |
57 ms |
16432 KB |
Output is correct |
17 |
Correct |
55 ms |
16348 KB |
Output is correct |
18 |
Correct |
65 ms |
16648 KB |
Output is correct |
19 |
Correct |
44 ms |
16424 KB |
Output is correct |
20 |
Correct |
47 ms |
16176 KB |
Output is correct |
21 |
Correct |
44 ms |
16064 KB |
Output is correct |
22 |
Correct |
50 ms |
15956 KB |
Output is correct |
23 |
Correct |
48 ms |
15784 KB |
Output is correct |
24 |
Correct |
51 ms |
15880 KB |
Output is correct |
25 |
Correct |
56 ms |
15708 KB |
Output is correct |
26 |
Correct |
51 ms |
15696 KB |
Output is correct |
27 |
Correct |
52 ms |
15708 KB |
Output is correct |
28 |
Correct |
53 ms |
15708 KB |
Output is correct |
29 |
Correct |
69 ms |
15972 KB |
Output is correct |
30 |
Correct |
55 ms |
15700 KB |
Output is correct |
31 |
Correct |
56 ms |
15956 KB |
Output is correct |
32 |
Correct |
60 ms |
16036 KB |
Output is correct |
33 |
Correct |
63 ms |
16484 KB |
Output is correct |
34 |
Correct |
67 ms |
17264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
14 ms |
10332 KB |
Output is correct |
4 |
Execution timed out |
5072 ms |
15164 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
3 ms |
10444 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
3 ms |
10332 KB |
Output is correct |
7 |
Correct |
3 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
4 ms |
10332 KB |
Output is correct |
10 |
Correct |
4 ms |
10332 KB |
Output is correct |
11 |
Correct |
4 ms |
10332 KB |
Output is correct |
12 |
Correct |
7 ms |
10448 KB |
Output is correct |
13 |
Correct |
7 ms |
10332 KB |
Output is correct |
14 |
Correct |
9 ms |
10968 KB |
Output is correct |
15 |
Correct |
3 ms |
10500 KB |
Output is correct |
16 |
Correct |
3 ms |
10332 KB |
Output is correct |
17 |
Correct |
3 ms |
10332 KB |
Output is correct |
18 |
Correct |
3 ms |
10332 KB |
Output is correct |
19 |
Correct |
3 ms |
10332 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
21 |
Correct |
5 ms |
10332 KB |
Output is correct |
22 |
Correct |
4 ms |
10476 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
5 ms |
10332 KB |
Output is correct |
25 |
Correct |
5 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10332 KB |
Output is correct |
27 |
Correct |
2 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10468 KB |
Output is correct |
29 |
Correct |
2 ms |
10332 KB |
Output is correct |
30 |
Correct |
58 ms |
10332 KB |
Output is correct |
31 |
Correct |
83 ms |
10544 KB |
Output is correct |
32 |
Correct |
121 ms |
10568 KB |
Output is correct |
33 |
Correct |
120 ms |
10580 KB |
Output is correct |
34 |
Correct |
125 ms |
10580 KB |
Output is correct |
35 |
Correct |
329 ms |
10568 KB |
Output is correct |
36 |
Correct |
328 ms |
10588 KB |
Output is correct |
37 |
Correct |
325 ms |
10588 KB |
Output is correct |
38 |
Correct |
70 ms |
10452 KB |
Output is correct |
39 |
Correct |
69 ms |
10656 KB |
Output is correct |
40 |
Correct |
70 ms |
10664 KB |
Output is correct |
41 |
Correct |
218 ms |
10672 KB |
Output is correct |
42 |
Correct |
219 ms |
10588 KB |
Output is correct |
43 |
Correct |
224 ms |
10644 KB |
Output is correct |
44 |
Correct |
70 ms |
10612 KB |
Output is correct |
45 |
Correct |
73 ms |
10588 KB |
Output is correct |
46 |
Correct |
72 ms |
10588 KB |
Output is correct |
47 |
Correct |
225 ms |
10836 KB |
Output is correct |
48 |
Correct |
223 ms |
10604 KB |
Output is correct |
49 |
Correct |
222 ms |
10588 KB |
Output is correct |
50 |
Correct |
144 ms |
10588 KB |
Output is correct |
51 |
Correct |
141 ms |
10588 KB |
Output is correct |
52 |
Correct |
144 ms |
10588 KB |
Output is correct |
53 |
Correct |
137 ms |
10588 KB |
Output is correct |
54 |
Correct |
139 ms |
10612 KB |
Output is correct |
55 |
Correct |
138 ms |
10588 KB |
Output is correct |
56 |
Correct |
14 ms |
10332 KB |
Output is correct |
57 |
Correct |
3 ms |
10332 KB |
Output is correct |
58 |
Correct |
3 ms |
10332 KB |
Output is correct |
59 |
Correct |
2 ms |
10332 KB |
Output is correct |
60 |
Correct |
2 ms |
10332 KB |
Output is correct |
61 |
Correct |
14 ms |
10500 KB |
Output is correct |
62 |
Execution timed out |
5050 ms |
18412 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |