#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 25;
const int inf = MAXN << 4;
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
int cnt;
vector <pair <int, int>> radj[MAXN];
void add_edge (int x, int y) {
radj[x].push_back({y, 0});
}
struct SegmentTree {
int tree[MAXN << 1];
int init (int l, int r, int node) {
if (l == r) return tree[node] = l;
int x = init(l, mid, tl), y = init(mid + 1, r, tr);
tree[node] = ++cnt;
add_edge(x, tree[node]); add_edge(y, tree[node]);
return tree[node];
}
void update (int l, int r, int a, int b, int c, int node) {
if (l > b || r < a) return;
if (l >= a && r <= b) {
add_edge(tree[node], c);
return;
}
update(l, mid, a, b, c, tl); update(mid + 1, r, a, b, c, tr);
}
} cur;
int n, q;
int dist1[MAXN], dist2[MAXN], dist3[MAXN];
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n; cnt = n;
cur.init(1, n, 1);
for (int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
++cnt;
radj[cnt].push_back({i, 1});
cur.update(1, n, l, r, cnt, 1);
}
for (int i = 1; i <= cnt; i++) dist1[i] = dist2[i] = inf;
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <>> cur;
cur.push({0, 1}); dist1[1] = 0;
while (!cur.empty()) {
auto k = cur.top();
cur.pop();
if (k.first > dist1[k.second]) continue;
for (auto j : radj[k.second]) {
if (k.first + j.second < dist1[j.first]) {
dist1[j.first] = k.first + j.second;
cur.push({dist1[j.first], j.first});
}
}
}
cur.push({0, n}); dist2[n] = 0;
while (!cur.empty()) {
auto k = cur.top();
cur.pop();
if (k.first > dist2[k.second]) continue;
for (auto j : radj[k.second]) {
if (k.first + j.second < dist2[j.first]) {
dist2[j.first] = k.first + j.second;
cur.push({dist2[j.first], j.first});
}
}
}
for (int i = 1; i <= cnt; i++) {
dist3[i] = dist1[i] + dist2[i];
cur.push({dist3[i], i});
}
while (!cur.empty()) {
auto k = cur.top();
cur.pop();
if (k.first > dist3[k.second]) continue;
for (auto j : radj[k.second]) {
if (k.first + j.second < dist3[j.first]) {
dist3[j.first] = k.first + j.second;
cur.push({dist3[j.first], j.first});
}
}
}
for (int i = 1; i <= cnt; i++) if (dist3[i] > n) dist3[i] = -1;
cin >> q;
while (q--) {
int x; cin >> x;
cout << dist3[x] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30812 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
7 ms |
30812 KB |
Output is correct |
4 |
Correct |
723 ms |
95040 KB |
Output is correct |
5 |
Correct |
370 ms |
76604 KB |
Output is correct |
6 |
Correct |
220 ms |
71948 KB |
Output is correct |
7 |
Correct |
266 ms |
70136 KB |
Output is correct |
8 |
Correct |
160 ms |
72048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
30812 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
7 ms |
30812 KB |
Output is correct |
4 |
Correct |
7 ms |
30936 KB |
Output is correct |
5 |
Correct |
7 ms |
30812 KB |
Output is correct |
6 |
Correct |
7 ms |
30812 KB |
Output is correct |
7 |
Correct |
7 ms |
30812 KB |
Output is correct |
8 |
Correct |
7 ms |
30812 KB |
Output is correct |
9 |
Correct |
7 ms |
30808 KB |
Output is correct |
10 |
Correct |
8 ms |
30808 KB |
Output is correct |
11 |
Correct |
7 ms |
31068 KB |
Output is correct |
12 |
Correct |
8 ms |
30940 KB |
Output is correct |
13 |
Correct |
8 ms |
31068 KB |
Output is correct |
14 |
Correct |
7 ms |
30896 KB |
Output is correct |
15 |
Correct |
7 ms |
31064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
30812 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
7 ms |
30812 KB |
Output is correct |
4 |
Correct |
7 ms |
30936 KB |
Output is correct |
5 |
Correct |
7 ms |
30812 KB |
Output is correct |
6 |
Correct |
7 ms |
30812 KB |
Output is correct |
7 |
Correct |
7 ms |
30812 KB |
Output is correct |
8 |
Correct |
7 ms |
30812 KB |
Output is correct |
9 |
Correct |
7 ms |
30808 KB |
Output is correct |
10 |
Correct |
8 ms |
30808 KB |
Output is correct |
11 |
Correct |
7 ms |
31068 KB |
Output is correct |
12 |
Correct |
8 ms |
30940 KB |
Output is correct |
13 |
Correct |
8 ms |
31068 KB |
Output is correct |
14 |
Correct |
7 ms |
30896 KB |
Output is correct |
15 |
Correct |
7 ms |
31064 KB |
Output is correct |
16 |
Correct |
11 ms |
31564 KB |
Output is correct |
17 |
Correct |
10 ms |
31324 KB |
Output is correct |
18 |
Correct |
11 ms |
31580 KB |
Output is correct |
19 |
Correct |
11 ms |
31664 KB |
Output is correct |
20 |
Correct |
9 ms |
31324 KB |
Output is correct |
21 |
Correct |
11 ms |
31324 KB |
Output is correct |
22 |
Correct |
9 ms |
31448 KB |
Output is correct |
23 |
Correct |
10 ms |
31324 KB |
Output is correct |
24 |
Correct |
9 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
30812 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
7 ms |
30812 KB |
Output is correct |
4 |
Correct |
7 ms |
30936 KB |
Output is correct |
5 |
Correct |
7 ms |
30812 KB |
Output is correct |
6 |
Correct |
7 ms |
30812 KB |
Output is correct |
7 |
Correct |
7 ms |
30812 KB |
Output is correct |
8 |
Correct |
7 ms |
30812 KB |
Output is correct |
9 |
Correct |
7 ms |
30808 KB |
Output is correct |
10 |
Correct |
8 ms |
30808 KB |
Output is correct |
11 |
Correct |
7 ms |
31068 KB |
Output is correct |
12 |
Correct |
8 ms |
30940 KB |
Output is correct |
13 |
Correct |
8 ms |
31068 KB |
Output is correct |
14 |
Correct |
7 ms |
30896 KB |
Output is correct |
15 |
Correct |
7 ms |
31064 KB |
Output is correct |
16 |
Correct |
11 ms |
31564 KB |
Output is correct |
17 |
Correct |
10 ms |
31324 KB |
Output is correct |
18 |
Correct |
11 ms |
31580 KB |
Output is correct |
19 |
Correct |
11 ms |
31664 KB |
Output is correct |
20 |
Correct |
9 ms |
31324 KB |
Output is correct |
21 |
Correct |
11 ms |
31324 KB |
Output is correct |
22 |
Correct |
9 ms |
31448 KB |
Output is correct |
23 |
Correct |
10 ms |
31324 KB |
Output is correct |
24 |
Correct |
9 ms |
31580 KB |
Output is correct |
25 |
Correct |
7 ms |
30812 KB |
Output is correct |
26 |
Correct |
7 ms |
30812 KB |
Output is correct |
27 |
Correct |
11 ms |
31576 KB |
Output is correct |
28 |
Correct |
11 ms |
31324 KB |
Output is correct |
29 |
Correct |
9 ms |
31196 KB |
Output is correct |
30 |
Correct |
11 ms |
31432 KB |
Output is correct |
31 |
Correct |
10 ms |
31424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
30812 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
7 ms |
30812 KB |
Output is correct |
4 |
Correct |
723 ms |
95040 KB |
Output is correct |
5 |
Correct |
370 ms |
76604 KB |
Output is correct |
6 |
Correct |
220 ms |
71948 KB |
Output is correct |
7 |
Correct |
266 ms |
70136 KB |
Output is correct |
8 |
Correct |
160 ms |
72048 KB |
Output is correct |
9 |
Correct |
7 ms |
30812 KB |
Output is correct |
10 |
Correct |
7 ms |
30812 KB |
Output is correct |
11 |
Correct |
7 ms |
30812 KB |
Output is correct |
12 |
Correct |
7 ms |
30936 KB |
Output is correct |
13 |
Correct |
7 ms |
30812 KB |
Output is correct |
14 |
Correct |
7 ms |
30812 KB |
Output is correct |
15 |
Correct |
7 ms |
30812 KB |
Output is correct |
16 |
Correct |
7 ms |
30812 KB |
Output is correct |
17 |
Correct |
7 ms |
30808 KB |
Output is correct |
18 |
Correct |
8 ms |
30808 KB |
Output is correct |
19 |
Correct |
7 ms |
31068 KB |
Output is correct |
20 |
Correct |
8 ms |
30940 KB |
Output is correct |
21 |
Correct |
8 ms |
31068 KB |
Output is correct |
22 |
Correct |
7 ms |
30896 KB |
Output is correct |
23 |
Correct |
7 ms |
31064 KB |
Output is correct |
24 |
Correct |
11 ms |
31564 KB |
Output is correct |
25 |
Correct |
10 ms |
31324 KB |
Output is correct |
26 |
Correct |
11 ms |
31580 KB |
Output is correct |
27 |
Correct |
11 ms |
31664 KB |
Output is correct |
28 |
Correct |
9 ms |
31324 KB |
Output is correct |
29 |
Correct |
11 ms |
31324 KB |
Output is correct |
30 |
Correct |
9 ms |
31448 KB |
Output is correct |
31 |
Correct |
10 ms |
31324 KB |
Output is correct |
32 |
Correct |
9 ms |
31580 KB |
Output is correct |
33 |
Correct |
7 ms |
30812 KB |
Output is correct |
34 |
Correct |
7 ms |
30812 KB |
Output is correct |
35 |
Correct |
11 ms |
31576 KB |
Output is correct |
36 |
Correct |
11 ms |
31324 KB |
Output is correct |
37 |
Correct |
9 ms |
31196 KB |
Output is correct |
38 |
Correct |
11 ms |
31432 KB |
Output is correct |
39 |
Correct |
10 ms |
31424 KB |
Output is correct |
40 |
Correct |
768 ms |
95648 KB |
Output is correct |
41 |
Correct |
389 ms |
76960 KB |
Output is correct |
42 |
Correct |
492 ms |
100380 KB |
Output is correct |
43 |
Correct |
475 ms |
100124 KB |
Output is correct |
44 |
Correct |
244 ms |
72288 KB |
Output is correct |
45 |
Correct |
316 ms |
77396 KB |
Output is correct |
46 |
Correct |
154 ms |
49760 KB |
Output is correct |
47 |
Correct |
454 ms |
79136 KB |
Output is correct |
48 |
Correct |
397 ms |
87408 KB |
Output is correct |