#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();
if(_ != dist[u]) continue;
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;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
692 ms |
84060 KB |
Output is correct |
5 |
Correct |
400 ms |
67748 KB |
Output is correct |
6 |
Correct |
303 ms |
68720 KB |
Output is correct |
7 |
Correct |
241 ms |
47704 KB |
Output is correct |
8 |
Correct |
200 ms |
48756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1144 KB |
Output is correct |
17 |
Correct |
4 ms |
1120 KB |
Output is correct |
18 |
Correct |
4 ms |
1256 KB |
Output is correct |
19 |
Correct |
4 ms |
1260 KB |
Output is correct |
20 |
Correct |
3 ms |
1128 KB |
Output is correct |
21 |
Correct |
4 ms |
1176 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
4 ms |
1124 KB |
Output is correct |
24 |
Correct |
3 ms |
1128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1144 KB |
Output is correct |
17 |
Correct |
4 ms |
1120 KB |
Output is correct |
18 |
Correct |
4 ms |
1256 KB |
Output is correct |
19 |
Correct |
4 ms |
1260 KB |
Output is correct |
20 |
Correct |
3 ms |
1128 KB |
Output is correct |
21 |
Correct |
4 ms |
1176 KB |
Output is correct |
22 |
Correct |
2 ms |
872 KB |
Output is correct |
23 |
Correct |
4 ms |
1124 KB |
Output is correct |
24 |
Correct |
3 ms |
1128 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
7 ms |
1136 KB |
Output is correct |
28 |
Correct |
7 ms |
1128 KB |
Output is correct |
29 |
Correct |
8 ms |
1120 KB |
Output is correct |
30 |
Correct |
6 ms |
1120 KB |
Output is correct |
31 |
Correct |
6 ms |
1136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
692 ms |
84060 KB |
Output is correct |
5 |
Correct |
400 ms |
67748 KB |
Output is correct |
6 |
Correct |
303 ms |
68720 KB |
Output is correct |
7 |
Correct |
241 ms |
47704 KB |
Output is correct |
8 |
Correct |
200 ms |
48756 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
320 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 |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
1144 KB |
Output is correct |
25 |
Correct |
4 ms |
1120 KB |
Output is correct |
26 |
Correct |
4 ms |
1256 KB |
Output is correct |
27 |
Correct |
4 ms |
1260 KB |
Output is correct |
28 |
Correct |
3 ms |
1128 KB |
Output is correct |
29 |
Correct |
4 ms |
1176 KB |
Output is correct |
30 |
Correct |
2 ms |
872 KB |
Output is correct |
31 |
Correct |
4 ms |
1124 KB |
Output is correct |
32 |
Correct |
3 ms |
1128 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
7 ms |
1136 KB |
Output is correct |
36 |
Correct |
7 ms |
1128 KB |
Output is correct |
37 |
Correct |
8 ms |
1120 KB |
Output is correct |
38 |
Correct |
6 ms |
1120 KB |
Output is correct |
39 |
Correct |
6 ms |
1136 KB |
Output is correct |
40 |
Correct |
947 ms |
88196 KB |
Output is correct |
41 |
Correct |
620 ms |
70496 KB |
Output is correct |
42 |
Correct |
765 ms |
86276 KB |
Output is correct |
43 |
Correct |
723 ms |
86024 KB |
Output is correct |
44 |
Correct |
496 ms |
71024 KB |
Output is correct |
45 |
Correct |
532 ms |
72312 KB |
Output is correct |
46 |
Correct |
205 ms |
26452 KB |
Output is correct |
47 |
Correct |
626 ms |
73184 KB |
Output is correct |
48 |
Correct |
591 ms |
71924 KB |
Output is correct |