#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], n, m, q;
int SpTmin[LOGN][N], SpTmax[LOGN][N];
int lg2(int a) {
return 31 - __builtin_clz(a);
}
int get(int l, int r) {
int m = lg2(r - l + 1);
return max(SpTmax[m][l], SpTmax[m][r - (1<<m) + 1]) -
min(SpTmin[m][l], SpTmin[m][r - (1<<m) + 1]) + 1;
}
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;
}
void solve_subtask_3() {
for(int i = 1; i <= n; ++ i) {
SpTmax[0][i] = SpTmin[0][i] = C[i];
}
for(int lg = 1; (1<<lg) <= n; ++ lg) {
for(int i = 1; i <= n - (1<<lg) + 1; ++ i) {
SpTmax[lg][i] = max(SpTmax[lg - 1][i], SpTmax[lg - 1][i + (1<<(lg - 1))]);
SpTmin[lg][i] = min(SpTmin[lg - 1][i], SpTmin[lg - 1][i + (1<<(lg - 1))]);
}
}
for(int l, r; q --> 0;) {
cin >> l >> r;
cout << get(l, r) << '\n';
}
}
main() {
#ifdef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // Home
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
bool subtsk3 = true;
for(int i = 1, a, b; i < n; ++ i) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
subtsk3 &= (a == i && b == i + 1);
}
for(int i = 1; i <= m; ++ i) {
cin >> C[i];
}
if(subtsk3) {
solve_subtask_3();
return 0;
}
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:89:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
89 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2708 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 |
3 ms |
2712 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2672 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 |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 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 |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2708 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 |
3 ms |
2712 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2672 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 |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
30 |
Correct |
12 ms |
2856 KB |
Output is correct |
31 |
Correct |
12 ms |
2868 KB |
Output is correct |
32 |
Correct |
19 ms |
2900 KB |
Output is correct |
33 |
Correct |
19 ms |
2900 KB |
Output is correct |
34 |
Correct |
18 ms |
2924 KB |
Output is correct |
35 |
Correct |
26 ms |
2924 KB |
Output is correct |
36 |
Correct |
37 ms |
2920 KB |
Output is correct |
37 |
Correct |
31 ms |
2924 KB |
Output is correct |
38 |
Correct |
27 ms |
2980 KB |
Output is correct |
39 |
Correct |
26 ms |
2900 KB |
Output is correct |
40 |
Correct |
26 ms |
3004 KB |
Output is correct |
41 |
Correct |
41 ms |
2996 KB |
Output is correct |
42 |
Correct |
38 ms |
3000 KB |
Output is correct |
43 |
Correct |
36 ms |
2900 KB |
Output is correct |
44 |
Correct |
27 ms |
2948 KB |
Output is correct |
45 |
Correct |
27 ms |
2900 KB |
Output is correct |
46 |
Correct |
28 ms |
2956 KB |
Output is correct |
47 |
Correct |
48 ms |
2900 KB |
Output is correct |
48 |
Correct |
39 ms |
2932 KB |
Output is correct |
49 |
Correct |
43 ms |
2948 KB |
Output is correct |
50 |
Correct |
8 ms |
2900 KB |
Output is correct |
51 |
Correct |
8 ms |
2900 KB |
Output is correct |
52 |
Correct |
9 ms |
3028 KB |
Output is correct |
53 |
Correct |
8 ms |
2928 KB |
Output is correct |
54 |
Correct |
8 ms |
2900 KB |
Output is correct |
55 |
Correct |
11 ms |
2900 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
2 ms |
2900 KB |
Output is correct |
58 |
Correct |
3 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Incorrect |
35 ms |
11544 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
27 ms |
8664 KB |
Output is correct |
3 |
Correct |
34 ms |
9300 KB |
Output is correct |
4 |
Correct |
51 ms |
10068 KB |
Output is correct |
5 |
Correct |
58 ms |
13744 KB |
Output is correct |
6 |
Correct |
46 ms |
13728 KB |
Output is correct |
7 |
Correct |
46 ms |
13644 KB |
Output is correct |
8 |
Correct |
50 ms |
13732 KB |
Output is correct |
9 |
Correct |
63 ms |
13628 KB |
Output is correct |
10 |
Correct |
52 ms |
13652 KB |
Output is correct |
11 |
Correct |
66 ms |
13740 KB |
Output is correct |
12 |
Correct |
55 ms |
13640 KB |
Output is correct |
13 |
Correct |
55 ms |
13744 KB |
Output is correct |
14 |
Correct |
91 ms |
13772 KB |
Output is correct |
15 |
Correct |
59 ms |
13892 KB |
Output is correct |
16 |
Correct |
51 ms |
13736 KB |
Output is correct |
17 |
Correct |
57 ms |
13728 KB |
Output is correct |
18 |
Correct |
54 ms |
13740 KB |
Output is correct |
19 |
Correct |
49 ms |
13708 KB |
Output is correct |
20 |
Correct |
53 ms |
13604 KB |
Output is correct |
21 |
Correct |
49 ms |
13652 KB |
Output is correct |
22 |
Correct |
70 ms |
13660 KB |
Output is correct |
23 |
Correct |
111 ms |
13676 KB |
Output is correct |
24 |
Correct |
108 ms |
13592 KB |
Output is correct |
25 |
Correct |
187 ms |
13696 KB |
Output is correct |
26 |
Correct |
267 ms |
13728 KB |
Output is correct |
27 |
Correct |
332 ms |
13712 KB |
Output is correct |
28 |
Correct |
382 ms |
13816 KB |
Output is correct |
29 |
Correct |
510 ms |
13708 KB |
Output is correct |
30 |
Correct |
628 ms |
13704 KB |
Output is correct |
31 |
Correct |
682 ms |
13760 KB |
Output is correct |
32 |
Correct |
708 ms |
13772 KB |
Output is correct |
33 |
Correct |
465 ms |
13848 KB |
Output is correct |
34 |
Correct |
77 ms |
13856 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 |
2 ms |
2644 KB |
Output is correct |
4 |
Execution timed out |
5053 ms |
8988 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2708 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 |
3 ms |
2712 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2672 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 |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
1 ms |
2644 KB |
Output is correct |
30 |
Correct |
12 ms |
2856 KB |
Output is correct |
31 |
Correct |
12 ms |
2868 KB |
Output is correct |
32 |
Correct |
19 ms |
2900 KB |
Output is correct |
33 |
Correct |
19 ms |
2900 KB |
Output is correct |
34 |
Correct |
18 ms |
2924 KB |
Output is correct |
35 |
Correct |
26 ms |
2924 KB |
Output is correct |
36 |
Correct |
37 ms |
2920 KB |
Output is correct |
37 |
Correct |
31 ms |
2924 KB |
Output is correct |
38 |
Correct |
27 ms |
2980 KB |
Output is correct |
39 |
Correct |
26 ms |
2900 KB |
Output is correct |
40 |
Correct |
26 ms |
3004 KB |
Output is correct |
41 |
Correct |
41 ms |
2996 KB |
Output is correct |
42 |
Correct |
38 ms |
3000 KB |
Output is correct |
43 |
Correct |
36 ms |
2900 KB |
Output is correct |
44 |
Correct |
27 ms |
2948 KB |
Output is correct |
45 |
Correct |
27 ms |
2900 KB |
Output is correct |
46 |
Correct |
28 ms |
2956 KB |
Output is correct |
47 |
Correct |
48 ms |
2900 KB |
Output is correct |
48 |
Correct |
39 ms |
2932 KB |
Output is correct |
49 |
Correct |
43 ms |
2948 KB |
Output is correct |
50 |
Correct |
8 ms |
2900 KB |
Output is correct |
51 |
Correct |
8 ms |
2900 KB |
Output is correct |
52 |
Correct |
9 ms |
3028 KB |
Output is correct |
53 |
Correct |
8 ms |
2928 KB |
Output is correct |
54 |
Correct |
8 ms |
2900 KB |
Output is correct |
55 |
Correct |
11 ms |
2900 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
2 ms |
2900 KB |
Output is correct |
58 |
Correct |
3 ms |
2900 KB |
Output is correct |
59 |
Correct |
1 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
2 ms |
2644 KB |
Output is correct |
62 |
Incorrect |
35 ms |
11544 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |