#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
vector<vector<int>> tree(4 * n);
function<void(int, int, int, int, int, int)> wrzuc = [&](int v, int l, int r, int ql, int qr, int i) {
if (ql <= l && r <= qr) {
tree[v].push_back(i);
return;
}
int m = (l + r) / 2;
if (ql <= m) wrzuc(2 * v, l, m, ql, qr, i);
if (m < qr) wrzuc(2 * v + 1, m + 1, r, ql, qr, i);
};
function<int(int, int, int, int)> szuk = [&](int v, int l, int r, int x) {
if (l == r) return v;
int m = (l + r) / 2;
if (x <= m) return szuk(2 * v, l, m, x);
else return szuk(2 * v + 1, m + 1, r, x);
};
for (int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
l--, r--;
wrzuc(1, 0, n - 1, l, r, i);
}
const int oo = 1e9 + 7;
auto sssp = [&](vector<int> ini) {
auto dist = ini;
auto tree_save = tree;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int i = 0; i < n; i++) if (ini[i] != oo) q.emplace(ini[i], i);
while (!q.empty()) {
auto [_, u] = q.top();
q.pop();
u = szuk(1, 0, n - 1, u);
while (u) {
while (!tree[u].empty()) {
int to = tree[u].back();
if (dist[to] > _ + 1) q.emplace(dist[to] = _ + 1, to);
tree[u].pop_back();
}
u /= 2;
}
}
tree = tree_save;
return dist;
};
vector<int> dist_0(n, oo); dist_0[0] = 0; dist_0 = sssp(dist_0);
vector<int> dist_n(n, oo); dist_n[n - 1] = 0; dist_n = sssp(dist_n);
vector<int> dist_merge(n);
dist_0[0]++; dist_n[n - 1]++;
for (int i = 0; i < n; i++) dist_merge[i] = dist_0[i] + dist_n[i];
dist_merge = sssp(dist_merge);
int q; cin >> q;
while (q--) {
int x; cin >> x;
x--;
cout << (dist_merge[x] >= oo ? -1 : dist_merge[x] - 1) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
787 ms |
84072 KB |
Output is correct |
5 |
Correct |
409 ms |
67752 KB |
Output is correct |
6 |
Correct |
271 ms |
68688 KB |
Output is correct |
7 |
Correct |
227 ms |
47480 KB |
Output is correct |
8 |
Correct |
179 ms |
48752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
5 ms |
1144 KB |
Output is correct |
17 |
Correct |
4 ms |
1128 KB |
Output is correct |
18 |
Correct |
5 ms |
1256 KB |
Output is correct |
19 |
Correct |
5 ms |
1260 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1124 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
4 ms |
1128 KB |
Output is correct |
24 |
Correct |
4 ms |
1128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
5 ms |
1144 KB |
Output is correct |
17 |
Correct |
4 ms |
1128 KB |
Output is correct |
18 |
Correct |
5 ms |
1256 KB |
Output is correct |
19 |
Correct |
5 ms |
1260 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1124 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
4 ms |
1128 KB |
Output is correct |
24 |
Correct |
4 ms |
1128 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
320 KB |
Output is correct |
27 |
Correct |
7 ms |
1264 KB |
Output is correct |
28 |
Correct |
7 ms |
1116 KB |
Output is correct |
29 |
Correct |
6 ms |
1128 KB |
Output is correct |
30 |
Correct |
7 ms |
1128 KB |
Output is correct |
31 |
Correct |
10 ms |
1136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
787 ms |
84072 KB |
Output is correct |
5 |
Correct |
409 ms |
67752 KB |
Output is correct |
6 |
Correct |
271 ms |
68688 KB |
Output is correct |
7 |
Correct |
227 ms |
47480 KB |
Output is correct |
8 |
Correct |
179 ms |
48752 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
5 ms |
1144 KB |
Output is correct |
25 |
Correct |
4 ms |
1128 KB |
Output is correct |
26 |
Correct |
5 ms |
1256 KB |
Output is correct |
27 |
Correct |
5 ms |
1260 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
4 ms |
1124 KB |
Output is correct |
30 |
Correct |
2 ms |
872 KB |
Output is correct |
31 |
Correct |
4 ms |
1128 KB |
Output is correct |
32 |
Correct |
4 ms |
1128 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
320 KB |
Output is correct |
35 |
Correct |
7 ms |
1264 KB |
Output is correct |
36 |
Correct |
7 ms |
1116 KB |
Output is correct |
37 |
Correct |
6 ms |
1128 KB |
Output is correct |
38 |
Correct |
7 ms |
1128 KB |
Output is correct |
39 |
Correct |
10 ms |
1136 KB |
Output is correct |
40 |
Correct |
944 ms |
88368 KB |
Output is correct |
41 |
Correct |
702 ms |
70660 KB |
Output is correct |
42 |
Correct |
783 ms |
86460 KB |
Output is correct |
43 |
Correct |
812 ms |
86228 KB |
Output is correct |
44 |
Correct |
486 ms |
71388 KB |
Output is correct |
45 |
Correct |
545 ms |
72396 KB |
Output is correct |
46 |
Correct |
208 ms |
26448 KB |
Output is correct |
47 |
Correct |
627 ms |
73252 KB |
Output is correct |
48 |
Correct |
564 ms |
71992 KB |
Output is correct |