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>
#define ll long long
using namespace std;
int N,K,L[200005],R[200005],dist[200005],d1[200005],d2[200005];
bool ubacen[2505];
struct bfss{
int gde,kol;
}pp,tren;
bool cmp(bfss a,bfss b){
return a.kol<b.kol;
}
vector<bfss> V;
queue<bfss> Q,C;
struct slog{
int r,gde,l,pn;
} st[525000],niz[200005];
bool cmp2(slog a,slog b){
return a.l<b.l;
}
slog opr(slog a,slog b){
if(a.r>b.r)
return a;
return b;
}
void update(int gde,int sta,int gde2=-1,int pn=-1){
gde+=K-1;
st[gde].r=sta;
if(gde2!=-1)
st[gde].gde=gde2;
if(pn!=-1)
st[gde].pn=pn;
gde/=2;
while(gde){
st[gde]=opr(st[gde*2],st[gde*2+1]);
gde/=2;
}
return;
}
slog get(int gde,int lg,int dg,int lc,int dc){
if(lg==lc and dg==dc)
return st[gde];
int sred=(lc+dc)/2;
if(dg<=sred)
return get(gde*2,lg,dg,lc,sred);
if(lg>sred)
return get(gde*2+1,lg,dg,sred+1,dc);
slog a,b;
a=get(gde*2,lg,sred,lc,sred);
b=get(gde*2+1,sred+1,dg,sred+1,dc);
return opr(a,b);
}
void BFS(){
for(int i=1;i<=N;i++)
update(i,niz[i].r,niz[i].gde,i);
for(int i=1;i<=N;i++)
dist[i]=-1;
if(Q.size()==0){
Q.push(C.front());
C.pop();
}
while(C.size()){
if(C.front().kol!=Q.front().kol)
break;
Q.push(C.front());
C.pop();
}
int dg,gg,sred,poz;
while(Q.size()){
tren=Q.front();
Q.pop();
if(dist[tren.gde]!=-1)
continue;
dist[tren.gde]=tren.kol;
dg=1;
gg=N;
while(dg<=gg){
sred=(dg+gg)/2;
if(niz[sred].l<=tren.gde){
poz=sred;
dg=sred+1;
}
else
gg=sred-1;
}
while(true){
// cout<<1<<" "<<poz<<endl;
slog x=get(1,1,poz,1,K);
/* cout<<x.pn<<endl;
for(int i=1;i<2*K;i++)
cout<<st[i].r<<" ";
cout<<endl;
for(int i=1;i<2*K;i++)
cout<<st[i].pn<<" ";
cout<<endl;*/
if(x.r<tren.gde)
break;
// cout<<x.r<<endl;
pp.gde=x.gde;
pp.kol=tren.kol+1;
Q.push(pp);
update(x.pn,-1);
}
while(C.size()){
if(C.front().kol>tren.kol+1)
break;
Q.push(C.front());
C.pop();
}
}
while(C.size())
C.pop();
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>N;
K=1;
while(K<N)
K*=2;
for(int i=1;i<=N;i++)
cin>>L[i]>>R[i];
for(int i=1;i<=N;i++){
niz[i].l=L[i];
niz[i].gde=i;
niz[i].r=R[i];
}
sort(niz+1,niz+1+N,cmp2);
for(int i=1;i<=N;i++){
if(L[i]!=1)
continue;
pp.gde=i;
pp.kol=0;
Q.push(pp);
}
BFS();
for(int i=1;i<=N;i++)
d1[i]=dist[i];
for(int i=1;i<=N;i++){
if(R[i]!=N)
continue;
pp.gde=i;
pp.kol=0;
Q.push(pp);
}
BFS();
for(int i=1;i<=N;i++)
d2[i]=dist[i];
for(int i=1;i<=N;i++){
if(d1[i]==-1 or d2[i]==-1)
continue;
pp.gde=i;
pp.kol=d1[i]+d2[i];
V.push_back(pp);
}
if(V.size()){
sort(V.begin(),V.end(),cmp);
for(int i=0;i<V.size();i++)
C.push(V[i]);
BFS();
}
else
for(int i=1;i<=N;i++)
dist[i]=-1;
int Q,x;
cin>>Q;
while(Q--){
cin>>x;
if(d1[x]==-1 or d2[x]==-1)
cout<<"-1\n";
else
cout<<dist[x]+1<<"\n";
}
return 0;
}
Compilation message (stderr)
passport.cpp: In function 'int main()':
passport.cpp:159:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bfss>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for(int i=0;i<V.size();i++)
| ~^~~~~~~~~
passport.cpp: In function 'void BFS()':
passport.cpp:67:19: warning: 'poz' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | int dg,gg,sred,poz;
| ^~~
# | 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... |