Submission #851542

#TimeUsernameProblemLanguageResultExecution timeMemory
851542willychanPassport (JOI23_passport)C++14
0 / 100
3 ms9820 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; //#include<bits/extc++.h> //__gnu_pbds const int N = 2e5+5; vector<int> side[N]; vector<int> rside[N]; int main(){ ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n;cin>>n; for(int i=1;i<=n;i++){ int l,r;cin>>l>>r; for(int j=l;j<=r;j++){ side[j].push_back(i); rside[i].push_back(j); } } int dis1[n+1]={0}; int disn[n+1]={0}; dis1[1]=1; disn[n]=1; queue<int> q; q.push(1); while(q.size()){ int a = q.front(); q.pop(); for(auto i : side[a]){ if(dis1[i]) continue; dis1[i] = dis1[a]+1; q.push(i); } } queue<int> q2; q2.push(n); while(q2.size()){ int a = q2.front(); q2.pop(); for(auto i : side[a]){ if(disn[i]) continue; disn[i] = disn[a]+1; q2.push(i); } } int Q;cin>>Q; int rans[n+1] = {0}; for(int i=1;i<=n;i++) { if(!dis1[i]) dis1[i] = 1e8; if(!disn[i]) disn[i] = 1e8; rans[i] = dis1[i]+disn[i]; } while(Q--){ int x;cin>>x; queue<int> qx; qx.push(x); vector<int> d(n+1,0); d[x]=1; while(qx.size()){ int a = qx.front(); qx.pop(); for(auto i : rside[a]){ if(d[i]) continue; d[i]= d[a]+1; qx.push(i); } } ll ans = 1e9; for(int i=1;i<=n;i++){ if(d[i]==0) continue; ans= min(ans,1LL*rans[i]+d[i]-3); } cout<<ans<<"\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...