#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 1e9;
int n, l[N], r[N];
int dl[5 * N], dr[5 * N], dist[5 * N], gde[N];
void Init(int node, int l, int r) {
if (l == r) {gde[l] = node; return;}
int mid = l + r >> 1;
Init(2 * node, l, mid);
Init(2 * node + 1, mid + 1, r);
}
vector<int> g[5 * N];
void Add(int node, int l, int r, int ql, int qr, int i) {
if (r < ql || qr < l) return;
if (ql <= l && r <= qr) {
g[i].push_back(n + node);
g[n + node].push_back(i);
return;
}
int mid = l + r >> 1;
Add(2 * node, l, mid, ql, qr, i);
Add(2 * node + 1, mid + 1, r, ql, qr, i);
}
void Bfs(int* d) {
set<pair<int, int>> st;
for (int i = 1; i <= n; i++) st.insert({d[i], i});
while (!st.empty()) {
auto it = st.begin();
int s = it->second;
st.erase(it);
if (s <= n) {
for (int u = gde[s]; u >= 1; u /= 2) {
if (d[u + n] > d[s]) {
if (st.find({d[u + n], u + n}) != st.end()) st.erase({d[u + n], u + n});
d[u + n] = d[s];
st.insert({d[u + n], u + n});
}
}
continue;
}
for (int u : g[s]) {
if (d[u] > d[s] + 1) {
if (st.find({d[u], u}) != st.end()) st.erase({d[u], u});
d[u] = d[s] + 1;
st.insert({d[u], u});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
Init(1, 1, n);
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
Add(1, 1, n, l[i], r[i], i);
}
for (int i = 1; i <= 5 * n; i++) dl[i] = dr[i] = dist[i] = inf;
dl[1] = 0; dr[n] = 0;
Bfs(dl); Bfs(dr);
dl[1] = 1; dr[n] = 1;
for (int i = 1; i <= n; i++) dist[i] = dl[i] + dr[i];
Bfs(dist);
for (int i = 1; i <= n; i++) {
if (dist[i] >= inf) dist[i] = -1;
else dist[i] -= 1;
}
int q; cin >> q;
while (q--) {
int x; cin >> x;
cout << dist[x] << en;
}
return 0;
}
Compilation message
passport.cpp: In function 'void Init(int, int, int)':
passport.cpp:15:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void Add(int, int, int, int, int, int)':
passport.cpp:27:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23796 KB |
Output is correct |
4 |
Correct |
1295 ms |
93460 KB |
Output is correct |
5 |
Correct |
730 ms |
66612 KB |
Output is correct |
6 |
Correct |
644 ms |
74864 KB |
Output is correct |
7 |
Correct |
609 ms |
63748 KB |
Output is correct |
8 |
Correct |
348 ms |
61004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23824 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
12 ms |
23832 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23836 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
15 ms |
23912 KB |
Output is correct |
12 |
Correct |
14 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23844 KB |
Output is correct |
14 |
Correct |
13 ms |
23892 KB |
Output is correct |
15 |
Correct |
13 ms |
23812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23824 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
12 ms |
23832 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23836 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
15 ms |
23912 KB |
Output is correct |
12 |
Correct |
14 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23844 KB |
Output is correct |
14 |
Correct |
13 ms |
23892 KB |
Output is correct |
15 |
Correct |
13 ms |
23812 KB |
Output is correct |
16 |
Correct |
20 ms |
24404 KB |
Output is correct |
17 |
Correct |
19 ms |
24348 KB |
Output is correct |
18 |
Correct |
20 ms |
24520 KB |
Output is correct |
19 |
Correct |
19 ms |
24448 KB |
Output is correct |
20 |
Correct |
19 ms |
24404 KB |
Output is correct |
21 |
Correct |
17 ms |
24304 KB |
Output is correct |
22 |
Correct |
16 ms |
24276 KB |
Output is correct |
23 |
Correct |
18 ms |
24404 KB |
Output is correct |
24 |
Correct |
17 ms |
24404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23824 KB |
Output is correct |
2 |
Correct |
13 ms |
23812 KB |
Output is correct |
3 |
Correct |
12 ms |
23832 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23836 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23768 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
15 ms |
23912 KB |
Output is correct |
12 |
Correct |
14 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23844 KB |
Output is correct |
14 |
Correct |
13 ms |
23892 KB |
Output is correct |
15 |
Correct |
13 ms |
23812 KB |
Output is correct |
16 |
Correct |
20 ms |
24404 KB |
Output is correct |
17 |
Correct |
19 ms |
24348 KB |
Output is correct |
18 |
Correct |
20 ms |
24520 KB |
Output is correct |
19 |
Correct |
19 ms |
24448 KB |
Output is correct |
20 |
Correct |
19 ms |
24404 KB |
Output is correct |
21 |
Correct |
17 ms |
24304 KB |
Output is correct |
22 |
Correct |
16 ms |
24276 KB |
Output is correct |
23 |
Correct |
18 ms |
24404 KB |
Output is correct |
24 |
Correct |
17 ms |
24404 KB |
Output is correct |
25 |
Correct |
12 ms |
23784 KB |
Output is correct |
26 |
Correct |
13 ms |
23732 KB |
Output is correct |
27 |
Correct |
19 ms |
24532 KB |
Output is correct |
28 |
Correct |
22 ms |
24368 KB |
Output is correct |
29 |
Correct |
18 ms |
24464 KB |
Output is correct |
30 |
Correct |
18 ms |
24276 KB |
Output is correct |
31 |
Correct |
20 ms |
24404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23796 KB |
Output is correct |
4 |
Correct |
1295 ms |
93460 KB |
Output is correct |
5 |
Correct |
730 ms |
66612 KB |
Output is correct |
6 |
Correct |
644 ms |
74864 KB |
Output is correct |
7 |
Correct |
609 ms |
63748 KB |
Output is correct |
8 |
Correct |
348 ms |
61004 KB |
Output is correct |
9 |
Correct |
12 ms |
23824 KB |
Output is correct |
10 |
Correct |
13 ms |
23812 KB |
Output is correct |
11 |
Correct |
12 ms |
23832 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23836 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23768 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
15 ms |
23912 KB |
Output is correct |
20 |
Correct |
14 ms |
23892 KB |
Output is correct |
21 |
Correct |
13 ms |
23844 KB |
Output is correct |
22 |
Correct |
13 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23812 KB |
Output is correct |
24 |
Correct |
20 ms |
24404 KB |
Output is correct |
25 |
Correct |
19 ms |
24348 KB |
Output is correct |
26 |
Correct |
20 ms |
24520 KB |
Output is correct |
27 |
Correct |
19 ms |
24448 KB |
Output is correct |
28 |
Correct |
19 ms |
24404 KB |
Output is correct |
29 |
Correct |
17 ms |
24304 KB |
Output is correct |
30 |
Correct |
16 ms |
24276 KB |
Output is correct |
31 |
Correct |
18 ms |
24404 KB |
Output is correct |
32 |
Correct |
17 ms |
24404 KB |
Output is correct |
33 |
Correct |
12 ms |
23784 KB |
Output is correct |
34 |
Correct |
13 ms |
23732 KB |
Output is correct |
35 |
Correct |
19 ms |
24532 KB |
Output is correct |
36 |
Correct |
22 ms |
24368 KB |
Output is correct |
37 |
Correct |
18 ms |
24464 KB |
Output is correct |
38 |
Correct |
18 ms |
24276 KB |
Output is correct |
39 |
Correct |
20 ms |
24404 KB |
Output is correct |
40 |
Correct |
1301 ms |
96020 KB |
Output is correct |
41 |
Correct |
755 ms |
68696 KB |
Output is correct |
42 |
Correct |
872 ms |
92848 KB |
Output is correct |
43 |
Correct |
871 ms |
96344 KB |
Output is correct |
44 |
Correct |
617 ms |
77060 KB |
Output is correct |
45 |
Correct |
589 ms |
71904 KB |
Output is correct |
46 |
Correct |
346 ms |
43180 KB |
Output is correct |
47 |
Correct |
984 ms |
81100 KB |
Output is correct |
48 |
Correct |
690 ms |
81852 KB |
Output is correct |