Submission #851522

#TimeUsernameProblemLanguageResultExecution timeMemory
851522willychanPassport (JOI23_passport)C++14
6 / 100
1084 ms251008 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; //#include<bits/extc++.h> //__gnu_pbds const int N = 200005; #define int ll struct segtree{ vector<int> minl; vector<int> maxr; int n; void init(int S){ minl.resize(4*S,1e9); maxr.resize(4*S,-1e9); n = S; } void set(int ind,int l,int r,pair<int,int> v,int t){ if(r-l==1){ minl[t] = min(minl[t],v.first); maxr[t] = max(maxr[t],v.second); return; } int mid = (l+r)/2; if(ind<mid) set(ind,l,mid,v,2*t); if(ind>=mid) set(ind,mid,r,v,2*t+1); minl[t] = min(minl[2*t],minl[2*t+1]); maxr[t] = max(maxr[2*t],maxr[2*t+1]); } pair<int,int> result(int l,int r,int L,int R,int t){ if(l==L && r==R) return {minl[t],maxr[t]}; int M = (L+R)/2; if(r<=M) return result(l,r,L,M,2*t); if(l>=M) return result(l,r,M,R,2*t+1); pair<int,int> ff = result(l,M,L,M,2*t); pair<int,int> ss = result(M,r,M,R,2*t+1); return {min(ff.first,ss.first),max(ff.second,ss.second)}; } }; signed main(){ ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n;cin>>n; segtree seg[20]; for(int i=0;i<20;i++) seg[i].init(n); for(int i=1;i<=n;i++){ int l,r;cin>>l>>r; seg[0].set(i,1,n+1,make_pair(l,r),1); } for(int j=1;j<20;j++){ for(int i=1;i<=n;i++){ pair<int,int> ran = seg[j-1].result(i,i+1,1,n+1,1); seg[j].set(i,1,n+1,seg[j-1].result(ran.first,ran.second+1,1,n+1,1),1); } } int q;cin>>q; while(q--){ int x;cin>>x; pair<int,int> ran = seg[19].result(x,x+1,1,n+1,1); if(ran.first!=1 || ran.second!=n){ cout<<-1<<"\n"; continue; } pair<int,int> re = {x,x}; int ans = 0; for(int j=19;j>=0;j--){ pair<int,int> ra = seg[j].result(re.first,re.second+1,1,n+1,1); if(ra.first!=1 || ra.second!=n){ re = ra; ans+=(1<<j); } } cout<<ans+1<<"\n" ; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...