Submission #828976

#TimeUsernameProblemLanguageResultExecution timeMemory
828976CSQ31Passport (JOI23_passport)C++17
100 / 100
244 ms16080 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) #define all(a) a.begin(),a.end() #define lb lower_bound #define ub upper_bound #define owo ios_base::sync_with_stdio(0);cin.tie(0); #define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\ debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> PII; typedef pair<int,int> pii; typedef vector<vector<int>> vii; typedef vector<vector<ll>> VII; ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);} const int MAXN = 2e5+5; int a[MAXN],b[MAXN],c[MAXN]; int dist[4][MAXN]; int mn[4*MAXN],mx[4*MAXN]; void build(int v,int l,int r){ if(l==r){ mn[v] = a[l]; mx[v] = b[l]; return; } int tm = (l+r)/2; build(2*v,l,tm); build(2*v+1,tm+1,r); mn[v] = min(mn[2*v],mn[2*v+1]); mx[v] = max(mx[2*v],mx[2*v+1]); } pii cur; vector<int>add; void getmx(int v,int l,int r,int tl,int tr){ if(l>r)return; if(mx[v] < cur.se)return; if(tl==tr){ if(dist[cur.fi][l] > dist[cur.fi][cur.se]+1){ dist[cur.fi][l] = dist[cur.fi][cur.se]+1; add.pb(l); } mx[v] = -1; mn[v] = 1e9; return; } int tm = (tl+tr)/2; if(mx[2*v] >= cur.se)getmx(2*v,l,min(r,tm),tl,tm); if(mx[2*v+1] >= cur.se)getmx(2*v+1,max(l,tm+1),r,tm+1,tr); mn[v] = min(mn[2*v],mn[2*v+1]); mx[v] = max(mx[2*v],mx[2*v+1]); } void getmn(int v,int l,int r,int tl,int tr){ if(l>r)return; if(mn[v] > cur.se)return; if(tl==tr){ if(dist[cur.fi][l] > dist[cur.fi][cur.se]+1){ dist[cur.fi][l] = dist[cur.fi][cur.se]+1; add.pb(l); } mx[v] = -1; mn[v] = 1e9; return; } int tm = (tl+tr)/2; if(mn[2*v] <= cur.se)getmn(2*v,l,min(r,tm),tl,tm); if(mn[2*v+1] <= cur.se)getmn(2*v+1,max(l,tm+1),r,tm+1,tr); mn[v] = min(mn[2*v],mn[2*v+1]); mx[v] = max(mx[2*v],mx[2*v+1]); } int main() { owo int n; cin>>n; for(int i=1;i<=n;i++)dist[0][i] = dist[1][i] = dist[2][i] = dist[3][i] = 1e9; for(int i=1;i<=n;i++){ cin>>a[i]>>b[i]; if(a[i]==1)c[i]++; if(b[i]==n)c[i]+=2; dist[c[i]][i] = 1; } queue<int>q; build(1,1,n); for(int i=1;i<=n;i++){if(c[i]==1)q.push(i);} while(!q.empty()){ int v = q.front(); q.pop(); add.clear(); cur = {1,v}; getmx(1,1,v,1,n); getmn(1,v,n,1,n); for(int x:add)q.push(x); } build(1,1,n); for(int i=1;i<=n;i++){if(c[i]==2)q.push(i);} while(!q.empty()){ int v = q.front(); q.pop(); add.clear(); cur = {2,v}; getmx(1,1,v,1,n); getmn(1,v,n,1,n); for(int x:add)q.push(x); } ///// priority_queue<pii,vector<pii>,greater<pii>>pq; for(int i=1;i<=n;i++){ dist[3][i] = min(dist[3][i],dist[1][i]+dist[2][i]-1); if(dist[3][i] <= n)pq.push({dist[3][i],i}); } build(1,1,n); while(!pq.empty()){ int d = pq.top().fi; int v = pq.top().se; pq.pop(); if(d!=dist[3][v])continue; add.clear(); cur = {3,v}; getmx(1,1,v,1,n); getmn(1,v,n,1,n); for(int x:add)pq.push({dist[3][x],x}); } int Q; cin>>Q; while(Q--){ int i; cin>>i; if(dist[3][i]==1e9)cout<<-1<<'\n'; else cout<<dist[3][i]<<'\n'; } }
#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...