#include<bits/stdc++.h>
using namespace std;
int N=2e5;
int inf=1e6;
vector<pair<int,int> >node[1000005];
vector<pair<int,int> >rv[1000005];
int disst[1000005];
int disen[1000005];
int alldis[1000005];
int vis[1000005];
void build(int st,int en,int i,int p){
if(st==en){
node[p+N].push_back({st,0});
return;
}
int m=(st+en)/2;
build(st,m,i*2,i);
build(m+1,en,i*2+1,i);
if(p!=-1)node[p+N].push_back({i+N,0});
}
void connect(int st,int en,int i,int l,int r,int x){
if(st>r||en<l)return;
if(st>=l&&en<=r){
if(st==en)return void(node[x].push_back({st,1}));
else return void(node[x].push_back({i+N,1}));
}
int m=(st+en)/2;
connect(st,m,i*2,l,r,x);
connect(m+1,en,i*2+1,l,r,x);
}
void djikstra_st(){
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
pq.push({0,1});
for(int i=1;i<=5*N;i++)vis[i]=0;
while(!pq.empty()){
int x=pq.top().second;
int c=pq.top().first;
pq.pop();
if(vis[x])continue;
vis[x]=1;
for(auto v:rv[x])if(c+v.second<disst[v.first])pq.push({c+v.second,v.first}),disst[v.first]=c+v.second;
}
}
void djikstra_en(){
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
pq.push({0,N});
for(int i=1;i<=5*N;i++)vis[i]=0;
while(!pq.empty()){
int x=pq.top().second;
int c=pq.top().first;
pq.pop();
if(vis[x])continue;
vis[x]=1;
for(auto v:rv[x])if(c+v.second<disen[v.first])pq.push({c+v.second,v.first}),disen[v.first]=c+v.second;
}
}
void djikstra_all(){
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
for(int i=1;i<=5*N;i++)alldis[i]=disst[i]+disen[i]-(1<i&&i<N),pq.push({alldis[i],i});
for(int i=1;i<=5*N;i++)vis[i]=0;
while(!pq.empty()){
int x=pq.top().second;
int c=pq.top().first;
pq.pop();
if(vis[x])continue;
vis[x]=1;
for(auto v:rv[x])if(c+v.second<alldis[v.first])pq.push({c+v.second,v.first}),alldis[v.first]=c+v.second;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>N;
build(1,N,1,-1);
for(int i=1;i<=N;i++){
int a,b;
cin>>a>>b;
connect(1,N,1,a,b,i);
}
for(int i=1;i<=5*N;i++)for(auto x:node[i])rv[x.first].push_back({i,x.second});
for(int i=1;i<=5*N;i++)disst[i]=disen[i]=alldis[i]=inf;
disst[1]=disen[N]=0;
djikstra_st();
djikstra_en();
//for(int i=1;i<=N;i++)cerr<<i<<" "<<disst[i]<<" "<<disen[i]<<endl;
djikstra_all();
int q;
cin>>q;
while(q--){
int x;
cin>>x;
cout<<(alldis[x]<inf?alldis[x]:-1)<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
54620 KB |
Output is correct |
2 |
Correct |
12 ms |
54692 KB |
Output is correct |
3 |
Correct |
11 ms |
54620 KB |
Output is correct |
4 |
Correct |
759 ms |
148776 KB |
Output is correct |
5 |
Correct |
480 ms |
108288 KB |
Output is correct |
6 |
Correct |
277 ms |
101880 KB |
Output is correct |
7 |
Correct |
300 ms |
97988 KB |
Output is correct |
8 |
Correct |
273 ms |
112256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
54616 KB |
Output is correct |
2 |
Correct |
13 ms |
54616 KB |
Output is correct |
3 |
Correct |
13 ms |
54620 KB |
Output is correct |
4 |
Correct |
12 ms |
54620 KB |
Output is correct |
5 |
Correct |
13 ms |
54684 KB |
Output is correct |
6 |
Correct |
12 ms |
54632 KB |
Output is correct |
7 |
Correct |
12 ms |
54620 KB |
Output is correct |
8 |
Correct |
12 ms |
54456 KB |
Output is correct |
9 |
Correct |
12 ms |
54708 KB |
Output is correct |
10 |
Correct |
14 ms |
54468 KB |
Output is correct |
11 |
Correct |
13 ms |
54616 KB |
Output is correct |
12 |
Correct |
15 ms |
54764 KB |
Output is correct |
13 |
Correct |
13 ms |
54676 KB |
Output is correct |
14 |
Correct |
12 ms |
54616 KB |
Output is correct |
15 |
Correct |
12 ms |
54792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
54616 KB |
Output is correct |
2 |
Correct |
13 ms |
54616 KB |
Output is correct |
3 |
Correct |
13 ms |
54620 KB |
Output is correct |
4 |
Correct |
12 ms |
54620 KB |
Output is correct |
5 |
Correct |
13 ms |
54684 KB |
Output is correct |
6 |
Correct |
12 ms |
54632 KB |
Output is correct |
7 |
Correct |
12 ms |
54620 KB |
Output is correct |
8 |
Correct |
12 ms |
54456 KB |
Output is correct |
9 |
Correct |
12 ms |
54708 KB |
Output is correct |
10 |
Correct |
14 ms |
54468 KB |
Output is correct |
11 |
Correct |
13 ms |
54616 KB |
Output is correct |
12 |
Correct |
15 ms |
54764 KB |
Output is correct |
13 |
Correct |
13 ms |
54676 KB |
Output is correct |
14 |
Correct |
12 ms |
54616 KB |
Output is correct |
15 |
Correct |
12 ms |
54792 KB |
Output is correct |
16 |
Correct |
17 ms |
55468 KB |
Output is correct |
17 |
Correct |
17 ms |
55388 KB |
Output is correct |
18 |
Correct |
17 ms |
55644 KB |
Output is correct |
19 |
Correct |
17 ms |
55648 KB |
Output is correct |
20 |
Correct |
15 ms |
55276 KB |
Output is correct |
21 |
Correct |
16 ms |
55388 KB |
Output is correct |
22 |
Correct |
16 ms |
55136 KB |
Output is correct |
23 |
Correct |
15 ms |
55256 KB |
Output is correct |
24 |
Correct |
15 ms |
55384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
54616 KB |
Output is correct |
2 |
Correct |
13 ms |
54616 KB |
Output is correct |
3 |
Correct |
13 ms |
54620 KB |
Output is correct |
4 |
Correct |
12 ms |
54620 KB |
Output is correct |
5 |
Correct |
13 ms |
54684 KB |
Output is correct |
6 |
Correct |
12 ms |
54632 KB |
Output is correct |
7 |
Correct |
12 ms |
54620 KB |
Output is correct |
8 |
Correct |
12 ms |
54456 KB |
Output is correct |
9 |
Correct |
12 ms |
54708 KB |
Output is correct |
10 |
Correct |
14 ms |
54468 KB |
Output is correct |
11 |
Correct |
13 ms |
54616 KB |
Output is correct |
12 |
Correct |
15 ms |
54764 KB |
Output is correct |
13 |
Correct |
13 ms |
54676 KB |
Output is correct |
14 |
Correct |
12 ms |
54616 KB |
Output is correct |
15 |
Correct |
12 ms |
54792 KB |
Output is correct |
16 |
Correct |
17 ms |
55468 KB |
Output is correct |
17 |
Correct |
17 ms |
55388 KB |
Output is correct |
18 |
Correct |
17 ms |
55644 KB |
Output is correct |
19 |
Correct |
17 ms |
55648 KB |
Output is correct |
20 |
Correct |
15 ms |
55276 KB |
Output is correct |
21 |
Correct |
16 ms |
55388 KB |
Output is correct |
22 |
Correct |
16 ms |
55136 KB |
Output is correct |
23 |
Correct |
15 ms |
55256 KB |
Output is correct |
24 |
Correct |
15 ms |
55384 KB |
Output is correct |
25 |
Correct |
12 ms |
54620 KB |
Output is correct |
26 |
Correct |
12 ms |
54660 KB |
Output is correct |
27 |
Correct |
20 ms |
55644 KB |
Output is correct |
28 |
Correct |
17 ms |
55404 KB |
Output is correct |
29 |
Correct |
15 ms |
55388 KB |
Output is correct |
30 |
Correct |
16 ms |
55376 KB |
Output is correct |
31 |
Correct |
18 ms |
55640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
54620 KB |
Output is correct |
2 |
Correct |
12 ms |
54692 KB |
Output is correct |
3 |
Correct |
11 ms |
54620 KB |
Output is correct |
4 |
Correct |
759 ms |
148776 KB |
Output is correct |
5 |
Correct |
480 ms |
108288 KB |
Output is correct |
6 |
Correct |
277 ms |
101880 KB |
Output is correct |
7 |
Correct |
300 ms |
97988 KB |
Output is correct |
8 |
Correct |
273 ms |
112256 KB |
Output is correct |
9 |
Correct |
13 ms |
54616 KB |
Output is correct |
10 |
Correct |
13 ms |
54616 KB |
Output is correct |
11 |
Correct |
13 ms |
54620 KB |
Output is correct |
12 |
Correct |
12 ms |
54620 KB |
Output is correct |
13 |
Correct |
13 ms |
54684 KB |
Output is correct |
14 |
Correct |
12 ms |
54632 KB |
Output is correct |
15 |
Correct |
12 ms |
54620 KB |
Output is correct |
16 |
Correct |
12 ms |
54456 KB |
Output is correct |
17 |
Correct |
12 ms |
54708 KB |
Output is correct |
18 |
Correct |
14 ms |
54468 KB |
Output is correct |
19 |
Correct |
13 ms |
54616 KB |
Output is correct |
20 |
Correct |
15 ms |
54764 KB |
Output is correct |
21 |
Correct |
13 ms |
54676 KB |
Output is correct |
22 |
Correct |
12 ms |
54616 KB |
Output is correct |
23 |
Correct |
12 ms |
54792 KB |
Output is correct |
24 |
Correct |
17 ms |
55468 KB |
Output is correct |
25 |
Correct |
17 ms |
55388 KB |
Output is correct |
26 |
Correct |
17 ms |
55644 KB |
Output is correct |
27 |
Correct |
17 ms |
55648 KB |
Output is correct |
28 |
Correct |
15 ms |
55276 KB |
Output is correct |
29 |
Correct |
16 ms |
55388 KB |
Output is correct |
30 |
Correct |
16 ms |
55136 KB |
Output is correct |
31 |
Correct |
15 ms |
55256 KB |
Output is correct |
32 |
Correct |
15 ms |
55384 KB |
Output is correct |
33 |
Correct |
12 ms |
54620 KB |
Output is correct |
34 |
Correct |
12 ms |
54660 KB |
Output is correct |
35 |
Correct |
20 ms |
55644 KB |
Output is correct |
36 |
Correct |
17 ms |
55404 KB |
Output is correct |
37 |
Correct |
15 ms |
55388 KB |
Output is correct |
38 |
Correct |
16 ms |
55376 KB |
Output is correct |
39 |
Correct |
18 ms |
55640 KB |
Output is correct |
40 |
Correct |
829 ms |
161276 KB |
Output is correct |
41 |
Correct |
501 ms |
111872 KB |
Output is correct |
42 |
Correct |
586 ms |
166276 KB |
Output is correct |
43 |
Correct |
584 ms |
165248 KB |
Output is correct |
44 |
Correct |
268 ms |
104656 KB |
Output is correct |
45 |
Correct |
407 ms |
117760 KB |
Output is correct |
46 |
Correct |
156 ms |
82336 KB |
Output is correct |
47 |
Correct |
471 ms |
118532 KB |
Output is correct |
48 |
Correct |
418 ms |
136228 KB |
Output is correct |