# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
889606 |
2023-12-20T03:45:31 Z |
vjudge1 |
Tourism (JOI23_tourism) |
C++17 |
|
4296 ms |
24580 KB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define f first
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
const int mod= 1e9 +7;
const int N=1e5*4;
int binpow (int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binpow (a, n-1) * a;
else {
int b = binpow (a, n/2);
return b * b;
}
}
vector<int>vis(N+1),vis1(N+1);
vector<int>v[N];
pii t[N*4];
void build(int tl,int tr,int v){
if(tl==tr){
t[v].f = vis[tl];
t[v].s = vis[tl];
return;
}
int tm = (tl+tr)/2;
build(tl,tm,v*2);
build(tm+1,tr,v*2+1);
t[v].f = max(t[v*2].f,t[v*2+1].f);
t[v].s = min(t[v*2].s,t[v*2+1].s);
}
pii get(int l,int r,int v,int tl,int tr){
if(tl>r||tr<l)return {-1,mod};
if(l<=tl&&tr<=r){
return t[v];
}
int tm = (tl + tr)/2;
pii x,y;
x = get(l,r,v*2,tl,tm);
y = get(l,r,v*2+1,tm+1,tr);
pii val;
val.f = max(x.f,y.f);
val.s = min(x.s,y.s);
return val;
}
void solve(){
int n,q,m,k;
cin>>n>>m>>q;
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
for(int i = 1;i<=m;i++){
int a;
cin>>a;
vis[i] = a;
vis1[a] = i;
}
if(n<=2000){
while (q--)
{
int l , r;
cin>>l>>r;
/*for (int i = 0; i < n + 1; i++){
dist[i] = mod;
}*/
vector<int>dist(n+1);
queue<pii>q;
q.push({vis[l],-1});
dist[vis[l]] = 1;
vector<int>pred(n+1);
while(!q.empty()){
auto [x,pr] = q.front();
q.pop();
for(auto to:v[x]){
if(to!=pr){
dist[to] = dist[x] + 1;
pred[to] = x;
q.push({to,x});
}
}
}
vector<int>g(n+1);
for(int i = l + 1;i<=r;i++){
int y = vis[i];
while(y!=vis[l]){
g[y] = 1;
y = pred[y];
}
}
int cnt = 0;
for(int i = 1;i<=n;i++)cnt+=g[i];
cout<<cnt + 1<<"\n";
}
}
else{
build(1,m,1);
while(q--){
int l,r;
cin>>l>>r;
pii x = get(l,r,1,1,m);
cout<<x.f - x.s + 1<<"\n";
}
}
}
signed main()
{
// freopen("seq.in", "r", stdin);
// freopen("seq.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int tt=1;//cin>>tt;
while(tt--)solve();
}
Compilation message
tourism.cpp: In function 'void solve()':
tourism.cpp:67:12: warning: unused variable 'k' [-Wunused-variable]
67 | int n,q,m,k;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
4 ms |
16220 KB |
Output is correct |
3 |
Correct |
4 ms |
16220 KB |
Output is correct |
4 |
Correct |
5 ms |
16220 KB |
Output is correct |
5 |
Correct |
5 ms |
16220 KB |
Output is correct |
6 |
Correct |
5 ms |
16220 KB |
Output is correct |
7 |
Correct |
5 ms |
16220 KB |
Output is correct |
8 |
Correct |
5 ms |
16216 KB |
Output is correct |
9 |
Correct |
6 ms |
16220 KB |
Output is correct |
10 |
Correct |
7 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16180 KB |
Output is correct |
12 |
Correct |
7 ms |
16220 KB |
Output is correct |
13 |
Correct |
7 ms |
16216 KB |
Output is correct |
14 |
Correct |
8 ms |
16220 KB |
Output is correct |
15 |
Correct |
10 ms |
16188 KB |
Output is correct |
16 |
Correct |
11 ms |
16220 KB |
Output is correct |
17 |
Correct |
10 ms |
16364 KB |
Output is correct |
18 |
Correct |
8 ms |
16216 KB |
Output is correct |
19 |
Correct |
8 ms |
16216 KB |
Output is correct |
20 |
Correct |
9 ms |
16220 KB |
Output is correct |
21 |
Correct |
6 ms |
16196 KB |
Output is correct |
22 |
Correct |
5 ms |
16220 KB |
Output is correct |
23 |
Correct |
5 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
16220 KB |
Output is correct |
26 |
Correct |
5 ms |
16412 KB |
Output is correct |
27 |
Correct |
4 ms |
16216 KB |
Output is correct |
28 |
Correct |
6 ms |
16220 KB |
Output is correct |
29 |
Correct |
5 ms |
16220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
4 ms |
16220 KB |
Output is correct |
3 |
Correct |
4 ms |
16220 KB |
Output is correct |
4 |
Correct |
5 ms |
16220 KB |
Output is correct |
5 |
Correct |
5 ms |
16220 KB |
Output is correct |
6 |
Correct |
5 ms |
16220 KB |
Output is correct |
7 |
Correct |
5 ms |
16220 KB |
Output is correct |
8 |
Correct |
5 ms |
16216 KB |
Output is correct |
9 |
Correct |
6 ms |
16220 KB |
Output is correct |
10 |
Correct |
7 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16180 KB |
Output is correct |
12 |
Correct |
7 ms |
16220 KB |
Output is correct |
13 |
Correct |
7 ms |
16216 KB |
Output is correct |
14 |
Correct |
8 ms |
16220 KB |
Output is correct |
15 |
Correct |
10 ms |
16188 KB |
Output is correct |
16 |
Correct |
11 ms |
16220 KB |
Output is correct |
17 |
Correct |
10 ms |
16364 KB |
Output is correct |
18 |
Correct |
8 ms |
16216 KB |
Output is correct |
19 |
Correct |
8 ms |
16216 KB |
Output is correct |
20 |
Correct |
9 ms |
16220 KB |
Output is correct |
21 |
Correct |
6 ms |
16196 KB |
Output is correct |
22 |
Correct |
5 ms |
16220 KB |
Output is correct |
23 |
Correct |
5 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
16220 KB |
Output is correct |
26 |
Correct |
5 ms |
16412 KB |
Output is correct |
27 |
Correct |
4 ms |
16216 KB |
Output is correct |
28 |
Correct |
6 ms |
16220 KB |
Output is correct |
29 |
Correct |
5 ms |
16220 KB |
Output is correct |
30 |
Correct |
43 ms |
16472 KB |
Output is correct |
31 |
Correct |
58 ms |
16476 KB |
Output is correct |
32 |
Correct |
93 ms |
16472 KB |
Output is correct |
33 |
Correct |
90 ms |
16476 KB |
Output is correct |
34 |
Correct |
86 ms |
16472 KB |
Output is correct |
35 |
Correct |
152 ms |
16492 KB |
Output is correct |
36 |
Correct |
159 ms |
16496 KB |
Output is correct |
37 |
Correct |
162 ms |
16472 KB |
Output is correct |
38 |
Correct |
1420 ms |
16476 KB |
Output is correct |
39 |
Correct |
1421 ms |
16724 KB |
Output is correct |
40 |
Correct |
1428 ms |
16492 KB |
Output is correct |
41 |
Correct |
4296 ms |
16480 KB |
Output is correct |
42 |
Correct |
3900 ms |
16740 KB |
Output is correct |
43 |
Correct |
4189 ms |
16492 KB |
Output is correct |
44 |
Correct |
670 ms |
16724 KB |
Output is correct |
45 |
Correct |
556 ms |
16724 KB |
Output is correct |
46 |
Correct |
642 ms |
16484 KB |
Output is correct |
47 |
Correct |
1616 ms |
16492 KB |
Output is correct |
48 |
Correct |
1328 ms |
16492 KB |
Output is correct |
49 |
Correct |
2041 ms |
16732 KB |
Output is correct |
50 |
Correct |
32 ms |
16476 KB |
Output is correct |
51 |
Correct |
32 ms |
16552 KB |
Output is correct |
52 |
Correct |
33 ms |
16544 KB |
Output is correct |
53 |
Correct |
33 ms |
16476 KB |
Output is correct |
54 |
Correct |
33 ms |
16476 KB |
Output is correct |
55 |
Correct |
33 ms |
16476 KB |
Output is correct |
56 |
Correct |
5 ms |
16220 KB |
Output is correct |
57 |
Correct |
31 ms |
16516 KB |
Output is correct |
58 |
Correct |
50 ms |
16476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16216 KB |
Output is correct |
2 |
Correct |
4 ms |
16220 KB |
Output is correct |
3 |
Correct |
6 ms |
16220 KB |
Output is correct |
4 |
Correct |
53 ms |
22612 KB |
Output is correct |
5 |
Correct |
51 ms |
21340 KB |
Output is correct |
6 |
Correct |
54 ms |
23864 KB |
Output is correct |
7 |
Correct |
72 ms |
24184 KB |
Output is correct |
8 |
Correct |
73 ms |
24144 KB |
Output is correct |
9 |
Correct |
74 ms |
24148 KB |
Output is correct |
10 |
Correct |
74 ms |
24580 KB |
Output is correct |
11 |
Correct |
73 ms |
24304 KB |
Output is correct |
12 |
Correct |
65 ms |
24148 KB |
Output is correct |
13 |
Correct |
64 ms |
24144 KB |
Output is correct |
14 |
Correct |
65 ms |
24148 KB |
Output is correct |
15 |
Correct |
35 ms |
19544 KB |
Output is correct |
16 |
Correct |
61 ms |
23724 KB |
Output is correct |
17 |
Correct |
2126 ms |
16508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Incorrect |
21 ms |
20572 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
4 ms |
16220 KB |
Output is correct |
3 |
Correct |
6 ms |
16216 KB |
Output is correct |
4 |
Incorrect |
63 ms |
23412 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
4 ms |
16220 KB |
Output is correct |
3 |
Correct |
4 ms |
16220 KB |
Output is correct |
4 |
Correct |
5 ms |
16220 KB |
Output is correct |
5 |
Correct |
5 ms |
16220 KB |
Output is correct |
6 |
Correct |
5 ms |
16220 KB |
Output is correct |
7 |
Correct |
5 ms |
16220 KB |
Output is correct |
8 |
Correct |
5 ms |
16216 KB |
Output is correct |
9 |
Correct |
6 ms |
16220 KB |
Output is correct |
10 |
Correct |
7 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16180 KB |
Output is correct |
12 |
Correct |
7 ms |
16220 KB |
Output is correct |
13 |
Correct |
7 ms |
16216 KB |
Output is correct |
14 |
Correct |
8 ms |
16220 KB |
Output is correct |
15 |
Correct |
10 ms |
16188 KB |
Output is correct |
16 |
Correct |
11 ms |
16220 KB |
Output is correct |
17 |
Correct |
10 ms |
16364 KB |
Output is correct |
18 |
Correct |
8 ms |
16216 KB |
Output is correct |
19 |
Correct |
8 ms |
16216 KB |
Output is correct |
20 |
Correct |
9 ms |
16220 KB |
Output is correct |
21 |
Correct |
6 ms |
16196 KB |
Output is correct |
22 |
Correct |
5 ms |
16220 KB |
Output is correct |
23 |
Correct |
5 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
16220 KB |
Output is correct |
26 |
Correct |
5 ms |
16412 KB |
Output is correct |
27 |
Correct |
4 ms |
16216 KB |
Output is correct |
28 |
Correct |
6 ms |
16220 KB |
Output is correct |
29 |
Correct |
5 ms |
16220 KB |
Output is correct |
30 |
Correct |
43 ms |
16472 KB |
Output is correct |
31 |
Correct |
58 ms |
16476 KB |
Output is correct |
32 |
Correct |
93 ms |
16472 KB |
Output is correct |
33 |
Correct |
90 ms |
16476 KB |
Output is correct |
34 |
Correct |
86 ms |
16472 KB |
Output is correct |
35 |
Correct |
152 ms |
16492 KB |
Output is correct |
36 |
Correct |
159 ms |
16496 KB |
Output is correct |
37 |
Correct |
162 ms |
16472 KB |
Output is correct |
38 |
Correct |
1420 ms |
16476 KB |
Output is correct |
39 |
Correct |
1421 ms |
16724 KB |
Output is correct |
40 |
Correct |
1428 ms |
16492 KB |
Output is correct |
41 |
Correct |
4296 ms |
16480 KB |
Output is correct |
42 |
Correct |
3900 ms |
16740 KB |
Output is correct |
43 |
Correct |
4189 ms |
16492 KB |
Output is correct |
44 |
Correct |
670 ms |
16724 KB |
Output is correct |
45 |
Correct |
556 ms |
16724 KB |
Output is correct |
46 |
Correct |
642 ms |
16484 KB |
Output is correct |
47 |
Correct |
1616 ms |
16492 KB |
Output is correct |
48 |
Correct |
1328 ms |
16492 KB |
Output is correct |
49 |
Correct |
2041 ms |
16732 KB |
Output is correct |
50 |
Correct |
32 ms |
16476 KB |
Output is correct |
51 |
Correct |
32 ms |
16552 KB |
Output is correct |
52 |
Correct |
33 ms |
16544 KB |
Output is correct |
53 |
Correct |
33 ms |
16476 KB |
Output is correct |
54 |
Correct |
33 ms |
16476 KB |
Output is correct |
55 |
Correct |
33 ms |
16476 KB |
Output is correct |
56 |
Correct |
5 ms |
16220 KB |
Output is correct |
57 |
Correct |
31 ms |
16516 KB |
Output is correct |
58 |
Correct |
50 ms |
16476 KB |
Output is correct |
59 |
Correct |
4 ms |
16216 KB |
Output is correct |
60 |
Correct |
4 ms |
16220 KB |
Output is correct |
61 |
Correct |
6 ms |
16220 KB |
Output is correct |
62 |
Correct |
53 ms |
22612 KB |
Output is correct |
63 |
Correct |
51 ms |
21340 KB |
Output is correct |
64 |
Correct |
54 ms |
23864 KB |
Output is correct |
65 |
Correct |
72 ms |
24184 KB |
Output is correct |
66 |
Correct |
73 ms |
24144 KB |
Output is correct |
67 |
Correct |
74 ms |
24148 KB |
Output is correct |
68 |
Correct |
74 ms |
24580 KB |
Output is correct |
69 |
Correct |
73 ms |
24304 KB |
Output is correct |
70 |
Correct |
65 ms |
24148 KB |
Output is correct |
71 |
Correct |
64 ms |
24144 KB |
Output is correct |
72 |
Correct |
65 ms |
24148 KB |
Output is correct |
73 |
Correct |
35 ms |
19544 KB |
Output is correct |
74 |
Correct |
61 ms |
23724 KB |
Output is correct |
75 |
Correct |
2126 ms |
16508 KB |
Output is correct |
76 |
Correct |
4 ms |
16220 KB |
Output is correct |
77 |
Incorrect |
21 ms |
20572 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |