# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
889636 |
2023-12-20T04:11:04 Z |
vjudge1 |
Tourism (JOI23_tourism) |
C++17 |
|
123 ms |
32520 KB |
#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;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 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 |
344 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 |
348 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 |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
488 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 |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 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 |
344 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 |
348 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 |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
488 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 |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
54 ms |
580 KB |
Output is correct |
31 |
Correct |
72 ms |
580 KB |
Output is correct |
32 |
Correct |
122 ms |
608 KB |
Output is correct |
33 |
Correct |
123 ms |
600 KB |
Output is correct |
34 |
Correct |
122 ms |
604 KB |
Output is correct |
35 |
Correct |
79 ms |
604 KB |
Output is correct |
36 |
Correct |
96 ms |
608 KB |
Output is correct |
37 |
Correct |
79 ms |
604 KB |
Output is correct |
38 |
Correct |
102 ms |
848 KB |
Output is correct |
39 |
Correct |
98 ms |
692 KB |
Output is correct |
40 |
Correct |
111 ms |
696 KB |
Output is correct |
41 |
Correct |
95 ms |
604 KB |
Output is correct |
42 |
Correct |
94 ms |
712 KB |
Output is correct |
43 |
Correct |
106 ms |
604 KB |
Output is correct |
44 |
Correct |
108 ms |
880 KB |
Output is correct |
45 |
Correct |
100 ms |
612 KB |
Output is correct |
46 |
Correct |
97 ms |
636 KB |
Output is correct |
47 |
Correct |
116 ms |
620 KB |
Output is correct |
48 |
Correct |
96 ms |
600 KB |
Output is correct |
49 |
Correct |
108 ms |
604 KB |
Output is correct |
50 |
Correct |
90 ms |
612 KB |
Output is correct |
51 |
Correct |
97 ms |
608 KB |
Output is correct |
52 |
Correct |
107 ms |
604 KB |
Output is correct |
53 |
Correct |
79 ms |
612 KB |
Output is correct |
54 |
Correct |
79 ms |
604 KB |
Output is correct |
55 |
Correct |
82 ms |
604 KB |
Output is correct |
56 |
Correct |
4 ms |
348 KB |
Output is correct |
57 |
Correct |
79 ms |
616 KB |
Output is correct |
58 |
Correct |
77 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Incorrect |
50 ms |
32520 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
19 ms |
4140 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Incorrect |
17 ms |
4444 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 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 |
344 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 |
348 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 |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
488 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 |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
54 ms |
580 KB |
Output is correct |
31 |
Correct |
72 ms |
580 KB |
Output is correct |
32 |
Correct |
122 ms |
608 KB |
Output is correct |
33 |
Correct |
123 ms |
600 KB |
Output is correct |
34 |
Correct |
122 ms |
604 KB |
Output is correct |
35 |
Correct |
79 ms |
604 KB |
Output is correct |
36 |
Correct |
96 ms |
608 KB |
Output is correct |
37 |
Correct |
79 ms |
604 KB |
Output is correct |
38 |
Correct |
102 ms |
848 KB |
Output is correct |
39 |
Correct |
98 ms |
692 KB |
Output is correct |
40 |
Correct |
111 ms |
696 KB |
Output is correct |
41 |
Correct |
95 ms |
604 KB |
Output is correct |
42 |
Correct |
94 ms |
712 KB |
Output is correct |
43 |
Correct |
106 ms |
604 KB |
Output is correct |
44 |
Correct |
108 ms |
880 KB |
Output is correct |
45 |
Correct |
100 ms |
612 KB |
Output is correct |
46 |
Correct |
97 ms |
636 KB |
Output is correct |
47 |
Correct |
116 ms |
620 KB |
Output is correct |
48 |
Correct |
96 ms |
600 KB |
Output is correct |
49 |
Correct |
108 ms |
604 KB |
Output is correct |
50 |
Correct |
90 ms |
612 KB |
Output is correct |
51 |
Correct |
97 ms |
608 KB |
Output is correct |
52 |
Correct |
107 ms |
604 KB |
Output is correct |
53 |
Correct |
79 ms |
612 KB |
Output is correct |
54 |
Correct |
79 ms |
604 KB |
Output is correct |
55 |
Correct |
82 ms |
604 KB |
Output is correct |
56 |
Correct |
4 ms |
348 KB |
Output is correct |
57 |
Correct |
79 ms |
616 KB |
Output is correct |
58 |
Correct |
77 ms |
604 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
4 ms |
344 KB |
Output is correct |
62 |
Incorrect |
50 ms |
32520 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |