#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 8e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
int id[N], cnt;
vector <pii> a[N];
void build (int i, int l, int r){
maxz (cnt, i);
if (l == r){
id[l] = i;
return;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
a[i << 1].pb({i, 0});
a[i << 1 | 1].pb({i, 0});
}
void add (int i, int l, int r, int u, int v, int x){
if (l > v || r < u) return;
if (l >= u && r <= v){
a[i].pb({x, 1});
return;
}
int mid = l + r >> 1;
add(i << 1, l, mid, u, v, x);
add(i << 1 | 1, mid + 1, r, u, v, x);
}
int n, l[N], r[N], d[3][N];
void go (int z){
priority_queue<pii, vector <pii>, greater<pii>> q;
forr (i, 1, cnt){
q.push({d[z][i], i});
}
while (!q.empty()) {
pii t = q.top();
q.pop();
int u = t.nd;
if (t.st != d[z][u] || t.st > n) continue;
for (pii e : a[u]){
int v = e.st, w = e.nd;
if (minz(d[z][v], d[z][u] + w)){
q.push({d[z][v], v});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef kaguya
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n;
build(1, 1, n);
forr (i, 1, n){
cin >> l[i] >> r[i];
add(1, 1, n, l[i], r[i], id[i]);
}
memset (d, 63, sizeof d);
d[0][id[1]] = 0;
go(0);
d[1][id[n]] = 0;
go(1);
forr (i, 1, n){
int u = id[i];
if (max(d[0][u], d[1][u]) > n){
continue;
}
d[2][u] = d[1][u] + d[0][u] - (i != n && i != 1);
}
go(2);
int qq, zz;
cin >> qq;
while (qq--){
cin >> zz;
zz = id[zz];
cout << (d[2][zz] <= n ? d[2][zz] : -1) << "\n";
}
return 0;
}
/*
*/
Compilation message
passport.cpp: In function 'void build(int, int, int)':
passport.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void add(int, int, int, int, int, int)':
passport.cpp:64:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
33360 KB |
Output is correct |
2 |
Correct |
6 ms |
33472 KB |
Output is correct |
3 |
Correct |
5 ms |
33360 KB |
Output is correct |
4 |
Correct |
679 ms |
89712 KB |
Output is correct |
5 |
Correct |
435 ms |
68664 KB |
Output is correct |
6 |
Correct |
352 ms |
61940 KB |
Output is correct |
7 |
Correct |
324 ms |
65332 KB |
Output is correct |
8 |
Correct |
266 ms |
69156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
32336 KB |
Output is correct |
2 |
Correct |
12 ms |
32336 KB |
Output is correct |
3 |
Correct |
11 ms |
32336 KB |
Output is correct |
4 |
Correct |
13 ms |
29520 KB |
Output is correct |
5 |
Correct |
11 ms |
32336 KB |
Output is correct |
6 |
Correct |
11 ms |
32336 KB |
Output is correct |
7 |
Correct |
11 ms |
30388 KB |
Output is correct |
8 |
Correct |
11 ms |
32336 KB |
Output is correct |
9 |
Correct |
11 ms |
33360 KB |
Output is correct |
10 |
Correct |
13 ms |
29520 KB |
Output is correct |
11 |
Correct |
14 ms |
29720 KB |
Output is correct |
12 |
Correct |
12 ms |
29776 KB |
Output is correct |
13 |
Correct |
11 ms |
32336 KB |
Output is correct |
14 |
Correct |
11 ms |
30288 KB |
Output is correct |
15 |
Correct |
11 ms |
30396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
32336 KB |
Output is correct |
2 |
Correct |
12 ms |
32336 KB |
Output is correct |
3 |
Correct |
11 ms |
32336 KB |
Output is correct |
4 |
Correct |
13 ms |
29520 KB |
Output is correct |
5 |
Correct |
11 ms |
32336 KB |
Output is correct |
6 |
Correct |
11 ms |
32336 KB |
Output is correct |
7 |
Correct |
11 ms |
30388 KB |
Output is correct |
8 |
Correct |
11 ms |
32336 KB |
Output is correct |
9 |
Correct |
11 ms |
33360 KB |
Output is correct |
10 |
Correct |
13 ms |
29520 KB |
Output is correct |
11 |
Correct |
14 ms |
29720 KB |
Output is correct |
12 |
Correct |
12 ms |
29776 KB |
Output is correct |
13 |
Correct |
11 ms |
32336 KB |
Output is correct |
14 |
Correct |
11 ms |
30288 KB |
Output is correct |
15 |
Correct |
11 ms |
30396 KB |
Output is correct |
16 |
Correct |
15 ms |
34052 KB |
Output is correct |
17 |
Correct |
16 ms |
32848 KB |
Output is correct |
18 |
Correct |
19 ms |
30492 KB |
Output is correct |
19 |
Correct |
16 ms |
30476 KB |
Output is correct |
20 |
Correct |
14 ms |
30544 KB |
Output is correct |
21 |
Correct |
17 ms |
30032 KB |
Output is correct |
22 |
Correct |
12 ms |
30800 KB |
Output is correct |
23 |
Correct |
16 ms |
30288 KB |
Output is correct |
24 |
Correct |
18 ms |
30304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
32336 KB |
Output is correct |
2 |
Correct |
12 ms |
32336 KB |
Output is correct |
3 |
Correct |
11 ms |
32336 KB |
Output is correct |
4 |
Correct |
13 ms |
29520 KB |
Output is correct |
5 |
Correct |
11 ms |
32336 KB |
Output is correct |
6 |
Correct |
11 ms |
32336 KB |
Output is correct |
7 |
Correct |
11 ms |
30388 KB |
Output is correct |
8 |
Correct |
11 ms |
32336 KB |
Output is correct |
9 |
Correct |
11 ms |
33360 KB |
Output is correct |
10 |
Correct |
13 ms |
29520 KB |
Output is correct |
11 |
Correct |
14 ms |
29720 KB |
Output is correct |
12 |
Correct |
12 ms |
29776 KB |
Output is correct |
13 |
Correct |
11 ms |
32336 KB |
Output is correct |
14 |
Correct |
11 ms |
30288 KB |
Output is correct |
15 |
Correct |
11 ms |
30396 KB |
Output is correct |
16 |
Correct |
15 ms |
34052 KB |
Output is correct |
17 |
Correct |
16 ms |
32848 KB |
Output is correct |
18 |
Correct |
19 ms |
30492 KB |
Output is correct |
19 |
Correct |
16 ms |
30476 KB |
Output is correct |
20 |
Correct |
14 ms |
30544 KB |
Output is correct |
21 |
Correct |
17 ms |
30032 KB |
Output is correct |
22 |
Correct |
12 ms |
30800 KB |
Output is correct |
23 |
Correct |
16 ms |
30288 KB |
Output is correct |
24 |
Correct |
18 ms |
30304 KB |
Output is correct |
25 |
Correct |
10 ms |
33532 KB |
Output is correct |
26 |
Correct |
15 ms |
29776 KB |
Output is correct |
27 |
Correct |
16 ms |
30340 KB |
Output is correct |
28 |
Correct |
17 ms |
32912 KB |
Output is correct |
29 |
Correct |
17 ms |
33616 KB |
Output is correct |
30 |
Correct |
16 ms |
33872 KB |
Output is correct |
31 |
Correct |
17 ms |
30288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
33360 KB |
Output is correct |
2 |
Correct |
6 ms |
33472 KB |
Output is correct |
3 |
Correct |
5 ms |
33360 KB |
Output is correct |
4 |
Correct |
679 ms |
89712 KB |
Output is correct |
5 |
Correct |
435 ms |
68664 KB |
Output is correct |
6 |
Correct |
352 ms |
61940 KB |
Output is correct |
7 |
Correct |
324 ms |
65332 KB |
Output is correct |
8 |
Correct |
266 ms |
69156 KB |
Output is correct |
9 |
Correct |
11 ms |
32336 KB |
Output is correct |
10 |
Correct |
12 ms |
32336 KB |
Output is correct |
11 |
Correct |
11 ms |
32336 KB |
Output is correct |
12 |
Correct |
13 ms |
29520 KB |
Output is correct |
13 |
Correct |
11 ms |
32336 KB |
Output is correct |
14 |
Correct |
11 ms |
32336 KB |
Output is correct |
15 |
Correct |
11 ms |
30388 KB |
Output is correct |
16 |
Correct |
11 ms |
32336 KB |
Output is correct |
17 |
Correct |
11 ms |
33360 KB |
Output is correct |
18 |
Correct |
13 ms |
29520 KB |
Output is correct |
19 |
Correct |
14 ms |
29720 KB |
Output is correct |
20 |
Correct |
12 ms |
29776 KB |
Output is correct |
21 |
Correct |
11 ms |
32336 KB |
Output is correct |
22 |
Correct |
11 ms |
30288 KB |
Output is correct |
23 |
Correct |
11 ms |
30396 KB |
Output is correct |
24 |
Correct |
15 ms |
34052 KB |
Output is correct |
25 |
Correct |
16 ms |
32848 KB |
Output is correct |
26 |
Correct |
19 ms |
30492 KB |
Output is correct |
27 |
Correct |
16 ms |
30476 KB |
Output is correct |
28 |
Correct |
14 ms |
30544 KB |
Output is correct |
29 |
Correct |
17 ms |
30032 KB |
Output is correct |
30 |
Correct |
12 ms |
30800 KB |
Output is correct |
31 |
Correct |
16 ms |
30288 KB |
Output is correct |
32 |
Correct |
18 ms |
30304 KB |
Output is correct |
33 |
Correct |
10 ms |
33532 KB |
Output is correct |
34 |
Correct |
15 ms |
29776 KB |
Output is correct |
35 |
Correct |
16 ms |
30340 KB |
Output is correct |
36 |
Correct |
17 ms |
32912 KB |
Output is correct |
37 |
Correct |
17 ms |
33616 KB |
Output is correct |
38 |
Correct |
16 ms |
33872 KB |
Output is correct |
39 |
Correct |
17 ms |
30288 KB |
Output is correct |
40 |
Correct |
790 ms |
91136 KB |
Output is correct |
41 |
Correct |
529 ms |
71180 KB |
Output is correct |
42 |
Correct |
579 ms |
95312 KB |
Output is correct |
43 |
Correct |
552 ms |
99368 KB |
Output is correct |
44 |
Correct |
381 ms |
62004 KB |
Output is correct |
45 |
Correct |
402 ms |
69780 KB |
Output is correct |
46 |
Correct |
198 ms |
48612 KB |
Output is correct |
47 |
Correct |
524 ms |
75160 KB |
Output is correct |
48 |
Correct |
430 ms |
82816 KB |
Output is correct |