Submission #936733

#TimeUsernameProblemLanguageResultExecution timeMemory
936733velislavgarkovPassport (JOI23_passport)C++14
100 / 100
786 ms92776 KiB
#include <iostream> #include <vector> #include <queue> #include <cstring> using namespace std; #define endl '\n' const int MAXN=2e5+10; vector <pair <int,int> > v[5*MAXN]; int l[MAXN], r[MAXN], c[MAXN]; int dist[5*MAXN]; priority_queue <pair <int,int> > q; int n; void build(int node, int l, int r) { if (l==r) { v[4*n+l].push_back({node,0}); return; } int mid=(l+r)/2; v[node*2].push_back({node,0}); v[node*2+1].push_back({node,0}); build(node*2,l,mid); build(node*2+1,mid+1,r); } void find_passports(int node, int l, int r, int ql, int qr, int ver) { if (ql>r || qr<l) return; if (l>=ql && r<=qr) { v[node].push_back({ver,1}); return; } int mid=(l+r)/2; find_passports(node*2,l,mid,ql,qr,ver); find_passports(node*2+1,mid+1,r,ql,qr,ver); } void dejkstra(int start) { memset(dist,-1,sizeof(dist)); dist[start]=0; q.push({0,start}); while (!q.empty()) { int x=q.top().second; if (-q.top().first!=dist[x]) { q.pop(); continue; } q.pop(); for (auto i:v[x]) { if (dist[i.first]==-1 || dist[x]+i.second<dist[i.first]) { dist[i.first]=dist[x]+i.second; q.push({-dist[i.first],i.first}); } } } } int main () { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i=1;i<=n;i++) cin >> l[i] >> r[i]; build(1,1,n); for (int i=1;i<=n;i++) { c[i]=-1; find_passports(1,1,n,l[i],r[i],4*n+i); } dejkstra(4*n+1); dist[4*n+1]=1; for (int i=1;i<=n;i++) { if (dist[4*n+i]==-1) continue; c[i]=dist[4*n+i]; } dejkstra(5*n); dist[5*n]=1; for (int i=1;i<=n;i++) { if (dist[4*n+i]==-1 || c[i]==-1) { c[i]=-1; continue; } c[i]+=dist[4*n+i]; } for (int i=1;i<=n;i++) { if (c[i]==-1) continue; v[5*n+1].push_back({4*n+i,c[i]}); } dejkstra(5*n+1); int q, t; cin >> q; for (int i=0;i<q;i++) { cin >> t; cout << max(-1,dist[4*n+t]-1) << endl; } 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...