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,L[200005],R[200005],dist[200005],d1[200005],d2[200005];
bool ubacen[2505];
struct slog{
int gde,kol;
}pp,tren;
bool cmp(slog a,slog b){
return a.kol<b.kol;
}
vector<slog> V;
queue<slog> Q,C;
void BFS(){
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();
}
while(Q.size()){
tren=Q.front();
Q.pop();
if(dist[tren.gde]!=-1)
continue;
dist[tren.gde]=tren.kol;
for(int i=1;i<=N;i++){
if(tren.gde<L[i])
continue;
if(tren.gde>R[i])
continue;
pp.gde=i;
pp.kol=tren.kol+1;
while(C.size()){
if(C.front().kol>pp.kol)
break;
Q.push(C.front());
C.pop();
}
Q.push(pp);
}
}
while(C.size())
C.pop();
return;
}
int main(){
cin>>N;
for(int i=1;i<=N;i++)
cin>>L[i]>>R[i];
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++)
cout<<d1[i]<<" ";
cout<<endl;
for(int i=1;i<=N;i++)
cout<<d2[i]<<" ";
cout<<endl;*/
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:92:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<slog>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i=0;i<V.size();i++)
| ~^~~~~~~~~
# | 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... |