This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N=200050;
int n,q,x,l[N],r[N],dist[3][N<<2],final[3][N<<2],rrl[N],mx;
vector<pair<int,int>> adj[N<<2],radj[N<<2];
priority_queue<pair<int,int>> pq;
void pre(int id,int l,int r){
mx=max(id,mx);
//cout<<id<<" "<<l<<" "<<r<<"\n";
if (l==r){
rrl[l]=id;
return;
}
int m=(l+r)/2;
pre(id*2,l,m);
pre(id*2+1,m+1,r);
adj[id].push_back({id*2,0});
adj[id].push_back({id*2+1,0});
radj[id*2].push_back({id,0});
radj[id*2+1].push_back({id,0});
}
void build(int id,int l,int r,int ql,int qr,int rt,int dist){
if (l>qr||r<ql) return;
if (ql<=l&&qr>=r){
//cout<<rt<<"<->"<<id<<"\n";
adj[rt].push_back({id,dist});
radj[id].push_back({rt,dist});
return;
}
int m=(l+r)/2;
build(id*2,l,m,ql,qr,rt,dist);
build(id*2+1,m+1,r,ql,qr,rt,dist);
}
void rdij(int id,int st){
while (pq.size()) pq.pop();
for (int i=0;i<N*4;i++) dist[id][i]=1e8,final[id][i]=0;
dist[id][st]=0;
pq.push({0,st});
while (pq.size()){
int cnde=pq.top().second;
pq.pop();
if (final[id][cnde]) continue;
final[id][cnde]=1;
//cout<<cnde<<": "<<dist[id][cnde]<<"\n";
for (auto i:radj[cnde]){
if (dist[id][i.first]>dist[id][cnde]+i.second){
dist[id][i.first]=dist[id][cnde]+i.second;
//cout<<cnde<<"->"<<i.first<<"\n";
pq.push({-dist[id][i.first],i.first});
}
}
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n;
for (int i=1;i<=n;i++) cin>>l[i]>>r[i];
pre(1,1,n);
for (int i=1;i<=n;i++){
build(1,1,n,l[i],r[i],rrl[i],1);
if (l[i]==1) radj[mx+1].push_back({rrl[i],0});
if (r[i]==n) radj[mx+2].push_back({rrl[i],0});
}
rdij(0,mx+1);
rdij(1,mx+2);
for (int i=1;i<=n;i++){
//cout<<i<<": "<<dist[0][rrl[i]]<<" "<<dist[1][rrl[i]]<<"\n";
radj[0].push_back({rrl[i],dist[0][rrl[i]]+dist[1][rrl[i]]});
adj[rrl[i]].push_back({0,dist[0][rrl[i]]+dist[1][rrl[i]]});
}
rdij(2,0);
cin>>q;
while (q--){
cin>>x;
int tmp=dist[2][rrl[x]];
cout<<(tmp>=1e8?-1:tmp+1)<<"\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |