#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define int ll
using namespace std;
typedef long long ll;
vector<vector<pair<int,ll> > > graph;
vector<int> pos;
void build(int l,int r,int ind){
if(l==r){
pos[l]=ind;
return;
}
int mid=(l+r)>>1;
build(l,mid,ind*2);
build(mid+1,r,ind*2+1);
graph[ind*2].push_back(make_pair(ind,0));
graph[ind*2+1].push_back(make_pair(ind,0));
}
void modify(int l,int r,int start,int end,int s,ll num,int ind){
if(r<start||end<l) return;
if(start<=l&&r<=end){
graph[ind].push_back(make_pair(s,num));
return;
}
int mid=(l+r)>>1;
modify(l,mid,start,end,s,num,ind*2);
modify(mid+1,r,start,end,s,num,ind*2+1);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n;
m = n;
graph.resize(4*n+4+m);
pos.resize(n+1);
build(1,n,1);
int a,b;
for(int i=0;i<m;i++){
cin>>a>>b;
modify(1,n,a,b,4*n+4+i,0,1);
graph[4*n+4+i].push_back(make_pair(pos[i + 1],1));
}
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > pq;
vector<ll> dist1(4*n+4+m,1e18),distn(4*n+4+m,1e18);
auto dijk=[&](vector<ll> &dist){
while(!pq.empty()){
auto [d,node]=pq.top();
pq.pop();
//cout<<d<<'\n';
if(d!=dist[node]) continue;
for(auto &[x,l]:graph[node]){
if(dist[x]>d+l){
dist[x]=d+l;
pq.push(make_pair(dist[x],x));
}
}
}
};
pq.push(make_pair(0,pos[1]));
dist1[pos[1]]=0;
dijk(dist1);
//for(int i=4*n+4;i<4*n+4+m;i++) cout<<dist1[i]<<' ';
//cout<<"\n";
//for(int i=1;i<=n;i++) cout<<dist1[pos[i]]<<' ';
pq.push(make_pair(0,pos[n]));
distn[pos[n]]=0;
dijk(distn);
vector<ll> dist(4*n+4+m,1e18);
for(int i=1;i<=4*n+3+m;i++) dist[i]=min(dist[i],dist1[i]+distn[i]);
for(int i=1;i<=4*n+3+m;i++) if(dist[i]<1e18) pq.push(make_pair(dist[i],i));
dijk(dist);
int q;
cin >> q;
while(q--){
int i;
cin >> i;
if(dist[pos[i]]>=1e18) cout<<"-1\n";
else cout<<dist[pos[i]]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
688 ms |
142068 KB |
Output is correct |
5 |
Correct |
423 ms |
105168 KB |
Output is correct |
6 |
Correct |
217 ms |
96616 KB |
Output is correct |
7 |
Correct |
254 ms |
87400 KB |
Output is correct |
8 |
Correct |
119 ms |
78336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1884 KB |
Output is correct |
17 |
Correct |
5 ms |
1628 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
4 ms |
1940 KB |
Output is correct |
20 |
Correct |
3 ms |
1620 KB |
Output is correct |
21 |
Correct |
2 ms |
1624 KB |
Output is correct |
22 |
Correct |
2 ms |
1368 KB |
Output is correct |
23 |
Correct |
4 ms |
1744 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1884 KB |
Output is correct |
17 |
Correct |
5 ms |
1628 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
4 ms |
1940 KB |
Output is correct |
20 |
Correct |
3 ms |
1620 KB |
Output is correct |
21 |
Correct |
2 ms |
1624 KB |
Output is correct |
22 |
Correct |
2 ms |
1368 KB |
Output is correct |
23 |
Correct |
4 ms |
1744 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
5 ms |
1748 KB |
Output is correct |
28 |
Correct |
4 ms |
1708 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
1628 KB |
Output is correct |
31 |
Correct |
4 ms |
1556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
688 ms |
142068 KB |
Output is correct |
5 |
Correct |
423 ms |
105168 KB |
Output is correct |
6 |
Correct |
217 ms |
96616 KB |
Output is correct |
7 |
Correct |
254 ms |
87400 KB |
Output is correct |
8 |
Correct |
119 ms |
78336 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
5 ms |
1628 KB |
Output is correct |
26 |
Correct |
4 ms |
2140 KB |
Output is correct |
27 |
Correct |
4 ms |
1940 KB |
Output is correct |
28 |
Correct |
3 ms |
1620 KB |
Output is correct |
29 |
Correct |
2 ms |
1624 KB |
Output is correct |
30 |
Correct |
2 ms |
1368 KB |
Output is correct |
31 |
Correct |
4 ms |
1744 KB |
Output is correct |
32 |
Correct |
2 ms |
1628 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
5 ms |
1748 KB |
Output is correct |
36 |
Correct |
4 ms |
1708 KB |
Output is correct |
37 |
Correct |
2 ms |
1628 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
39 |
Correct |
4 ms |
1556 KB |
Output is correct |
40 |
Correct |
773 ms |
142764 KB |
Output is correct |
41 |
Correct |
413 ms |
106176 KB |
Output is correct |
42 |
Correct |
479 ms |
150908 KB |
Output is correct |
43 |
Correct |
482 ms |
150900 KB |
Output is correct |
44 |
Correct |
230 ms |
96704 KB |
Output is correct |
45 |
Correct |
279 ms |
100972 KB |
Output is correct |
46 |
Correct |
132 ms |
38744 KB |
Output is correct |
47 |
Correct |
478 ms |
110220 KB |
Output is correct |
48 |
Correct |
286 ms |
114712 KB |
Output is correct |