#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 2e5+5;
vector<pair<int,int> > side[4*N];
int b;
void addto(int x,int l,int r,int L,int R,int t){
if(l==L && r ==R){
side[t].push_back({x,1});
return ;
}
int M = (L+R)/2;
if(r<=M) addto(x,l,r,L,M,2*t);
else if(l>=M) addto(x,l,r,M,R,2*t+1);
else{
addto(x,l,M,L,M,2*t);
addto(x,M,r,M,R,2*t+1);
}
}
int ntot[N];
void built(int ind,int l,int r){
if(r-l==1){
ntot[l]=ind;
return;
}
int m = (l+r)/2;
side[2*ind].push_back({ind,0});
side[2*ind+1].push_back({ind,0});
built(2*ind,l,m);
built(2*ind+1,m,r);
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;
built(1,1,n+1);
for(int i=1;i<=n;i++){
int l,r;cin>>l>>r;
addto(ntot[i],l,r+1,1,n+1,1);
}
vector<ll> dis1(4*n+5,1e9);
vector<ll> disn(4*n+5,1e9);
dis1[ntot[1]]=0;
disn[ntot[n]]=0;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
pq.push({0,ntot[1]});
while(pq.size()){
pair<int,int> cur = pq.top();
pq.pop();
if(dis1[cur.second]!=cur.first) continue;
for(auto e : side[cur.second]){
if(dis1[e.first]>cur.first+e.second){
dis1[e.first] = cur.first+e.second;
pq.push({dis1[e.first],e.first});
}
}
}
pq.push({0,ntot[n]});
while(pq.size()){
pair<int,int> cur = pq.top();
pq.pop();
if(disn[cur.second]!=cur.first) continue;
for(auto e : side[cur.second]){
if(disn[e.first]>cur.first+e.second){
disn[e.first] = cur.first+e.second;
pq.push({disn[e.first],e.first});
}
}
}
for(int i=1;i<=n;i++){
side[0].push_back({ntot[i],max(dis1[ntot[i]]-1,0LL)+max(disn[ntot[i]]-1,0LL)+1});
}
vector<ll> ans(4*n+5,1e9);
ans[0]=0;
pq.push({0,0});
while(pq.size()){
pair<int,int> cur = pq.top();
pq.pop();
if(ans[cur.second]!=cur.first) continue;
for(auto e : side[cur.second]){
if(ans[e.first]>cur.first+e.second){
ans[e.first] = cur.first+e.second;
pq.push({ans[e.first],e.first});
}
}
}
int q;cin>>q;
while(q--){
int x;cin>>x;
if(ans[ntot[x]]>n) cout<<-1<<"\n";
else cout<<ans[ntot[x]]<<"\n";
}
return 0;
}
//ans = min(ans,1LL*max(dis1[i]-1,0)+max(disn[i]-1,0)+d[i]+1);
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19648 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
19804 KB |
Output is correct |
4 |
Correct |
598 ms |
83276 KB |
Output is correct |
5 |
Correct |
264 ms |
65244 KB |
Output is correct |
6 |
Correct |
155 ms |
60700 KB |
Output is correct |
7 |
Correct |
177 ms |
56200 KB |
Output is correct |
8 |
Correct |
77 ms |
48704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19800 KB |
Output is correct |
2 |
Correct |
4 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
19804 KB |
Output is correct |
4 |
Correct |
4 ms |
19656 KB |
Output is correct |
5 |
Correct |
5 ms |
19660 KB |
Output is correct |
6 |
Correct |
5 ms |
19636 KB |
Output is correct |
7 |
Correct |
4 ms |
19804 KB |
Output is correct |
8 |
Correct |
4 ms |
19804 KB |
Output is correct |
9 |
Correct |
4 ms |
19800 KB |
Output is correct |
10 |
Correct |
5 ms |
19800 KB |
Output is correct |
11 |
Correct |
5 ms |
19800 KB |
Output is correct |
12 |
Correct |
5 ms |
19804 KB |
Output is correct |
13 |
Correct |
5 ms |
19860 KB |
Output is correct |
14 |
Correct |
4 ms |
19800 KB |
Output is correct |
15 |
Correct |
5 ms |
19804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19800 KB |
Output is correct |
2 |
Correct |
4 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
19804 KB |
Output is correct |
4 |
Correct |
4 ms |
19656 KB |
Output is correct |
5 |
Correct |
5 ms |
19660 KB |
Output is correct |
6 |
Correct |
5 ms |
19636 KB |
Output is correct |
7 |
Correct |
4 ms |
19804 KB |
Output is correct |
8 |
Correct |
4 ms |
19804 KB |
Output is correct |
9 |
Correct |
4 ms |
19800 KB |
Output is correct |
10 |
Correct |
5 ms |
19800 KB |
Output is correct |
11 |
Correct |
5 ms |
19800 KB |
Output is correct |
12 |
Correct |
5 ms |
19804 KB |
Output is correct |
13 |
Correct |
5 ms |
19860 KB |
Output is correct |
14 |
Correct |
4 ms |
19800 KB |
Output is correct |
15 |
Correct |
5 ms |
19804 KB |
Output is correct |
16 |
Correct |
8 ms |
20316 KB |
Output is correct |
17 |
Correct |
7 ms |
20456 KB |
Output is correct |
18 |
Correct |
8 ms |
20572 KB |
Output is correct |
19 |
Correct |
8 ms |
20572 KB |
Output is correct |
20 |
Correct |
6 ms |
20316 KB |
Output is correct |
21 |
Correct |
7 ms |
20316 KB |
Output is correct |
22 |
Correct |
5 ms |
20316 KB |
Output is correct |
23 |
Correct |
7 ms |
20316 KB |
Output is correct |
24 |
Correct |
6 ms |
20316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19800 KB |
Output is correct |
2 |
Correct |
4 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
19804 KB |
Output is correct |
4 |
Correct |
4 ms |
19656 KB |
Output is correct |
5 |
Correct |
5 ms |
19660 KB |
Output is correct |
6 |
Correct |
5 ms |
19636 KB |
Output is correct |
7 |
Correct |
4 ms |
19804 KB |
Output is correct |
8 |
Correct |
4 ms |
19804 KB |
Output is correct |
9 |
Correct |
4 ms |
19800 KB |
Output is correct |
10 |
Correct |
5 ms |
19800 KB |
Output is correct |
11 |
Correct |
5 ms |
19800 KB |
Output is correct |
12 |
Correct |
5 ms |
19804 KB |
Output is correct |
13 |
Correct |
5 ms |
19860 KB |
Output is correct |
14 |
Correct |
4 ms |
19800 KB |
Output is correct |
15 |
Correct |
5 ms |
19804 KB |
Output is correct |
16 |
Correct |
8 ms |
20316 KB |
Output is correct |
17 |
Correct |
7 ms |
20456 KB |
Output is correct |
18 |
Correct |
8 ms |
20572 KB |
Output is correct |
19 |
Correct |
8 ms |
20572 KB |
Output is correct |
20 |
Correct |
6 ms |
20316 KB |
Output is correct |
21 |
Correct |
7 ms |
20316 KB |
Output is correct |
22 |
Correct |
5 ms |
20316 KB |
Output is correct |
23 |
Correct |
7 ms |
20316 KB |
Output is correct |
24 |
Correct |
6 ms |
20316 KB |
Output is correct |
25 |
Correct |
4 ms |
19804 KB |
Output is correct |
26 |
Correct |
4 ms |
19804 KB |
Output is correct |
27 |
Correct |
7 ms |
20380 KB |
Output is correct |
28 |
Correct |
7 ms |
20316 KB |
Output is correct |
29 |
Correct |
6 ms |
20316 KB |
Output is correct |
30 |
Correct |
6 ms |
20316 KB |
Output is correct |
31 |
Correct |
7 ms |
20384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19648 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
19804 KB |
Output is correct |
4 |
Correct |
598 ms |
83276 KB |
Output is correct |
5 |
Correct |
264 ms |
65244 KB |
Output is correct |
6 |
Correct |
155 ms |
60700 KB |
Output is correct |
7 |
Correct |
177 ms |
56200 KB |
Output is correct |
8 |
Correct |
77 ms |
48704 KB |
Output is correct |
9 |
Correct |
4 ms |
19800 KB |
Output is correct |
10 |
Correct |
4 ms |
19804 KB |
Output is correct |
11 |
Correct |
4 ms |
19804 KB |
Output is correct |
12 |
Correct |
4 ms |
19656 KB |
Output is correct |
13 |
Correct |
5 ms |
19660 KB |
Output is correct |
14 |
Correct |
5 ms |
19636 KB |
Output is correct |
15 |
Correct |
4 ms |
19804 KB |
Output is correct |
16 |
Correct |
4 ms |
19804 KB |
Output is correct |
17 |
Correct |
4 ms |
19800 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
5 ms |
19800 KB |
Output is correct |
20 |
Correct |
5 ms |
19804 KB |
Output is correct |
21 |
Correct |
5 ms |
19860 KB |
Output is correct |
22 |
Correct |
4 ms |
19800 KB |
Output is correct |
23 |
Correct |
5 ms |
19804 KB |
Output is correct |
24 |
Correct |
8 ms |
20316 KB |
Output is correct |
25 |
Correct |
7 ms |
20456 KB |
Output is correct |
26 |
Correct |
8 ms |
20572 KB |
Output is correct |
27 |
Correct |
8 ms |
20572 KB |
Output is correct |
28 |
Correct |
6 ms |
20316 KB |
Output is correct |
29 |
Correct |
7 ms |
20316 KB |
Output is correct |
30 |
Correct |
5 ms |
20316 KB |
Output is correct |
31 |
Correct |
7 ms |
20316 KB |
Output is correct |
32 |
Correct |
6 ms |
20316 KB |
Output is correct |
33 |
Correct |
4 ms |
19804 KB |
Output is correct |
34 |
Correct |
4 ms |
19804 KB |
Output is correct |
35 |
Correct |
7 ms |
20380 KB |
Output is correct |
36 |
Correct |
7 ms |
20316 KB |
Output is correct |
37 |
Correct |
6 ms |
20316 KB |
Output is correct |
38 |
Correct |
6 ms |
20316 KB |
Output is correct |
39 |
Correct |
7 ms |
20384 KB |
Output is correct |
40 |
Correct |
621 ms |
89556 KB |
Output is correct |
41 |
Correct |
280 ms |
67944 KB |
Output is correct |
42 |
Correct |
381 ms |
94904 KB |
Output is correct |
43 |
Correct |
378 ms |
94812 KB |
Output is correct |
44 |
Correct |
174 ms |
62888 KB |
Output is correct |
45 |
Correct |
195 ms |
70184 KB |
Output is correct |
46 |
Correct |
106 ms |
37484 KB |
Output is correct |
47 |
Correct |
368 ms |
70364 KB |
Output is correct |
48 |
Correct |
248 ms |
77384 KB |
Output is correct |