Submission #890439

#TimeUsernameProblemLanguageResultExecution timeMemory
890439Darren0724Passport (JOI23_passport)C++17
6 / 100
646 ms514940 KiB
#include <bits/stdc++.h> using namespace std; #define LCBorz ios_base::sync_with_stdio(false); cin.tie(0); #define all(x) x.begin(), x.end() #define endl '\n' const int K=18; const int INF=1e9; const int mod=998244353; struct sparse_table_min{ vector<int> v[K]; void init(vector<int> &a){ int n=a.size(); for(int i=0;i<K;i++){ v[i].resize(n); } for(int i=0;i<n;i++){ v[0][i]=a[i]; } for(int j=1;j<K;j++){ int p=(1<<j); for(int i=0;i+p<=n;i++){ v[j][i]=min(v[j-1][i],v[j-1][i+(p>>1)]); } } } int ask(int l,int r){ int p=__lg(r-l+1); return min(v[p][l],v[p][r-(1<<p)+1]); } }; struct sparse_table_max{ vector<int> v[K]; void init(vector<int> &a){ int n=a.size(); for(int i=0;i<K;i++){ v[i].resize(n); } for(int i=0;i<n;i++){ v[0][i]=a[i]; } for(int j=1;j<K;j++){ int p=(1<<j); for(int i=0;i+p<=n;i++){ v[j][i]=max(v[j-1][i],v[j-1][i+(p>>1)]); } } } int ask(int l,int r){ int p=__lg(r-l+1); return max(v[p][l],v[p][r-(1<<p)+1]); } }; void solve(){ int n;cin>>n; vector<int> b(n+1),c(n+1),b1(n+1),c1(n+1); for(int i=1;i<=n;i++){ cin>>b[i]>>c[i]; b1[i]=b[i]; c1[i]=c[i]; } vector<sparse_table_min> st1(K); vector<sparse_table_max> st2(K); for(int i=0;i<K;i++){ st1[i].init(b); st2[i].init(c); for(int j=1;j<=n;j++){ int l=b[j],r=c[j]; b[j]=st1[i].ask(l,r); c[j]=st2[i].ask(l,r); } } vector<int> ans(n+1); for(int i=1;i<=n;i++){ if(b[i]!=1||c[i]!=n){ ans[i]=-1; continue; } int l=b1[i],r=c1[i]; for(int j=K-1;j>=0;j--){ int l1=st1[j].ask(l,r); int r1=st2[j].ask(l,r); if(r1==n&&l1==1){ continue; } l=l1,r=r1; ans[i]+=(1<<j); } if(l==1&&r==n){ ans[i]--; } ans[i]+=2; } int q;cin>>q; for(int i=0;i<q;i++){ int p;cin>>p; cout<<ans[p]<<endl; } } int32_t main() { LCBorz; int t=1;//cin>>t; while(t--){ solve(); } 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...