#include <bits/stdc++.h>
#define ii pair <int, int>
#define F first
#define S second
#define pb push_back
using namespace std;
const int N = 2e5 + 5;
int n, pos[N];
vector <ii> adj[4 * N];
int dist[3][4 * N];
int mx = 0, used[4 * N];
void build(int s, int l, int r){
used[s] = false;
for (int i = 0; i <= 2; ++ i) dist[i][s] = 1e9;
mx = max(mx, s);
if (l == r){
if (l != 1 and l != n) used[s] = true;
pos[l] = s;
return;
}
int mid = l + r >> 1;
build(2 * s, l, mid);
build(2 * s + 1, mid + 1, r);
adj[2 * s].pb(ii(s, 0));
adj[2 * s + 1].pb(ii(s, 0));
}
void get(int s, int l, int r, int u, int v, int pos){
if (u <= l and r <= v) {
adj[s].pb(ii(pos, 1));
return;
}
int mid = l + r >> 1;
if (mid >= u) get(2 * s, l, mid, u, v, pos);
if (mid + 1 <= v) get(2 * s + 1, mid + 1, r, u, v, pos);
}
priority_queue <ii> pq;
void dijktra(int t){
while (!pq.empty()){
int u = pq.top().S;
int du = -pq.top().F;
//if (t == 2) cout << u << ' ' << dist[t][u] << endl;
pq.pop();
if (du != dist[t][u]) continue;
for (ii v : adj[u]){
if (dist[t][v.F] > du + v.S){
dist[t][v.F] = du + v.S;
pq.push(ii(-dist[t][v.F], v.F));
}
}
}
}
void solve(){
cin >> n;
build(1, 1, n);
for (int i = 1; i <= n; ++ i){
int l, r; cin >> l >> r;
get(1, 1, n, l, r, pos[i]);
}
dist[0][pos[1]] = 0;
pq.push(ii(0, pos[1]));
dijktra(0);
dist[1][pos[n]] = 0;
pq.push(ii(0, pos[n]));
dijktra(1);
//cout << endl;
for (int i = 1; i <= mx; ++ i) {
//cout << dist[0][i] << ' ' << dist[1][i] << endl;
dist[2][i] = dist[0][i] + dist[1][i];
if (used[i] == true) dist[2][i] --;
//cout << dist[2][i] << endl;
pq.push(ii(-dist[2][i], i));
}
dijktra(2);
int q; cin >> q;
while (q --){
int x; cin >> x;
if (dist[2][pos[x]] >= 1e9) cout << -1 << '\n';
else cout << dist[2][pos[x]] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Compilation message
passport.cpp: In function 'void build(int, int, int)':
passport.cpp:26:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void get(int, int, int, int, int, int)':
passport.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23384 KB |
Output is correct |
2 |
Correct |
5 ms |
23388 KB |
Output is correct |
3 |
Correct |
5 ms |
23388 KB |
Output is correct |
4 |
Correct |
591 ms |
79152 KB |
Output is correct |
5 |
Correct |
289 ms |
59072 KB |
Output is correct |
6 |
Correct |
163 ms |
55252 KB |
Output is correct |
7 |
Correct |
215 ms |
52068 KB |
Output is correct |
8 |
Correct |
182 ms |
59568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23388 KB |
Output is correct |
2 |
Correct |
4 ms |
23240 KB |
Output is correct |
3 |
Correct |
5 ms |
23380 KB |
Output is correct |
4 |
Correct |
5 ms |
23388 KB |
Output is correct |
5 |
Correct |
5 ms |
23388 KB |
Output is correct |
6 |
Correct |
5 ms |
23388 KB |
Output is correct |
7 |
Correct |
6 ms |
23388 KB |
Output is correct |
8 |
Correct |
5 ms |
23388 KB |
Output is correct |
9 |
Correct |
5 ms |
23388 KB |
Output is correct |
10 |
Correct |
5 ms |
23236 KB |
Output is correct |
11 |
Correct |
5 ms |
23388 KB |
Output is correct |
12 |
Correct |
5 ms |
23388 KB |
Output is correct |
13 |
Correct |
5 ms |
23388 KB |
Output is correct |
14 |
Correct |
5 ms |
23388 KB |
Output is correct |
15 |
Correct |
5 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23388 KB |
Output is correct |
2 |
Correct |
4 ms |
23240 KB |
Output is correct |
3 |
Correct |
5 ms |
23380 KB |
Output is correct |
4 |
Correct |
5 ms |
23388 KB |
Output is correct |
5 |
Correct |
5 ms |
23388 KB |
Output is correct |
6 |
Correct |
5 ms |
23388 KB |
Output is correct |
7 |
Correct |
6 ms |
23388 KB |
Output is correct |
8 |
Correct |
5 ms |
23388 KB |
Output is correct |
9 |
Correct |
5 ms |
23388 KB |
Output is correct |
10 |
Correct |
5 ms |
23236 KB |
Output is correct |
11 |
Correct |
5 ms |
23388 KB |
Output is correct |
12 |
Correct |
5 ms |
23388 KB |
Output is correct |
13 |
Correct |
5 ms |
23388 KB |
Output is correct |
14 |
Correct |
5 ms |
23388 KB |
Output is correct |
15 |
Correct |
5 ms |
23388 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23644 KB |
Output is correct |
18 |
Correct |
9 ms |
23900 KB |
Output is correct |
19 |
Correct |
8 ms |
23896 KB |
Output is correct |
20 |
Correct |
6 ms |
23640 KB |
Output is correct |
21 |
Correct |
7 ms |
23644 KB |
Output is correct |
22 |
Correct |
7 ms |
23644 KB |
Output is correct |
23 |
Correct |
8 ms |
23900 KB |
Output is correct |
24 |
Correct |
7 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23388 KB |
Output is correct |
2 |
Correct |
4 ms |
23240 KB |
Output is correct |
3 |
Correct |
5 ms |
23380 KB |
Output is correct |
4 |
Correct |
5 ms |
23388 KB |
Output is correct |
5 |
Correct |
5 ms |
23388 KB |
Output is correct |
6 |
Correct |
5 ms |
23388 KB |
Output is correct |
7 |
Correct |
6 ms |
23388 KB |
Output is correct |
8 |
Correct |
5 ms |
23388 KB |
Output is correct |
9 |
Correct |
5 ms |
23388 KB |
Output is correct |
10 |
Correct |
5 ms |
23236 KB |
Output is correct |
11 |
Correct |
5 ms |
23388 KB |
Output is correct |
12 |
Correct |
5 ms |
23388 KB |
Output is correct |
13 |
Correct |
5 ms |
23388 KB |
Output is correct |
14 |
Correct |
5 ms |
23388 KB |
Output is correct |
15 |
Correct |
5 ms |
23388 KB |
Output is correct |
16 |
Correct |
8 ms |
23900 KB |
Output is correct |
17 |
Correct |
8 ms |
23644 KB |
Output is correct |
18 |
Correct |
9 ms |
23900 KB |
Output is correct |
19 |
Correct |
8 ms |
23896 KB |
Output is correct |
20 |
Correct |
6 ms |
23640 KB |
Output is correct |
21 |
Correct |
7 ms |
23644 KB |
Output is correct |
22 |
Correct |
7 ms |
23644 KB |
Output is correct |
23 |
Correct |
8 ms |
23900 KB |
Output is correct |
24 |
Correct |
7 ms |
23900 KB |
Output is correct |
25 |
Correct |
5 ms |
23388 KB |
Output is correct |
26 |
Correct |
5 ms |
23228 KB |
Output is correct |
27 |
Correct |
10 ms |
23896 KB |
Output is correct |
28 |
Correct |
8 ms |
23660 KB |
Output is correct |
29 |
Correct |
7 ms |
23852 KB |
Output is correct |
30 |
Correct |
8 ms |
23644 KB |
Output is correct |
31 |
Correct |
8 ms |
23644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23384 KB |
Output is correct |
2 |
Correct |
5 ms |
23388 KB |
Output is correct |
3 |
Correct |
5 ms |
23388 KB |
Output is correct |
4 |
Correct |
591 ms |
79152 KB |
Output is correct |
5 |
Correct |
289 ms |
59072 KB |
Output is correct |
6 |
Correct |
163 ms |
55252 KB |
Output is correct |
7 |
Correct |
215 ms |
52068 KB |
Output is correct |
8 |
Correct |
182 ms |
59568 KB |
Output is correct |
9 |
Correct |
4 ms |
23388 KB |
Output is correct |
10 |
Correct |
4 ms |
23240 KB |
Output is correct |
11 |
Correct |
5 ms |
23380 KB |
Output is correct |
12 |
Correct |
5 ms |
23388 KB |
Output is correct |
13 |
Correct |
5 ms |
23388 KB |
Output is correct |
14 |
Correct |
5 ms |
23388 KB |
Output is correct |
15 |
Correct |
6 ms |
23388 KB |
Output is correct |
16 |
Correct |
5 ms |
23388 KB |
Output is correct |
17 |
Correct |
5 ms |
23388 KB |
Output is correct |
18 |
Correct |
5 ms |
23236 KB |
Output is correct |
19 |
Correct |
5 ms |
23388 KB |
Output is correct |
20 |
Correct |
5 ms |
23388 KB |
Output is correct |
21 |
Correct |
5 ms |
23388 KB |
Output is correct |
22 |
Correct |
5 ms |
23388 KB |
Output is correct |
23 |
Correct |
5 ms |
23388 KB |
Output is correct |
24 |
Correct |
8 ms |
23900 KB |
Output is correct |
25 |
Correct |
8 ms |
23644 KB |
Output is correct |
26 |
Correct |
9 ms |
23900 KB |
Output is correct |
27 |
Correct |
8 ms |
23896 KB |
Output is correct |
28 |
Correct |
6 ms |
23640 KB |
Output is correct |
29 |
Correct |
7 ms |
23644 KB |
Output is correct |
30 |
Correct |
7 ms |
23644 KB |
Output is correct |
31 |
Correct |
8 ms |
23900 KB |
Output is correct |
32 |
Correct |
7 ms |
23900 KB |
Output is correct |
33 |
Correct |
5 ms |
23388 KB |
Output is correct |
34 |
Correct |
5 ms |
23228 KB |
Output is correct |
35 |
Correct |
10 ms |
23896 KB |
Output is correct |
36 |
Correct |
8 ms |
23660 KB |
Output is correct |
37 |
Correct |
7 ms |
23852 KB |
Output is correct |
38 |
Correct |
8 ms |
23644 KB |
Output is correct |
39 |
Correct |
8 ms |
23644 KB |
Output is correct |
40 |
Correct |
604 ms |
80756 KB |
Output is correct |
41 |
Correct |
323 ms |
61960 KB |
Output is correct |
42 |
Correct |
416 ms |
88620 KB |
Output is correct |
43 |
Correct |
418 ms |
88340 KB |
Output is correct |
44 |
Correct |
194 ms |
56512 KB |
Output is correct |
45 |
Correct |
265 ms |
63900 KB |
Output is correct |
46 |
Correct |
125 ms |
40132 KB |
Output is correct |
47 |
Correct |
387 ms |
63544 KB |
Output is correct |
48 |
Correct |
316 ms |
71520 KB |
Output is correct |