#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
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 |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
450 ms |
18136 KB |
Output is correct |
5 |
Correct |
274 ms |
16808 KB |
Output is correct |
6 |
Correct |
274 ms |
17072 KB |
Output is correct |
7 |
Correct |
259 ms |
18220 KB |
Output is correct |
8 |
Correct |
95 ms |
12120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
4 ms |
596 KB |
Output is correct |
20 |
Correct |
5 ms |
596 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
4 ms |
596 KB |
Output is correct |
20 |
Correct |
5 ms |
596 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
4 ms |
628 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
450 ms |
18136 KB |
Output is correct |
5 |
Correct |
274 ms |
16808 KB |
Output is correct |
6 |
Correct |
274 ms |
17072 KB |
Output is correct |
7 |
Correct |
259 ms |
18220 KB |
Output is correct |
8 |
Correct |
95 ms |
12120 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
5 ms |
596 KB |
Output is correct |
29 |
Correct |
4 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
4 ms |
628 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
596 KB |
Output is correct |
40 |
Correct |
495 ms |
18596 KB |
Output is correct |
41 |
Correct |
433 ms |
18160 KB |
Output is correct |
42 |
Correct |
336 ms |
17840 KB |
Output is correct |
43 |
Correct |
322 ms |
18248 KB |
Output is correct |
44 |
Correct |
327 ms |
18420 KB |
Output is correct |
45 |
Correct |
292 ms |
16316 KB |
Output is correct |
46 |
Correct |
120 ms |
6740 KB |
Output is correct |
47 |
Correct |
349 ms |
18168 KB |
Output is correct |
48 |
Correct |
266 ms |
16572 KB |
Output is correct |