#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> p2;
const int N=2e5+5;
const int M=5*N;
const int inf=1e9;
int n,q;
vector<p2> adj[M];
int dist[3][M];
bool vis[M];
void link(int u,int v,int w=0){
adj[v].emplace_back(u,w);
}
void build(int l,int r,int i){
if(l==r)return void(link(n+i,l));
int m=(l+r)/2;
build(l,m,i*2);
build(m+1,r,i*2+1);
link(n+i,n+i*2);
link(n+i,n+i*2+1);
}
void connect(int l,int r,int i,int x,int y,int u){
if(y<l||r<x)return;
if(x<=l&&r<=y)return void(link(u,n+i,1));
int m=(l+r)/2;
connect(l,m,i*2,x,y,u);
connect(m+1,r,i*2+1,x,y,u);
}
void dijk(int *dist){
priority_queue<p2,vector<p2>,greater<p2>> pq;
for(int i=1;i<=5*n;i++)pq.emplace(dist[i],i),vis[i]=false;
while(!pq.empty()){
auto [d,u]=pq.top();
pq.pop();
if(vis[u])continue;
vis[u]=true;
for(auto [v,w]:adj[u])if(d+w<dist[v]){
pq.emplace(d+w,v);
dist[v]=d+w;
}
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
build(1,n,1);
for(int i=1;i<=n;i++){
int l,r;
cin >> l >> r;
connect(1,n,1,l,r,i);
}
for(int i=0;i<3;i++)for(int j=1;j<=5*n;j++)dist[i][j]=inf;
dist[0][1]=dist[1][n]=0;
dijk(dist[0]);
dijk(dist[1]);
for(int i=1;i<=n;i++)dist[2][i]=min(inf,dist[0][i]+dist[1][i]-(1<i&&i<n));
dijk(dist[2]);
cin >> q;
while(q--){
int x;
cin >> x;
cout << (dist[2][x]<inf?dist[2][x]:-1) << "\n";
}
}
Compilation message
passport.cpp: In function 'void dijk(int*)':
passport.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | auto [d,u]=pq.top();
| ^
passport.cpp:45:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
45 | for(auto [v,w]:adj[u])if(d+w<dist[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
30296 KB |
Output is correct |
2 |
Correct |
6 ms |
30300 KB |
Output is correct |
3 |
Correct |
7 ms |
30300 KB |
Output is correct |
4 |
Correct |
1031 ms |
111088 KB |
Output is correct |
5 |
Correct |
678 ms |
82984 KB |
Output is correct |
6 |
Correct |
604 ms |
77100 KB |
Output is correct |
7 |
Correct |
568 ms |
84384 KB |
Output is correct |
8 |
Correct |
386 ms |
80176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30300 KB |
Output is correct |
2 |
Correct |
6 ms |
30152 KB |
Output is correct |
3 |
Correct |
6 ms |
30300 KB |
Output is correct |
4 |
Correct |
7 ms |
30300 KB |
Output is correct |
5 |
Correct |
6 ms |
30300 KB |
Output is correct |
6 |
Correct |
7 ms |
30300 KB |
Output is correct |
7 |
Correct |
7 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30316 KB |
Output is correct |
9 |
Correct |
7 ms |
30300 KB |
Output is correct |
10 |
Correct |
6 ms |
30300 KB |
Output is correct |
11 |
Correct |
8 ms |
30300 KB |
Output is correct |
12 |
Correct |
8 ms |
30300 KB |
Output is correct |
13 |
Correct |
8 ms |
30196 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
6 ms |
30400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30300 KB |
Output is correct |
2 |
Correct |
6 ms |
30152 KB |
Output is correct |
3 |
Correct |
6 ms |
30300 KB |
Output is correct |
4 |
Correct |
7 ms |
30300 KB |
Output is correct |
5 |
Correct |
6 ms |
30300 KB |
Output is correct |
6 |
Correct |
7 ms |
30300 KB |
Output is correct |
7 |
Correct |
7 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30316 KB |
Output is correct |
9 |
Correct |
7 ms |
30300 KB |
Output is correct |
10 |
Correct |
6 ms |
30300 KB |
Output is correct |
11 |
Correct |
8 ms |
30300 KB |
Output is correct |
12 |
Correct |
8 ms |
30300 KB |
Output is correct |
13 |
Correct |
8 ms |
30196 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
6 ms |
30400 KB |
Output is correct |
16 |
Correct |
13 ms |
30964 KB |
Output is correct |
17 |
Correct |
13 ms |
30868 KB |
Output is correct |
18 |
Correct |
13 ms |
31116 KB |
Output is correct |
19 |
Correct |
13 ms |
31168 KB |
Output is correct |
20 |
Correct |
11 ms |
30812 KB |
Output is correct |
21 |
Correct |
11 ms |
30916 KB |
Output is correct |
22 |
Correct |
13 ms |
30840 KB |
Output is correct |
23 |
Correct |
12 ms |
30956 KB |
Output is correct |
24 |
Correct |
11 ms |
30956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30300 KB |
Output is correct |
2 |
Correct |
6 ms |
30152 KB |
Output is correct |
3 |
Correct |
6 ms |
30300 KB |
Output is correct |
4 |
Correct |
7 ms |
30300 KB |
Output is correct |
5 |
Correct |
6 ms |
30300 KB |
Output is correct |
6 |
Correct |
7 ms |
30300 KB |
Output is correct |
7 |
Correct |
7 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30316 KB |
Output is correct |
9 |
Correct |
7 ms |
30300 KB |
Output is correct |
10 |
Correct |
6 ms |
30300 KB |
Output is correct |
11 |
Correct |
8 ms |
30300 KB |
Output is correct |
12 |
Correct |
8 ms |
30300 KB |
Output is correct |
13 |
Correct |
8 ms |
30196 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
6 ms |
30400 KB |
Output is correct |
16 |
Correct |
13 ms |
30964 KB |
Output is correct |
17 |
Correct |
13 ms |
30868 KB |
Output is correct |
18 |
Correct |
13 ms |
31116 KB |
Output is correct |
19 |
Correct |
13 ms |
31168 KB |
Output is correct |
20 |
Correct |
11 ms |
30812 KB |
Output is correct |
21 |
Correct |
11 ms |
30916 KB |
Output is correct |
22 |
Correct |
13 ms |
30840 KB |
Output is correct |
23 |
Correct |
12 ms |
30956 KB |
Output is correct |
24 |
Correct |
11 ms |
30956 KB |
Output is correct |
25 |
Correct |
6 ms |
30296 KB |
Output is correct |
26 |
Correct |
7 ms |
30300 KB |
Output is correct |
27 |
Correct |
13 ms |
30960 KB |
Output is correct |
28 |
Correct |
14 ms |
30868 KB |
Output is correct |
29 |
Correct |
12 ms |
30860 KB |
Output is correct |
30 |
Correct |
14 ms |
30868 KB |
Output is correct |
31 |
Correct |
12 ms |
30856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
30296 KB |
Output is correct |
2 |
Correct |
6 ms |
30300 KB |
Output is correct |
3 |
Correct |
7 ms |
30300 KB |
Output is correct |
4 |
Correct |
1031 ms |
111088 KB |
Output is correct |
5 |
Correct |
678 ms |
82984 KB |
Output is correct |
6 |
Correct |
604 ms |
77100 KB |
Output is correct |
7 |
Correct |
568 ms |
84384 KB |
Output is correct |
8 |
Correct |
386 ms |
80176 KB |
Output is correct |
9 |
Correct |
6 ms |
30300 KB |
Output is correct |
10 |
Correct |
6 ms |
30152 KB |
Output is correct |
11 |
Correct |
6 ms |
30300 KB |
Output is correct |
12 |
Correct |
7 ms |
30300 KB |
Output is correct |
13 |
Correct |
6 ms |
30300 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
7 ms |
30552 KB |
Output is correct |
16 |
Correct |
6 ms |
30316 KB |
Output is correct |
17 |
Correct |
7 ms |
30300 KB |
Output is correct |
18 |
Correct |
6 ms |
30300 KB |
Output is correct |
19 |
Correct |
8 ms |
30300 KB |
Output is correct |
20 |
Correct |
8 ms |
30300 KB |
Output is correct |
21 |
Correct |
8 ms |
30196 KB |
Output is correct |
22 |
Correct |
7 ms |
30300 KB |
Output is correct |
23 |
Correct |
6 ms |
30400 KB |
Output is correct |
24 |
Correct |
13 ms |
30964 KB |
Output is correct |
25 |
Correct |
13 ms |
30868 KB |
Output is correct |
26 |
Correct |
13 ms |
31116 KB |
Output is correct |
27 |
Correct |
13 ms |
31168 KB |
Output is correct |
28 |
Correct |
11 ms |
30812 KB |
Output is correct |
29 |
Correct |
11 ms |
30916 KB |
Output is correct |
30 |
Correct |
13 ms |
30840 KB |
Output is correct |
31 |
Correct |
12 ms |
30956 KB |
Output is correct |
32 |
Correct |
11 ms |
30956 KB |
Output is correct |
33 |
Correct |
6 ms |
30296 KB |
Output is correct |
34 |
Correct |
7 ms |
30300 KB |
Output is correct |
35 |
Correct |
13 ms |
30960 KB |
Output is correct |
36 |
Correct |
14 ms |
30868 KB |
Output is correct |
37 |
Correct |
12 ms |
30860 KB |
Output is correct |
38 |
Correct |
14 ms |
30868 KB |
Output is correct |
39 |
Correct |
12 ms |
30856 KB |
Output is correct |
40 |
Correct |
1043 ms |
114332 KB |
Output is correct |
41 |
Correct |
715 ms |
83492 KB |
Output is correct |
42 |
Correct |
759 ms |
110136 KB |
Output is correct |
43 |
Correct |
754 ms |
119680 KB |
Output is correct |
44 |
Correct |
664 ms |
77508 KB |
Output is correct |
45 |
Correct |
629 ms |
84988 KB |
Output is correct |
46 |
Correct |
260 ms |
53460 KB |
Output is correct |
47 |
Correct |
765 ms |
96012 KB |
Output is correct |
48 |
Correct |
680 ms |
102656 KB |
Output is correct |