#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 |
1 |
Correct |
27 ms |
56592 KB |
Output is correct |
2 |
Correct |
26 ms |
56652 KB |
Output is correct |
3 |
Correct |
28 ms |
56592 KB |
Output is correct |
4 |
Correct |
818 ms |
149680 KB |
Output is correct |
5 |
Correct |
432 ms |
109452 KB |
Output is correct |
6 |
Correct |
284 ms |
95804 KB |
Output is correct |
7 |
Correct |
296 ms |
92172 KB |
Output is correct |
8 |
Correct |
242 ms |
113724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
56704 KB |
Output is correct |
2 |
Correct |
26 ms |
56660 KB |
Output is correct |
3 |
Correct |
26 ms |
56652 KB |
Output is correct |
4 |
Correct |
26 ms |
56580 KB |
Output is correct |
5 |
Correct |
25 ms |
56628 KB |
Output is correct |
6 |
Correct |
28 ms |
56604 KB |
Output is correct |
7 |
Correct |
27 ms |
56660 KB |
Output is correct |
8 |
Correct |
26 ms |
56600 KB |
Output is correct |
9 |
Correct |
26 ms |
56580 KB |
Output is correct |
10 |
Correct |
26 ms |
56596 KB |
Output is correct |
11 |
Correct |
27 ms |
56796 KB |
Output is correct |
12 |
Correct |
26 ms |
56728 KB |
Output is correct |
13 |
Correct |
26 ms |
56768 KB |
Output is correct |
14 |
Correct |
28 ms |
56708 KB |
Output is correct |
15 |
Correct |
29 ms |
56732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
56704 KB |
Output is correct |
2 |
Correct |
26 ms |
56660 KB |
Output is correct |
3 |
Correct |
26 ms |
56652 KB |
Output is correct |
4 |
Correct |
26 ms |
56580 KB |
Output is correct |
5 |
Correct |
25 ms |
56628 KB |
Output is correct |
6 |
Correct |
28 ms |
56604 KB |
Output is correct |
7 |
Correct |
27 ms |
56660 KB |
Output is correct |
8 |
Correct |
26 ms |
56600 KB |
Output is correct |
9 |
Correct |
26 ms |
56580 KB |
Output is correct |
10 |
Correct |
26 ms |
56596 KB |
Output is correct |
11 |
Correct |
27 ms |
56796 KB |
Output is correct |
12 |
Correct |
26 ms |
56728 KB |
Output is correct |
13 |
Correct |
26 ms |
56768 KB |
Output is correct |
14 |
Correct |
28 ms |
56708 KB |
Output is correct |
15 |
Correct |
29 ms |
56732 KB |
Output is correct |
16 |
Correct |
35 ms |
57432 KB |
Output is correct |
17 |
Correct |
31 ms |
57244 KB |
Output is correct |
18 |
Correct |
41 ms |
57728 KB |
Output is correct |
19 |
Correct |
35 ms |
57764 KB |
Output is correct |
20 |
Correct |
30 ms |
57132 KB |
Output is correct |
21 |
Correct |
29 ms |
57300 KB |
Output is correct |
22 |
Correct |
29 ms |
57172 KB |
Output is correct |
23 |
Correct |
31 ms |
57284 KB |
Output is correct |
24 |
Correct |
30 ms |
57412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
56704 KB |
Output is correct |
2 |
Correct |
26 ms |
56660 KB |
Output is correct |
3 |
Correct |
26 ms |
56652 KB |
Output is correct |
4 |
Correct |
26 ms |
56580 KB |
Output is correct |
5 |
Correct |
25 ms |
56628 KB |
Output is correct |
6 |
Correct |
28 ms |
56604 KB |
Output is correct |
7 |
Correct |
27 ms |
56660 KB |
Output is correct |
8 |
Correct |
26 ms |
56600 KB |
Output is correct |
9 |
Correct |
26 ms |
56580 KB |
Output is correct |
10 |
Correct |
26 ms |
56596 KB |
Output is correct |
11 |
Correct |
27 ms |
56796 KB |
Output is correct |
12 |
Correct |
26 ms |
56728 KB |
Output is correct |
13 |
Correct |
26 ms |
56768 KB |
Output is correct |
14 |
Correct |
28 ms |
56708 KB |
Output is correct |
15 |
Correct |
29 ms |
56732 KB |
Output is correct |
16 |
Correct |
35 ms |
57432 KB |
Output is correct |
17 |
Correct |
31 ms |
57244 KB |
Output is correct |
18 |
Correct |
41 ms |
57728 KB |
Output is correct |
19 |
Correct |
35 ms |
57764 KB |
Output is correct |
20 |
Correct |
30 ms |
57132 KB |
Output is correct |
21 |
Correct |
29 ms |
57300 KB |
Output is correct |
22 |
Correct |
29 ms |
57172 KB |
Output is correct |
23 |
Correct |
31 ms |
57284 KB |
Output is correct |
24 |
Correct |
30 ms |
57412 KB |
Output is correct |
25 |
Correct |
29 ms |
56592 KB |
Output is correct |
26 |
Correct |
29 ms |
56644 KB |
Output is correct |
27 |
Correct |
32 ms |
57548 KB |
Output is correct |
28 |
Correct |
30 ms |
57284 KB |
Output is correct |
29 |
Correct |
29 ms |
57192 KB |
Output is correct |
30 |
Correct |
31 ms |
57300 KB |
Output is correct |
31 |
Correct |
30 ms |
57340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
56592 KB |
Output is correct |
2 |
Correct |
26 ms |
56652 KB |
Output is correct |
3 |
Correct |
28 ms |
56592 KB |
Output is correct |
4 |
Correct |
818 ms |
149680 KB |
Output is correct |
5 |
Correct |
432 ms |
109452 KB |
Output is correct |
6 |
Correct |
284 ms |
95804 KB |
Output is correct |
7 |
Correct |
296 ms |
92172 KB |
Output is correct |
8 |
Correct |
242 ms |
113724 KB |
Output is correct |
9 |
Correct |
26 ms |
56704 KB |
Output is correct |
10 |
Correct |
26 ms |
56660 KB |
Output is correct |
11 |
Correct |
26 ms |
56652 KB |
Output is correct |
12 |
Correct |
26 ms |
56580 KB |
Output is correct |
13 |
Correct |
25 ms |
56628 KB |
Output is correct |
14 |
Correct |
28 ms |
56604 KB |
Output is correct |
15 |
Correct |
27 ms |
56660 KB |
Output is correct |
16 |
Correct |
26 ms |
56600 KB |
Output is correct |
17 |
Correct |
26 ms |
56580 KB |
Output is correct |
18 |
Correct |
26 ms |
56596 KB |
Output is correct |
19 |
Correct |
27 ms |
56796 KB |
Output is correct |
20 |
Correct |
26 ms |
56728 KB |
Output is correct |
21 |
Correct |
26 ms |
56768 KB |
Output is correct |
22 |
Correct |
28 ms |
56708 KB |
Output is correct |
23 |
Correct |
29 ms |
56732 KB |
Output is correct |
24 |
Correct |
35 ms |
57432 KB |
Output is correct |
25 |
Correct |
31 ms |
57244 KB |
Output is correct |
26 |
Correct |
41 ms |
57728 KB |
Output is correct |
27 |
Correct |
35 ms |
57764 KB |
Output is correct |
28 |
Correct |
30 ms |
57132 KB |
Output is correct |
29 |
Correct |
29 ms |
57300 KB |
Output is correct |
30 |
Correct |
29 ms |
57172 KB |
Output is correct |
31 |
Correct |
31 ms |
57284 KB |
Output is correct |
32 |
Correct |
30 ms |
57412 KB |
Output is correct |
33 |
Correct |
29 ms |
56592 KB |
Output is correct |
34 |
Correct |
29 ms |
56644 KB |
Output is correct |
35 |
Correct |
32 ms |
57548 KB |
Output is correct |
36 |
Correct |
30 ms |
57284 KB |
Output is correct |
37 |
Correct |
29 ms |
57192 KB |
Output is correct |
38 |
Correct |
31 ms |
57300 KB |
Output is correct |
39 |
Correct |
30 ms |
57340 KB |
Output is correct |
40 |
Correct |
893 ms |
151920 KB |
Output is correct |
41 |
Correct |
465 ms |
110196 KB |
Output is correct |
42 |
Correct |
639 ms |
166116 KB |
Output is correct |
43 |
Correct |
660 ms |
165676 KB |
Output is correct |
44 |
Correct |
313 ms |
96544 KB |
Output is correct |
45 |
Correct |
384 ms |
110528 KB |
Output is correct |
46 |
Correct |
212 ms |
75640 KB |
Output is correct |
47 |
Correct |
583 ms |
114272 KB |
Output is correct |
48 |
Correct |
479 ms |
128976 KB |
Output is correct |