#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<=5*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 |
8 ms |
30296 KB |
Output is correct |
2 |
Correct |
6 ms |
30300 KB |
Output is correct |
3 |
Correct |
7 ms |
30152 KB |
Output is correct |
4 |
Correct |
1014 ms |
112388 KB |
Output is correct |
5 |
Correct |
678 ms |
83052 KB |
Output is correct |
6 |
Correct |
583 ms |
77280 KB |
Output is correct |
7 |
Correct |
560 ms |
84868 KB |
Output is correct |
8 |
Correct |
382 ms |
79736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30312 KB |
Output is correct |
2 |
Correct |
6 ms |
30320 KB |
Output is correct |
3 |
Correct |
6 ms |
30300 KB |
Output is correct |
4 |
Correct |
7 ms |
30304 KB |
Output is correct |
5 |
Correct |
7 ms |
30300 KB |
Output is correct |
6 |
Correct |
6 ms |
30312 KB |
Output is correct |
7 |
Correct |
6 ms |
30300 KB |
Output is correct |
8 |
Correct |
6 ms |
30300 KB |
Output is correct |
9 |
Correct |
7 ms |
30316 KB |
Output is correct |
10 |
Correct |
6 ms |
30300 KB |
Output is correct |
11 |
Correct |
7 ms |
30300 KB |
Output is correct |
12 |
Correct |
7 ms |
30300 KB |
Output is correct |
13 |
Correct |
7 ms |
30300 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
7 ms |
30172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30312 KB |
Output is correct |
2 |
Correct |
6 ms |
30320 KB |
Output is correct |
3 |
Correct |
6 ms |
30300 KB |
Output is correct |
4 |
Correct |
7 ms |
30304 KB |
Output is correct |
5 |
Correct |
7 ms |
30300 KB |
Output is correct |
6 |
Correct |
6 ms |
30312 KB |
Output is correct |
7 |
Correct |
6 ms |
30300 KB |
Output is correct |
8 |
Correct |
6 ms |
30300 KB |
Output is correct |
9 |
Correct |
7 ms |
30316 KB |
Output is correct |
10 |
Correct |
6 ms |
30300 KB |
Output is correct |
11 |
Correct |
7 ms |
30300 KB |
Output is correct |
12 |
Correct |
7 ms |
30300 KB |
Output is correct |
13 |
Correct |
7 ms |
30300 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
7 ms |
30172 KB |
Output is correct |
16 |
Correct |
13 ms |
31116 KB |
Output is correct |
17 |
Correct |
12 ms |
30968 KB |
Output is correct |
18 |
Correct |
13 ms |
31116 KB |
Output is correct |
19 |
Correct |
13 ms |
31088 KB |
Output is correct |
20 |
Correct |
12 ms |
30864 KB |
Output is correct |
21 |
Correct |
12 ms |
30868 KB |
Output is correct |
22 |
Correct |
10 ms |
30852 KB |
Output is correct |
23 |
Correct |
12 ms |
30940 KB |
Output is correct |
24 |
Correct |
11 ms |
30960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30312 KB |
Output is correct |
2 |
Correct |
6 ms |
30320 KB |
Output is correct |
3 |
Correct |
6 ms |
30300 KB |
Output is correct |
4 |
Correct |
7 ms |
30304 KB |
Output is correct |
5 |
Correct |
7 ms |
30300 KB |
Output is correct |
6 |
Correct |
6 ms |
30312 KB |
Output is correct |
7 |
Correct |
6 ms |
30300 KB |
Output is correct |
8 |
Correct |
6 ms |
30300 KB |
Output is correct |
9 |
Correct |
7 ms |
30316 KB |
Output is correct |
10 |
Correct |
6 ms |
30300 KB |
Output is correct |
11 |
Correct |
7 ms |
30300 KB |
Output is correct |
12 |
Correct |
7 ms |
30300 KB |
Output is correct |
13 |
Correct |
7 ms |
30300 KB |
Output is correct |
14 |
Correct |
7 ms |
30300 KB |
Output is correct |
15 |
Correct |
7 ms |
30172 KB |
Output is correct |
16 |
Correct |
13 ms |
31116 KB |
Output is correct |
17 |
Correct |
12 ms |
30968 KB |
Output is correct |
18 |
Correct |
13 ms |
31116 KB |
Output is correct |
19 |
Correct |
13 ms |
31088 KB |
Output is correct |
20 |
Correct |
12 ms |
30864 KB |
Output is correct |
21 |
Correct |
12 ms |
30868 KB |
Output is correct |
22 |
Correct |
10 ms |
30852 KB |
Output is correct |
23 |
Correct |
12 ms |
30940 KB |
Output is correct |
24 |
Correct |
11 ms |
30960 KB |
Output is correct |
25 |
Correct |
6 ms |
30300 KB |
Output is correct |
26 |
Correct |
6 ms |
30332 KB |
Output is correct |
27 |
Correct |
13 ms |
31068 KB |
Output is correct |
28 |
Correct |
14 ms |
30968 KB |
Output is correct |
29 |
Correct |
12 ms |
30868 KB |
Output is correct |
30 |
Correct |
12 ms |
30868 KB |
Output is correct |
31 |
Correct |
12 ms |
30888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30296 KB |
Output is correct |
2 |
Correct |
6 ms |
30300 KB |
Output is correct |
3 |
Correct |
7 ms |
30152 KB |
Output is correct |
4 |
Correct |
1014 ms |
112388 KB |
Output is correct |
5 |
Correct |
678 ms |
83052 KB |
Output is correct |
6 |
Correct |
583 ms |
77280 KB |
Output is correct |
7 |
Correct |
560 ms |
84868 KB |
Output is correct |
8 |
Correct |
382 ms |
79736 KB |
Output is correct |
9 |
Correct |
6 ms |
30312 KB |
Output is correct |
10 |
Correct |
6 ms |
30320 KB |
Output is correct |
11 |
Correct |
6 ms |
30300 KB |
Output is correct |
12 |
Correct |
7 ms |
30304 KB |
Output is correct |
13 |
Correct |
7 ms |
30300 KB |
Output is correct |
14 |
Correct |
6 ms |
30312 KB |
Output is correct |
15 |
Correct |
6 ms |
30300 KB |
Output is correct |
16 |
Correct |
6 ms |
30300 KB |
Output is correct |
17 |
Correct |
7 ms |
30316 KB |
Output is correct |
18 |
Correct |
6 ms |
30300 KB |
Output is correct |
19 |
Correct |
7 ms |
30300 KB |
Output is correct |
20 |
Correct |
7 ms |
30300 KB |
Output is correct |
21 |
Correct |
7 ms |
30300 KB |
Output is correct |
22 |
Correct |
7 ms |
30300 KB |
Output is correct |
23 |
Correct |
7 ms |
30172 KB |
Output is correct |
24 |
Correct |
13 ms |
31116 KB |
Output is correct |
25 |
Correct |
12 ms |
30968 KB |
Output is correct |
26 |
Correct |
13 ms |
31116 KB |
Output is correct |
27 |
Correct |
13 ms |
31088 KB |
Output is correct |
28 |
Correct |
12 ms |
30864 KB |
Output is correct |
29 |
Correct |
12 ms |
30868 KB |
Output is correct |
30 |
Correct |
10 ms |
30852 KB |
Output is correct |
31 |
Correct |
12 ms |
30940 KB |
Output is correct |
32 |
Correct |
11 ms |
30960 KB |
Output is correct |
33 |
Correct |
6 ms |
30300 KB |
Output is correct |
34 |
Correct |
6 ms |
30332 KB |
Output is correct |
35 |
Correct |
13 ms |
31068 KB |
Output is correct |
36 |
Correct |
14 ms |
30968 KB |
Output is correct |
37 |
Correct |
12 ms |
30868 KB |
Output is correct |
38 |
Correct |
12 ms |
30868 KB |
Output is correct |
39 |
Correct |
12 ms |
30888 KB |
Output is correct |
40 |
Correct |
1051 ms |
113400 KB |
Output is correct |
41 |
Correct |
703 ms |
83980 KB |
Output is correct |
42 |
Correct |
781 ms |
110464 KB |
Output is correct |
43 |
Correct |
764 ms |
120164 KB |
Output is correct |
44 |
Correct |
605 ms |
77204 KB |
Output is correct |
45 |
Correct |
634 ms |
85404 KB |
Output is correct |
46 |
Correct |
254 ms |
53572 KB |
Output is correct |
47 |
Correct |
781 ms |
96064 KB |
Output is correct |
48 |
Correct |
652 ms |
104424 KB |
Output is correct |