#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
using namespace std ;
const int N = 1e5, NS = 2000 ;
bool flag1 ;
bool us[NS + 1] ;
int n, m, q, c[N + 1], l[N + 1], r[N + 1], p[N + 1], dist[N + 1],ind[N + 1], pw[2 * N + 1] ;
vector<int> v[N + 1] ;
vector<pair<int, int>> tree ;
pair<int, int> mn_mx, mn[2 * N + 1][20] ;
void dfs(int city, int last)
{
ind[city] = tree.size() ;
tree.push_back({dist[city], city}) ;
p[city] = last ;
for(int i : v[city])
{
if(i == last)
continue ;
dist[i] = dist[city] + 1 ;
dfs(i, city) ;
tree.push_back({dist[city], city}) ;
}
}
void build_lca()
{
for(int i = 2 ; i <= tree.size() ; i++)
pw[i] = pw[i / 2] + 1 ;
for(int i = 0 ; i < tree.size() ; i++)
mn[i][0] = tree[i] ;
for(int i = 1 ; i < 20 ; i++)
for(int j = 0 ; j <= (int)tree.size() - (1 << i) ; j++)
mn[j][i] = min(mn[j][i - 1], mn[j + (1 << (i - 1))][i - 1]) ;
}
int get_lca(int a, int b)
{
a = ind[a] ;
b = ind[b] ;
if(a > b)
swap(a, b) ;
int num = pw[b - a + 1] ;
return min(mn[a][num], mn[b - (1 << num) + 1][num]).se ;
}
void build_mn_mx()
{
}
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n >> m >> q ;
for(int i = 1 ; i < n ; i++)
{
int a, b ;
cin >> a >> b ;
if(a != i || b != i + 1)flag1 = 1 ;
v[a].push_back(b) ;
v[b].push_back(a) ;
}
for(int i = 1 ; i <= m ; i++)
cin >> c[i] ;
for(int i = 1 ; i <= q ; i++)
cin >> l[i] >> r[i] ;
if(n <= 2000 && m <= 2000 && q <= 2000)
{
us[0] = 1 ;
dfs(1, 0) ;
build_lca() ;
for(int i = 1 ; i <= q ; i++)
{
int ans = 0, lc ;
lc = c[l[i]] ;
for(int j = l[i] + 1 ; j <= r[i] ; j++)
lc = get_lca(lc, c[j]) ;
for(int j = l[i] ; j <= r[i] ; j++)
{
int now = c[j] ;
while(!us[now])
{
ans++ ;
us[now] = 1 ;
if(now == lc)
break ;
now = p[now] ;
}
}
for(int i = 1 ; i <= n ; i++)
us[i] = 0 ;
cout << ans << '\n' ;
}
return 0 ;
}
if(!flag1)
{
// build_mx_mn() ;
}
return 0 ;
}
//8 8 1
//1 2
//2 3
//3 4
//4 5
//5 6
//6 7
//7 8
//8 6 4 3 5 2 4 7
//3 5
Compilation message
tourism.cpp: In function 'void build_lca()':
tourism.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i = 2 ; i <= tree.size() ; i++)
| ~~^~~~~~~~~~~~~~
tourism.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0 ; i < tree.size() ; i++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2812 KB |
Output is correct |
14 |
Correct |
2 ms |
2816 KB |
Output is correct |
15 |
Correct |
2 ms |
2820 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2748 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2812 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2924 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 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 |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2812 KB |
Output is correct |
14 |
Correct |
2 ms |
2816 KB |
Output is correct |
15 |
Correct |
2 ms |
2820 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2748 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2812 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2924 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
14 ms |
3332 KB |
Output is correct |
31 |
Correct |
20 ms |
3336 KB |
Output is correct |
32 |
Correct |
30 ms |
3560 KB |
Output is correct |
33 |
Correct |
29 ms |
3460 KB |
Output is correct |
34 |
Correct |
30 ms |
3572 KB |
Output is correct |
35 |
Correct |
48 ms |
3464 KB |
Output is correct |
36 |
Correct |
50 ms |
3564 KB |
Output is correct |
37 |
Correct |
48 ms |
3540 KB |
Output is correct |
38 |
Correct |
29 ms |
3668 KB |
Output is correct |
39 |
Correct |
30 ms |
3596 KB |
Output is correct |
40 |
Correct |
26 ms |
3668 KB |
Output is correct |
41 |
Correct |
56 ms |
3736 KB |
Output is correct |
42 |
Correct |
52 ms |
3712 KB |
Output is correct |
43 |
Correct |
65 ms |
3668 KB |
Output is correct |
44 |
Correct |
25 ms |
3540 KB |
Output is correct |
45 |
Correct |
28 ms |
3616 KB |
Output is correct |
46 |
Correct |
26 ms |
3648 KB |
Output is correct |
47 |
Correct |
50 ms |
3540 KB |
Output is correct |
48 |
Correct |
57 ms |
3604 KB |
Output is correct |
49 |
Correct |
52 ms |
3640 KB |
Output is correct |
50 |
Correct |
26 ms |
3588 KB |
Output is correct |
51 |
Correct |
26 ms |
3580 KB |
Output is correct |
52 |
Correct |
29 ms |
3600 KB |
Output is correct |
53 |
Correct |
25 ms |
3540 KB |
Output is correct |
54 |
Correct |
25 ms |
3540 KB |
Output is correct |
55 |
Correct |
25 ms |
3576 KB |
Output is correct |
56 |
Correct |
18 ms |
2752 KB |
Output is correct |
57 |
Correct |
3 ms |
3460 KB |
Output is correct |
58 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
18 ms |
2732 KB |
Output is correct |
4 |
Incorrect |
28 ms |
7272 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
16 ms |
4604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
18 ms |
2732 KB |
Output is correct |
4 |
Incorrect |
28 ms |
5528 KB |
Output isn't correct |
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 |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2812 KB |
Output is correct |
14 |
Correct |
2 ms |
2816 KB |
Output is correct |
15 |
Correct |
2 ms |
2820 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2748 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2812 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2924 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
14 ms |
3332 KB |
Output is correct |
31 |
Correct |
20 ms |
3336 KB |
Output is correct |
32 |
Correct |
30 ms |
3560 KB |
Output is correct |
33 |
Correct |
29 ms |
3460 KB |
Output is correct |
34 |
Correct |
30 ms |
3572 KB |
Output is correct |
35 |
Correct |
48 ms |
3464 KB |
Output is correct |
36 |
Correct |
50 ms |
3564 KB |
Output is correct |
37 |
Correct |
48 ms |
3540 KB |
Output is correct |
38 |
Correct |
29 ms |
3668 KB |
Output is correct |
39 |
Correct |
30 ms |
3596 KB |
Output is correct |
40 |
Correct |
26 ms |
3668 KB |
Output is correct |
41 |
Correct |
56 ms |
3736 KB |
Output is correct |
42 |
Correct |
52 ms |
3712 KB |
Output is correct |
43 |
Correct |
65 ms |
3668 KB |
Output is correct |
44 |
Correct |
25 ms |
3540 KB |
Output is correct |
45 |
Correct |
28 ms |
3616 KB |
Output is correct |
46 |
Correct |
26 ms |
3648 KB |
Output is correct |
47 |
Correct |
50 ms |
3540 KB |
Output is correct |
48 |
Correct |
57 ms |
3604 KB |
Output is correct |
49 |
Correct |
52 ms |
3640 KB |
Output is correct |
50 |
Correct |
26 ms |
3588 KB |
Output is correct |
51 |
Correct |
26 ms |
3580 KB |
Output is correct |
52 |
Correct |
29 ms |
3600 KB |
Output is correct |
53 |
Correct |
25 ms |
3540 KB |
Output is correct |
54 |
Correct |
25 ms |
3540 KB |
Output is correct |
55 |
Correct |
25 ms |
3576 KB |
Output is correct |
56 |
Correct |
18 ms |
2752 KB |
Output is correct |
57 |
Correct |
3 ms |
3460 KB |
Output is correct |
58 |
Correct |
3 ms |
3540 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
18 ms |
2732 KB |
Output is correct |
62 |
Incorrect |
28 ms |
7272 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |