Submission #888705

#TimeUsernameProblemLanguageResultExecution timeMemory
888705vjudge1Passport (JOI23_passport)C++17
0 / 100
618 ms1048576 KiB
#include <bits/stdc++.h> #define int long long #define all(x) x.begin(),x.end() #define ff first #define ss second #define pb push_back using namespace std; const int N=1e5+5; vector <int> g[N]; signed main(){ ios_base::sync_with_stdio(); cin.tie(0); int n; cin>>n; vector <int> l(n),r(n); for(int i=0;i<n;i++)cin>>l[i]>>r[i]; for(int i=0;i<n;i++){ for(int j=l[i];j<=r[i];j++){ g[i+1].pb(j); } } int q;cin>>q; set <pair <int,int> > st; vector <int> ans; while(q--){ int x;cin>>x; vector <int> d(n+1,1e18),p(n+1); st.insert({0,x}); d[x]=0; while(!st.empty()){ int v=st.begin()->second; st.erase(st.begin()); for(auto to : g[v]){ if(d[to]>d[v]){ p[to]=v; st.erase({d[to],to}); d[to]=d[v]+1; st.insert({d[to],to}); } } } int mx=0; set <int> s; for(int i=1;i<=n;i++){ mx=max(mx,d[i]); if(d[i]==1e18)break; int curv=i; while(curv!=x){ curv=p[curv]; s.insert(curv); //cout<<curv<<" "; } //cout<<endl; } if(mx==1e18)ans.pb(-1); else ans.pb(s.size()); } for(auto x : ans)cout<<x<<"\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...