#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 <= 5 * 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]) {
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) {
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;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23724 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Execution timed out |
2012 ms |
121680 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23748 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
18 ms |
23828 KB |
Output is correct |
7 |
Correct |
14 ms |
23760 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23808 KB |
Output is correct |
11 |
Correct |
15 ms |
23848 KB |
Output is correct |
12 |
Correct |
19 ms |
23896 KB |
Output is correct |
13 |
Correct |
15 ms |
23892 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
14 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23748 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
18 ms |
23828 KB |
Output is correct |
7 |
Correct |
14 ms |
23760 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23808 KB |
Output is correct |
11 |
Correct |
15 ms |
23848 KB |
Output is correct |
12 |
Correct |
19 ms |
23896 KB |
Output is correct |
13 |
Correct |
15 ms |
23892 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
14 ms |
23936 KB |
Output is correct |
16 |
Correct |
33 ms |
24788 KB |
Output is correct |
17 |
Correct |
26 ms |
24808 KB |
Output is correct |
18 |
Correct |
28 ms |
24880 KB |
Output is correct |
19 |
Correct |
31 ms |
25148 KB |
Output is correct |
20 |
Correct |
24 ms |
24724 KB |
Output is correct |
21 |
Correct |
25 ms |
24828 KB |
Output is correct |
22 |
Correct |
26 ms |
24660 KB |
Output is correct |
23 |
Correct |
33 ms |
24764 KB |
Output is correct |
24 |
Correct |
25 ms |
24840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23748 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
18 ms |
23828 KB |
Output is correct |
7 |
Correct |
14 ms |
23760 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23808 KB |
Output is correct |
11 |
Correct |
15 ms |
23848 KB |
Output is correct |
12 |
Correct |
19 ms |
23896 KB |
Output is correct |
13 |
Correct |
15 ms |
23892 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
14 ms |
23936 KB |
Output is correct |
16 |
Correct |
33 ms |
24788 KB |
Output is correct |
17 |
Correct |
26 ms |
24808 KB |
Output is correct |
18 |
Correct |
28 ms |
24880 KB |
Output is correct |
19 |
Correct |
31 ms |
25148 KB |
Output is correct |
20 |
Correct |
24 ms |
24724 KB |
Output is correct |
21 |
Correct |
25 ms |
24828 KB |
Output is correct |
22 |
Correct |
26 ms |
24660 KB |
Output is correct |
23 |
Correct |
33 ms |
24764 KB |
Output is correct |
24 |
Correct |
25 ms |
24840 KB |
Output is correct |
25 |
Correct |
13 ms |
23764 KB |
Output is correct |
26 |
Correct |
15 ms |
23764 KB |
Output is correct |
27 |
Correct |
29 ms |
24872 KB |
Output is correct |
28 |
Correct |
30 ms |
24788 KB |
Output is correct |
29 |
Correct |
25 ms |
24836 KB |
Output is correct |
30 |
Correct |
31 ms |
24824 KB |
Output is correct |
31 |
Correct |
27 ms |
24788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23724 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Execution timed out |
2012 ms |
121680 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |