#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 100100;
const int LOGN = 17;
vector < int > adj[N];
int up[N][LOGN], depth[N], use[N], T, ans, C[N];
void dfs(int v, int p) {
depth[v] = depth[p] + 1;
up[v][0] = p;
for(int i = 1; i < LOGN; ++ i) {
up[v][i] = up[ up[v][i - 1] ][i - 1];
}
for(auto &u : adj[v]) {
if(u != p) {
dfs(u, v);
}
}
}
int slow_LCA(int a, int b) {
if(depth[a] < depth[b]) {
swap(a, b);
}
ans += use[a] != T;
ans += use[b] != T;
use[a] = T;
use[b] = T;
for(; depth[a] > depth[b];) {
a = up[a][0];
if(use[a] == T) {
return b;
}
++ ans;
use[a] = T;
}
for(; a != b;) {
a = up[a][0];
b = up[b][0];
if(a == b) {
use[b] = T;
++ ans;
return a;
}
ans += 2;
use[a] = T;
use[b] = T;
}
return b;
}
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for(int i = 1, a, b; i < n; ++ i) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 1; i <= m; ++ i) {
cin >> C[i];
}
dfs(1, 1);
for(int l, r, m; q --> 0;) {
cin >> l >> r;
ans = 1;
m = C[l ++];
use[m] = ++ T;
for(; l <= r; ++ l) {
m = slow_LCA(m, C[l]);
}
cout << ans << '\n';
}
}
Compilation message
tourism.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
62 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2576 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2704 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2576 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2704 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
30 |
Correct |
9 ms |
2772 KB |
Output is correct |
31 |
Correct |
12 ms |
2772 KB |
Output is correct |
32 |
Correct |
18 ms |
2916 KB |
Output is correct |
33 |
Correct |
18 ms |
2920 KB |
Output is correct |
34 |
Correct |
18 ms |
2900 KB |
Output is correct |
35 |
Correct |
25 ms |
2920 KB |
Output is correct |
36 |
Correct |
25 ms |
2900 KB |
Output is correct |
37 |
Correct |
27 ms |
2912 KB |
Output is correct |
38 |
Correct |
25 ms |
2980 KB |
Output is correct |
39 |
Correct |
26 ms |
2900 KB |
Output is correct |
40 |
Correct |
26 ms |
2988 KB |
Output is correct |
41 |
Correct |
37 ms |
2996 KB |
Output is correct |
42 |
Correct |
41 ms |
2980 KB |
Output is correct |
43 |
Correct |
40 ms |
2960 KB |
Output is correct |
44 |
Correct |
27 ms |
2900 KB |
Output is correct |
45 |
Correct |
27 ms |
2940 KB |
Output is correct |
46 |
Correct |
28 ms |
2936 KB |
Output is correct |
47 |
Correct |
37 ms |
2900 KB |
Output is correct |
48 |
Correct |
38 ms |
2940 KB |
Output is correct |
49 |
Correct |
37 ms |
2940 KB |
Output is correct |
50 |
Correct |
10 ms |
2900 KB |
Output is correct |
51 |
Correct |
7 ms |
2924 KB |
Output is correct |
52 |
Correct |
8 ms |
2928 KB |
Output is correct |
53 |
Correct |
7 ms |
2900 KB |
Output is correct |
54 |
Correct |
7 ms |
2900 KB |
Output is correct |
55 |
Correct |
13 ms |
2900 KB |
Output is correct |
56 |
Correct |
10 ms |
2704 KB |
Output is correct |
57 |
Correct |
3 ms |
2900 KB |
Output is correct |
58 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
6 ms |
2644 KB |
Output is correct |
4 |
Execution timed out |
5030 ms |
11528 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
37 ms |
8724 KB |
Output is correct |
3 |
Correct |
50 ms |
9360 KB |
Output is correct |
4 |
Correct |
34 ms |
10120 KB |
Output is correct |
5 |
Correct |
58 ms |
13644 KB |
Output is correct |
6 |
Correct |
69 ms |
13724 KB |
Output is correct |
7 |
Correct |
72 ms |
13728 KB |
Output is correct |
8 |
Correct |
66 ms |
13720 KB |
Output is correct |
9 |
Correct |
70 ms |
13724 KB |
Output is correct |
10 |
Correct |
68 ms |
13668 KB |
Output is correct |
11 |
Correct |
65 ms |
13640 KB |
Output is correct |
12 |
Correct |
91 ms |
13712 KB |
Output is correct |
13 |
Correct |
72 ms |
13748 KB |
Output is correct |
14 |
Correct |
83 ms |
13748 KB |
Output is correct |
15 |
Correct |
72 ms |
13900 KB |
Output is correct |
16 |
Correct |
75 ms |
13744 KB |
Output is correct |
17 |
Correct |
68 ms |
13756 KB |
Output is correct |
18 |
Correct |
71 ms |
13736 KB |
Output is correct |
19 |
Correct |
63 ms |
13716 KB |
Output is correct |
20 |
Correct |
59 ms |
13696 KB |
Output is correct |
21 |
Correct |
55 ms |
13664 KB |
Output is correct |
22 |
Correct |
77 ms |
13704 KB |
Output is correct |
23 |
Correct |
73 ms |
13588 KB |
Output is correct |
24 |
Correct |
110 ms |
13692 KB |
Output is correct |
25 |
Correct |
179 ms |
13688 KB |
Output is correct |
26 |
Correct |
211 ms |
13704 KB |
Output is correct |
27 |
Correct |
304 ms |
13708 KB |
Output is correct |
28 |
Correct |
404 ms |
13688 KB |
Output is correct |
29 |
Correct |
522 ms |
13772 KB |
Output is correct |
30 |
Correct |
659 ms |
13744 KB |
Output is correct |
31 |
Correct |
655 ms |
13748 KB |
Output is correct |
32 |
Correct |
694 ms |
13740 KB |
Output is correct |
33 |
Correct |
445 ms |
13824 KB |
Output is correct |
34 |
Correct |
58 ms |
13880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
7 ms |
2708 KB |
Output is correct |
4 |
Execution timed out |
5065 ms |
9084 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2576 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2704 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
1 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
30 |
Correct |
9 ms |
2772 KB |
Output is correct |
31 |
Correct |
12 ms |
2772 KB |
Output is correct |
32 |
Correct |
18 ms |
2916 KB |
Output is correct |
33 |
Correct |
18 ms |
2920 KB |
Output is correct |
34 |
Correct |
18 ms |
2900 KB |
Output is correct |
35 |
Correct |
25 ms |
2920 KB |
Output is correct |
36 |
Correct |
25 ms |
2900 KB |
Output is correct |
37 |
Correct |
27 ms |
2912 KB |
Output is correct |
38 |
Correct |
25 ms |
2980 KB |
Output is correct |
39 |
Correct |
26 ms |
2900 KB |
Output is correct |
40 |
Correct |
26 ms |
2988 KB |
Output is correct |
41 |
Correct |
37 ms |
2996 KB |
Output is correct |
42 |
Correct |
41 ms |
2980 KB |
Output is correct |
43 |
Correct |
40 ms |
2960 KB |
Output is correct |
44 |
Correct |
27 ms |
2900 KB |
Output is correct |
45 |
Correct |
27 ms |
2940 KB |
Output is correct |
46 |
Correct |
28 ms |
2936 KB |
Output is correct |
47 |
Correct |
37 ms |
2900 KB |
Output is correct |
48 |
Correct |
38 ms |
2940 KB |
Output is correct |
49 |
Correct |
37 ms |
2940 KB |
Output is correct |
50 |
Correct |
10 ms |
2900 KB |
Output is correct |
51 |
Correct |
7 ms |
2924 KB |
Output is correct |
52 |
Correct |
8 ms |
2928 KB |
Output is correct |
53 |
Correct |
7 ms |
2900 KB |
Output is correct |
54 |
Correct |
7 ms |
2900 KB |
Output is correct |
55 |
Correct |
13 ms |
2900 KB |
Output is correct |
56 |
Correct |
10 ms |
2704 KB |
Output is correct |
57 |
Correct |
3 ms |
2900 KB |
Output is correct |
58 |
Correct |
2 ms |
2900 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
1 ms |
2644 KB |
Output is correct |
61 |
Correct |
6 ms |
2644 KB |
Output is correct |
62 |
Execution timed out |
5030 ms |
11528 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |