#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
using namespace __gnu_pbds;
const int mod = 1e9+7;
const double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 1e5+5;
vector<int> g[N];
int ans = 0;
int c[N],t2[N*4],t[N*4],dis[N],up[20][N],tin[N],tout[N];
bool vis[N];
int par[N];
vector<int> pos[N];
int timer;
void build(int v, int l, int r){
if(l == r){
t[v] = c[l];
t2[v] = c[l];
return;
}
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid + 1, r);
t[v] = min(t[v * 2], t[v * 2 + 1]);
t2[v] = max(t2[v * 2], t2[v * 2 + 1]);
}
int getmn(int v, int tl, int tr, int l, int r) {
if (l > tr || tl > r) return 1e18;
if (l <= tl && tr <= r)return t[v];
int tm = (tl + tr) / 2;
return min(getmn(v * 2, tl, tm, l, r),getmn(v * 2 + 1, tm + 1, tr, l, r));
}
int getmx(int v, int tl, int tr, int l, int r) {
if (l > tr || tl > r) return 0;
if (l <= tl && tr <= r)return t2[v];
int tm = (tl + tr) / 2;
return max(getmx(v * 2, tl, tm, l, r),getmx(v * 2 + 1, tm + 1, tr, l, r));
}
void dfs(int v,int p,int l,int r){
par[v] = p;
for(int i : pos[v]){
if(l <= i && i <= r){
int h = v;
while(h != 0 && !vis[h]){
vis[h] = 1;
h = par[h];
}
break;
}
}
for(int to : g[v]){
if(to != p) dfs(to,v,l,r);
}
}
void solve(){
int n,m,q;
cin >> n >> m >> q;
bool ok = 1;
for(int i = 1; i < n; i++){
int u,v; cin >> u >> v;
g[u].pb(v); g[v].pb(u);
if(!(u == i && v == i+1)) ok = 0;
}
for(int i = 1; i <= m; i++){
cin >> c[i];
}
if(ok){
build(1,1,m);
while(q--){
int l,r; cin >> l >> r;
cout << getmx(1,1,m,l,r) - getmn(1,1,m,l,r) + 1 << '\n';
}
return;
}
for(int i = 1; i <= m; i++) pos[c[i]].pb(i);
while(q--){
int l,r; cin >> l >> r;
dfs(c[l],0,l,r);
int cnt = 0;
for(int i = 1; i <= n; i++) cnt += vis[i],vis[i] = 0;
cout << cnt << '\n';
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message
tourism.cpp:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
103 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8280 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8524 KB |
Output is correct |
14 |
Correct |
3 ms |
8280 KB |
Output is correct |
15 |
Correct |
4 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
4 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8284 KB |
Output is correct |
25 |
Correct |
3 ms |
8280 KB |
Output is correct |
26 |
Correct |
4 ms |
8488 KB |
Output is correct |
27 |
Correct |
3 ms |
12380 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8280 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8524 KB |
Output is correct |
14 |
Correct |
3 ms |
8280 KB |
Output is correct |
15 |
Correct |
4 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
4 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8284 KB |
Output is correct |
25 |
Correct |
3 ms |
8280 KB |
Output is correct |
26 |
Correct |
4 ms |
8488 KB |
Output is correct |
27 |
Correct |
3 ms |
12380 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8508 KB |
Output is correct |
30 |
Correct |
47 ms |
8600 KB |
Output is correct |
31 |
Correct |
61 ms |
8540 KB |
Output is correct |
32 |
Correct |
94 ms |
8536 KB |
Output is correct |
33 |
Correct |
98 ms |
8604 KB |
Output is correct |
34 |
Correct |
95 ms |
8540 KB |
Output is correct |
35 |
Correct |
37 ms |
8540 KB |
Output is correct |
36 |
Correct |
38 ms |
8628 KB |
Output is correct |
37 |
Correct |
38 ms |
8540 KB |
Output is correct |
38 |
Correct |
85 ms |
8716 KB |
Output is correct |
39 |
Correct |
87 ms |
8540 KB |
Output is correct |
40 |
Correct |
84 ms |
8536 KB |
Output is correct |
41 |
Correct |
49 ms |
8536 KB |
Output is correct |
42 |
Correct |
47 ms |
8536 KB |
Output is correct |
43 |
Correct |
45 ms |
8536 KB |
Output is correct |
44 |
Correct |
84 ms |
8536 KB |
Output is correct |
45 |
Correct |
84 ms |
8640 KB |
Output is correct |
46 |
Correct |
85 ms |
8796 KB |
Output is correct |
47 |
Correct |
44 ms |
8540 KB |
Output is correct |
48 |
Correct |
56 ms |
8536 KB |
Output is correct |
49 |
Correct |
45 ms |
8536 KB |
Output is correct |
50 |
Correct |
68 ms |
8540 KB |
Output is correct |
51 |
Correct |
66 ms |
8628 KB |
Output is correct |
52 |
Correct |
68 ms |
8636 KB |
Output is correct |
53 |
Correct |
67 ms |
8612 KB |
Output is correct |
54 |
Correct |
66 ms |
8620 KB |
Output is correct |
55 |
Correct |
65 ms |
8540 KB |
Output is correct |
56 |
Correct |
4 ms |
12380 KB |
Output is correct |
57 |
Correct |
31 ms |
8596 KB |
Output is correct |
58 |
Correct |
85 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
2 ms |
12464 KB |
Output is correct |
3 |
Correct |
4 ms |
12380 KB |
Output is correct |
4 |
Correct |
77 ms |
16588 KB |
Output is correct |
5 |
Correct |
69 ms |
17488 KB |
Output is correct |
6 |
Correct |
65 ms |
17924 KB |
Output is correct |
7 |
Correct |
103 ms |
18256 KB |
Output is correct |
8 |
Correct |
103 ms |
18224 KB |
Output is correct |
9 |
Correct |
102 ms |
18260 KB |
Output is correct |
10 |
Correct |
101 ms |
18260 KB |
Output is correct |
11 |
Correct |
102 ms |
18260 KB |
Output is correct |
12 |
Correct |
91 ms |
18256 KB |
Output is correct |
13 |
Correct |
89 ms |
18260 KB |
Output is correct |
14 |
Correct |
90 ms |
18264 KB |
Output is correct |
15 |
Correct |
33 ms |
15708 KB |
Output is correct |
16 |
Correct |
77 ms |
17936 KB |
Output is correct |
17 |
Correct |
81 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
3328 ms |
11700 KB |
Output is correct |
3 |
Correct |
4208 ms |
13140 KB |
Output is correct |
4 |
Correct |
4223 ms |
13212 KB |
Output is correct |
5 |
Correct |
39 ms |
15184 KB |
Output is correct |
6 |
Correct |
57 ms |
15384 KB |
Output is correct |
7 |
Correct |
78 ms |
15188 KB |
Output is correct |
8 |
Correct |
191 ms |
15372 KB |
Output is correct |
9 |
Correct |
497 ms |
15376 KB |
Output is correct |
10 |
Correct |
1379 ms |
15368 KB |
Output is correct |
11 |
Correct |
4589 ms |
15372 KB |
Output is correct |
12 |
Execution timed out |
5070 ms |
15372 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
3 ms |
12380 KB |
Output is correct |
3 |
Correct |
4 ms |
12380 KB |
Output is correct |
4 |
Execution timed out |
5057 ms |
12580 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8280 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
4 ms |
8280 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8524 KB |
Output is correct |
14 |
Correct |
3 ms |
8280 KB |
Output is correct |
15 |
Correct |
4 ms |
8536 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
4 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8536 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8284 KB |
Output is correct |
25 |
Correct |
3 ms |
8280 KB |
Output is correct |
26 |
Correct |
4 ms |
8488 KB |
Output is correct |
27 |
Correct |
3 ms |
12380 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8508 KB |
Output is correct |
30 |
Correct |
47 ms |
8600 KB |
Output is correct |
31 |
Correct |
61 ms |
8540 KB |
Output is correct |
32 |
Correct |
94 ms |
8536 KB |
Output is correct |
33 |
Correct |
98 ms |
8604 KB |
Output is correct |
34 |
Correct |
95 ms |
8540 KB |
Output is correct |
35 |
Correct |
37 ms |
8540 KB |
Output is correct |
36 |
Correct |
38 ms |
8628 KB |
Output is correct |
37 |
Correct |
38 ms |
8540 KB |
Output is correct |
38 |
Correct |
85 ms |
8716 KB |
Output is correct |
39 |
Correct |
87 ms |
8540 KB |
Output is correct |
40 |
Correct |
84 ms |
8536 KB |
Output is correct |
41 |
Correct |
49 ms |
8536 KB |
Output is correct |
42 |
Correct |
47 ms |
8536 KB |
Output is correct |
43 |
Correct |
45 ms |
8536 KB |
Output is correct |
44 |
Correct |
84 ms |
8536 KB |
Output is correct |
45 |
Correct |
84 ms |
8640 KB |
Output is correct |
46 |
Correct |
85 ms |
8796 KB |
Output is correct |
47 |
Correct |
44 ms |
8540 KB |
Output is correct |
48 |
Correct |
56 ms |
8536 KB |
Output is correct |
49 |
Correct |
45 ms |
8536 KB |
Output is correct |
50 |
Correct |
68 ms |
8540 KB |
Output is correct |
51 |
Correct |
66 ms |
8628 KB |
Output is correct |
52 |
Correct |
68 ms |
8636 KB |
Output is correct |
53 |
Correct |
67 ms |
8612 KB |
Output is correct |
54 |
Correct |
66 ms |
8620 KB |
Output is correct |
55 |
Correct |
65 ms |
8540 KB |
Output is correct |
56 |
Correct |
4 ms |
12380 KB |
Output is correct |
57 |
Correct |
31 ms |
8596 KB |
Output is correct |
58 |
Correct |
85 ms |
8536 KB |
Output is correct |
59 |
Correct |
2 ms |
12380 KB |
Output is correct |
60 |
Correct |
2 ms |
12464 KB |
Output is correct |
61 |
Correct |
4 ms |
12380 KB |
Output is correct |
62 |
Correct |
77 ms |
16588 KB |
Output is correct |
63 |
Correct |
69 ms |
17488 KB |
Output is correct |
64 |
Correct |
65 ms |
17924 KB |
Output is correct |
65 |
Correct |
103 ms |
18256 KB |
Output is correct |
66 |
Correct |
103 ms |
18224 KB |
Output is correct |
67 |
Correct |
102 ms |
18260 KB |
Output is correct |
68 |
Correct |
101 ms |
18260 KB |
Output is correct |
69 |
Correct |
102 ms |
18260 KB |
Output is correct |
70 |
Correct |
91 ms |
18256 KB |
Output is correct |
71 |
Correct |
89 ms |
18260 KB |
Output is correct |
72 |
Correct |
90 ms |
18264 KB |
Output is correct |
73 |
Correct |
33 ms |
15708 KB |
Output is correct |
74 |
Correct |
77 ms |
17936 KB |
Output is correct |
75 |
Correct |
81 ms |
14680 KB |
Output is correct |
76 |
Correct |
2 ms |
8280 KB |
Output is correct |
77 |
Correct |
3328 ms |
11700 KB |
Output is correct |
78 |
Correct |
4208 ms |
13140 KB |
Output is correct |
79 |
Correct |
4223 ms |
13212 KB |
Output is correct |
80 |
Correct |
39 ms |
15184 KB |
Output is correct |
81 |
Correct |
57 ms |
15384 KB |
Output is correct |
82 |
Correct |
78 ms |
15188 KB |
Output is correct |
83 |
Correct |
191 ms |
15372 KB |
Output is correct |
84 |
Correct |
497 ms |
15376 KB |
Output is correct |
85 |
Correct |
1379 ms |
15368 KB |
Output is correct |
86 |
Correct |
4589 ms |
15372 KB |
Output is correct |
87 |
Execution timed out |
5070 ms |
15372 KB |
Time limit exceeded |
88 |
Halted |
0 ms |
0 KB |
- |