#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
struct Sp{
vector <vector<int>> T;
int _s;
Sp(auto &a, int s){
_s = s;
T.resize(20);
int n = a.size();
for ( auto &x: T ){
x.resize(n);
}
T[0] = a;
for ( auto &u: T[0] ) u *= _s;
for ( int i = 1; i < 20; i++ ){
int k = 1 << i - 1;
for ( int j = 0; j < n; j++ ){
T[i][j] = min(T[i - 1][j], T[i - 1][min(n - 1, j + k)]);
}
}
}
int get(int l, int r){
int lg = __lg(r - l + 1), k = 1 << lg;
return min(T[lg][l], T[lg][r - k + 1]) * _s;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q; cin >> n >> m >> q;
vector <vector<int>> G(n);
bool Qflag = true;
for ( int i = 1; i < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
G[u].pb(v), G[v].pb(u);
if ( u + 1 != v ) Qflag = false;
}
vector <int> C(m);
for ( auto &x: C ){
cin >> x; --x;
}
if ( max({n, m, q}) <= 3000 ){
while ( q-- ){
int l, r; cin >> l >> r;
--l, --r;
int s = r - l + 1;
vector <int> sub(n), hv(n);
for ( int i = l; i <= r; i++ ){
sub[C[i]]++;
hv[C[i]] = true;
}
auto dfs = [&](auto dfs, int u, int p) -> void{
for ( auto &v: G[u] ){
if ( v != p ){
dfs(dfs, v, u);
sub[u] += sub[v];
}
}
};
dfs(dfs, 0, -1);
int ans = 0;
auto dfs2 = [&](auto dfs2, int u, int p) -> void{
bool flag = false, vs = false;
for ( auto &v: G[u] ){
if ( sub[v] ){
if ( !vs ) vs = true;
else flag = true;
}
}
if ( hv[u] || flag ) ++ans;
for ( auto &v: G[u] ){
if ( v != p ){
sub[u] -= sub[v];
sub[v] += sub[u];
dfs2(dfs2, v, u);
sub[v] -= sub[u];
sub[u] += sub[v];
}
}
};
dfs2(dfs2, 0, -1);
cout << ans << ln;
} exit(0);
}
if ( Qflag ){
Sp mn(C, 1), mx(C, -1);
while ( q-- ){
int l, r; cin >> l >> r;
--l, --r;
cout << mx.get(l, r) - mn.get(l, r) + 1 << ln;
}
}
cout << '\n';
}
Compilation message
tourism.cpp:34:8: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
34 | Sp(auto &a, int s){
| ^~~~
tourism.cpp: In function 'int main()':
tourism.cpp:77:17: warning: unused variable 's' [-Wunused-variable]
77 | int s = r - l + 1;
| ^
tourism.cpp: In instantiation of 'Sp::Sp(auto:23&, long long int) [with auto:23 = std::vector<long long int>]':
tourism.cpp:117:19: required from here
tourism.cpp:44:28: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
44 | int k = 1 << i - 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
488 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
356 KB |
Output is correct |
29 |
Correct |
2 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
488 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
356 KB |
Output is correct |
29 |
Correct |
2 ms |
480 KB |
Output is correct |
30 |
Correct |
55 ms |
588 KB |
Output is correct |
31 |
Correct |
72 ms |
348 KB |
Output is correct |
32 |
Correct |
117 ms |
604 KB |
Output is correct |
33 |
Correct |
119 ms |
608 KB |
Output is correct |
34 |
Correct |
115 ms |
604 KB |
Output is correct |
35 |
Correct |
79 ms |
604 KB |
Output is correct |
36 |
Correct |
77 ms |
620 KB |
Output is correct |
37 |
Correct |
80 ms |
600 KB |
Output is correct |
38 |
Correct |
96 ms |
600 KB |
Output is correct |
39 |
Correct |
99 ms |
684 KB |
Output is correct |
40 |
Correct |
94 ms |
600 KB |
Output is correct |
41 |
Correct |
94 ms |
856 KB |
Output is correct |
42 |
Correct |
94 ms |
696 KB |
Output is correct |
43 |
Correct |
94 ms |
604 KB |
Output is correct |
44 |
Correct |
97 ms |
600 KB |
Output is correct |
45 |
Correct |
100 ms |
604 KB |
Output is correct |
46 |
Correct |
112 ms |
640 KB |
Output is correct |
47 |
Correct |
103 ms |
600 KB |
Output is correct |
48 |
Correct |
96 ms |
616 KB |
Output is correct |
49 |
Correct |
95 ms |
604 KB |
Output is correct |
50 |
Correct |
86 ms |
848 KB |
Output is correct |
51 |
Correct |
93 ms |
604 KB |
Output is correct |
52 |
Correct |
79 ms |
608 KB |
Output is correct |
53 |
Correct |
106 ms |
616 KB |
Output is correct |
54 |
Correct |
109 ms |
768 KB |
Output is correct |
55 |
Correct |
113 ms |
624 KB |
Output is correct |
56 |
Correct |
5 ms |
512 KB |
Output is correct |
57 |
Correct |
112 ms |
596 KB |
Output is correct |
58 |
Correct |
74 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
51 ms |
32352 KB |
Output is correct |
5 |
Correct |
43 ms |
23488 KB |
Output is correct |
6 |
Correct |
59 ms |
32084 KB |
Output is correct |
7 |
Correct |
79 ms |
38740 KB |
Output is correct |
8 |
Correct |
65 ms |
38736 KB |
Output is correct |
9 |
Correct |
70 ms |
38580 KB |
Output is correct |
10 |
Correct |
65 ms |
38736 KB |
Output is correct |
11 |
Correct |
64 ms |
38740 KB |
Output is correct |
12 |
Correct |
57 ms |
38480 KB |
Output is correct |
13 |
Correct |
57 ms |
38740 KB |
Output is correct |
14 |
Correct |
58 ms |
38740 KB |
Output is correct |
15 |
Correct |
31 ms |
5980 KB |
Output is correct |
16 |
Correct |
62 ms |
38152 KB |
Output is correct |
17 |
Correct |
43 ms |
32760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
15 ms |
4188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Incorrect |
16 ms |
4444 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
488 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
356 KB |
Output is correct |
29 |
Correct |
2 ms |
480 KB |
Output is correct |
30 |
Correct |
55 ms |
588 KB |
Output is correct |
31 |
Correct |
72 ms |
348 KB |
Output is correct |
32 |
Correct |
117 ms |
604 KB |
Output is correct |
33 |
Correct |
119 ms |
608 KB |
Output is correct |
34 |
Correct |
115 ms |
604 KB |
Output is correct |
35 |
Correct |
79 ms |
604 KB |
Output is correct |
36 |
Correct |
77 ms |
620 KB |
Output is correct |
37 |
Correct |
80 ms |
600 KB |
Output is correct |
38 |
Correct |
96 ms |
600 KB |
Output is correct |
39 |
Correct |
99 ms |
684 KB |
Output is correct |
40 |
Correct |
94 ms |
600 KB |
Output is correct |
41 |
Correct |
94 ms |
856 KB |
Output is correct |
42 |
Correct |
94 ms |
696 KB |
Output is correct |
43 |
Correct |
94 ms |
604 KB |
Output is correct |
44 |
Correct |
97 ms |
600 KB |
Output is correct |
45 |
Correct |
100 ms |
604 KB |
Output is correct |
46 |
Correct |
112 ms |
640 KB |
Output is correct |
47 |
Correct |
103 ms |
600 KB |
Output is correct |
48 |
Correct |
96 ms |
616 KB |
Output is correct |
49 |
Correct |
95 ms |
604 KB |
Output is correct |
50 |
Correct |
86 ms |
848 KB |
Output is correct |
51 |
Correct |
93 ms |
604 KB |
Output is correct |
52 |
Correct |
79 ms |
608 KB |
Output is correct |
53 |
Correct |
106 ms |
616 KB |
Output is correct |
54 |
Correct |
109 ms |
768 KB |
Output is correct |
55 |
Correct |
113 ms |
624 KB |
Output is correct |
56 |
Correct |
5 ms |
512 KB |
Output is correct |
57 |
Correct |
112 ms |
596 KB |
Output is correct |
58 |
Correct |
74 ms |
600 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
4 ms |
348 KB |
Output is correct |
62 |
Correct |
51 ms |
32352 KB |
Output is correct |
63 |
Correct |
43 ms |
23488 KB |
Output is correct |
64 |
Correct |
59 ms |
32084 KB |
Output is correct |
65 |
Correct |
79 ms |
38740 KB |
Output is correct |
66 |
Correct |
65 ms |
38736 KB |
Output is correct |
67 |
Correct |
70 ms |
38580 KB |
Output is correct |
68 |
Correct |
65 ms |
38736 KB |
Output is correct |
69 |
Correct |
64 ms |
38740 KB |
Output is correct |
70 |
Correct |
57 ms |
38480 KB |
Output is correct |
71 |
Correct |
57 ms |
38740 KB |
Output is correct |
72 |
Correct |
58 ms |
38740 KB |
Output is correct |
73 |
Correct |
31 ms |
5980 KB |
Output is correct |
74 |
Correct |
62 ms |
38152 KB |
Output is correct |
75 |
Correct |
43 ms |
32760 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Incorrect |
15 ms |
4188 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |