#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, M = 5 * N, inf = 1e9;
int n, q, dist[3][M];
vector<pair<int, int> > adj[M];
bool vis[M];
void link(int u, int v, int w = 0) {
adj[v].emplace_back(u, w);
}
void build(int l, int r, int i) {
if (l == r) return void(link(n + i, l));
int m = (l + r) / 2;
build(l, m, i * 2);
build(m + 1, r, i * 2 + 1);
link(n + i, n + i * 2);
link(n + i, n + i * 2 + 1);
}
void connect(int l, int r, int i, int x, int y, int u) {
if (y < l || r < x) return;
if (x <= l && r <= y) return void(link(u, n + i, 1));
int m = (l + r) / 2;
connect(l, m, i * 2, x, y, u);
connect(m + 1, r, i * 2 + 1, x, y, u);
}
void dijk(int *dist) {
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
for (int i = 1; i <= 5 * n; i++) {
pq.emplace(dist[i], i);
vis[i] = false;
}
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (vis[u]) continue;
vis[u] = true;
for (auto [v, w] : adj[u]) {
if (d + w < dist[v]) {
pq.emplace(d + w, v);
dist[v] = d + w;
}
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
build(1, n, 1);
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
connect(1, n, 1, l, r, i);
}
for (int i = 0; i < 3; i++) for (int j = 1; j <= 5 * n; j++) dist[i][j] = inf;
dist[0][1] = dist[1][n] = 0;
dijk(dist[0]);
dijk(dist[1]);
for (int i = 1; i <= n; i++) dist[2][i] = min(inf, dist[0][i] + dist[1][i] - (1 < i && i < n));
dijk(dist[2]);
cin >> q;
while (q--) {
int x;
cin >> x;
cout << (dist[2][x] < inf ? dist[2][x] : -1) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
29020 KB |
Output is correct |
2 |
Correct |
6 ms |
29016 KB |
Output is correct |
3 |
Correct |
6 ms |
29136 KB |
Output is correct |
4 |
Correct |
1104 ms |
112112 KB |
Output is correct |
5 |
Correct |
689 ms |
83140 KB |
Output is correct |
6 |
Correct |
631 ms |
77080 KB |
Output is correct |
7 |
Correct |
598 ms |
83712 KB |
Output is correct |
8 |
Correct |
411 ms |
78668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29132 KB |
Output is correct |
2 |
Correct |
6 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29016 KB |
Output is correct |
4 |
Correct |
6 ms |
29016 KB |
Output is correct |
5 |
Correct |
7 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29020 KB |
Output is correct |
7 |
Correct |
7 ms |
29016 KB |
Output is correct |
8 |
Correct |
7 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29188 KB |
Output is correct |
10 |
Correct |
6 ms |
29020 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
7 ms |
29120 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29132 KB |
Output is correct |
2 |
Correct |
6 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29016 KB |
Output is correct |
4 |
Correct |
6 ms |
29016 KB |
Output is correct |
5 |
Correct |
7 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29020 KB |
Output is correct |
7 |
Correct |
7 ms |
29016 KB |
Output is correct |
8 |
Correct |
7 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29188 KB |
Output is correct |
10 |
Correct |
6 ms |
29020 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
7 ms |
29120 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29020 KB |
Output is correct |
16 |
Correct |
13 ms |
29836 KB |
Output is correct |
17 |
Correct |
12 ms |
29788 KB |
Output is correct |
18 |
Correct |
15 ms |
30048 KB |
Output is correct |
19 |
Correct |
12 ms |
30060 KB |
Output is correct |
20 |
Correct |
14 ms |
29688 KB |
Output is correct |
21 |
Correct |
11 ms |
29800 KB |
Output is correct |
22 |
Correct |
10 ms |
29708 KB |
Output is correct |
23 |
Correct |
12 ms |
29832 KB |
Output is correct |
24 |
Correct |
11 ms |
29836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29132 KB |
Output is correct |
2 |
Correct |
6 ms |
29020 KB |
Output is correct |
3 |
Correct |
6 ms |
29016 KB |
Output is correct |
4 |
Correct |
6 ms |
29016 KB |
Output is correct |
5 |
Correct |
7 ms |
29020 KB |
Output is correct |
6 |
Correct |
6 ms |
29020 KB |
Output is correct |
7 |
Correct |
7 ms |
29016 KB |
Output is correct |
8 |
Correct |
7 ms |
29020 KB |
Output is correct |
9 |
Correct |
6 ms |
29188 KB |
Output is correct |
10 |
Correct |
6 ms |
29020 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
7 ms |
29120 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29276 KB |
Output is correct |
15 |
Correct |
6 ms |
29020 KB |
Output is correct |
16 |
Correct |
13 ms |
29836 KB |
Output is correct |
17 |
Correct |
12 ms |
29788 KB |
Output is correct |
18 |
Correct |
15 ms |
30048 KB |
Output is correct |
19 |
Correct |
12 ms |
30060 KB |
Output is correct |
20 |
Correct |
14 ms |
29688 KB |
Output is correct |
21 |
Correct |
11 ms |
29800 KB |
Output is correct |
22 |
Correct |
10 ms |
29708 KB |
Output is correct |
23 |
Correct |
12 ms |
29832 KB |
Output is correct |
24 |
Correct |
11 ms |
29836 KB |
Output is correct |
25 |
Correct |
6 ms |
29032 KB |
Output is correct |
26 |
Correct |
7 ms |
29032 KB |
Output is correct |
27 |
Correct |
13 ms |
29840 KB |
Output is correct |
28 |
Correct |
13 ms |
29800 KB |
Output is correct |
29 |
Correct |
14 ms |
29692 KB |
Output is correct |
30 |
Correct |
12 ms |
29776 KB |
Output is correct |
31 |
Correct |
12 ms |
29788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
29020 KB |
Output is correct |
2 |
Correct |
6 ms |
29016 KB |
Output is correct |
3 |
Correct |
6 ms |
29136 KB |
Output is correct |
4 |
Correct |
1104 ms |
112112 KB |
Output is correct |
5 |
Correct |
689 ms |
83140 KB |
Output is correct |
6 |
Correct |
631 ms |
77080 KB |
Output is correct |
7 |
Correct |
598 ms |
83712 KB |
Output is correct |
8 |
Correct |
411 ms |
78668 KB |
Output is correct |
9 |
Correct |
6 ms |
29132 KB |
Output is correct |
10 |
Correct |
6 ms |
29020 KB |
Output is correct |
11 |
Correct |
6 ms |
29016 KB |
Output is correct |
12 |
Correct |
6 ms |
29016 KB |
Output is correct |
13 |
Correct |
7 ms |
29020 KB |
Output is correct |
14 |
Correct |
6 ms |
29020 KB |
Output is correct |
15 |
Correct |
7 ms |
29016 KB |
Output is correct |
16 |
Correct |
7 ms |
29020 KB |
Output is correct |
17 |
Correct |
6 ms |
29188 KB |
Output is correct |
18 |
Correct |
6 ms |
29020 KB |
Output is correct |
19 |
Correct |
7 ms |
29276 KB |
Output is correct |
20 |
Correct |
7 ms |
29120 KB |
Output is correct |
21 |
Correct |
8 ms |
29276 KB |
Output is correct |
22 |
Correct |
7 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29020 KB |
Output is correct |
24 |
Correct |
13 ms |
29836 KB |
Output is correct |
25 |
Correct |
12 ms |
29788 KB |
Output is correct |
26 |
Correct |
15 ms |
30048 KB |
Output is correct |
27 |
Correct |
12 ms |
30060 KB |
Output is correct |
28 |
Correct |
14 ms |
29688 KB |
Output is correct |
29 |
Correct |
11 ms |
29800 KB |
Output is correct |
30 |
Correct |
10 ms |
29708 KB |
Output is correct |
31 |
Correct |
12 ms |
29832 KB |
Output is correct |
32 |
Correct |
11 ms |
29836 KB |
Output is correct |
33 |
Correct |
6 ms |
29032 KB |
Output is correct |
34 |
Correct |
7 ms |
29032 KB |
Output is correct |
35 |
Correct |
13 ms |
29840 KB |
Output is correct |
36 |
Correct |
13 ms |
29800 KB |
Output is correct |
37 |
Correct |
14 ms |
29692 KB |
Output is correct |
38 |
Correct |
12 ms |
29776 KB |
Output is correct |
39 |
Correct |
12 ms |
29788 KB |
Output is correct |
40 |
Correct |
1109 ms |
115876 KB |
Output is correct |
41 |
Correct |
737 ms |
83960 KB |
Output is correct |
42 |
Correct |
783 ms |
109680 KB |
Output is correct |
43 |
Correct |
800 ms |
122632 KB |
Output is correct |
44 |
Correct |
651 ms |
77248 KB |
Output is correct |
45 |
Correct |
656 ms |
84908 KB |
Output is correct |
46 |
Correct |
332 ms |
53888 KB |
Output is correct |
47 |
Correct |
818 ms |
95100 KB |
Output is correct |
48 |
Correct |
715 ms |
103216 KB |
Output is correct |