/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
const int INF = INT_MAX / 2;
int N;
int L[N_MAX + 2], R[N_MAX + 2];
int Q;
vector <pair <int, int>> adj[N_MAX * 4 + 2];
int encode[N_MAX * 4 + 2];
void build (int node, int l, int r) {
if (l == r) {
encode[node] = l;
return;
}
encode[node] = node + N;
int mid = (l + r) / 2;
int left = node * 2, right = node * 2 + 1;
build(left, l, mid);
build(right, mid + 1, r);
adj[encode[left]].push_back({encode[node], 0});
adj[encode[right]].push_back({encode[node], 0});
}
void build () {
build(1, 1, N);
}
void connect_range (int node, int l, int r, int i) {
if (L[i] <= l && r <= R[i]) {
adj[encode[node]].push_back({i, 1});
return;
}
int mid = (l + r) / 2;
int left = node * 2, right = node * 2 + 1;
if (L[i] <= mid) {
connect_range(left, l, mid, i);
}
if (mid + 1 <= R[i]) {
connect_range(right, mid + 1, r, i);
}
}
void connect_range (int i) {
connect_range(1, 1, N, i);
}
int dp_left[N_MAX * 4 + 2];
int dp_right[N_MAX * 4 + 2];
int dp_full[N_MAX * 4 + 2];
vector <int> at_dist[N_MAX + 2];
bool seen[N_MAX * 4 + 2];
void Dijkstra (int dist[]) {
for (int i = 1; i <= N; i++) {
if (dist[i] < N) {
at_dist[dist[i]].push_back(i);
}
}
fill(dist + N + 1, dist + N * 4 + 1, INF);
fill(seen + 1, seen + N * 4 + 1, false);
for (int d = 0; d < N; d++) {
while (at_dist[d].empty() == false) {
int u = at_dist[d].back();
at_dist[d].pop_back();
if (seen[u] == true) {
continue;
}
seen[u] = true;
for (pair <int, int> e : adj[u]) {
int v, c; tie(v, c) = e;
if (dist[u] + c < dist[v]) {
dist[v] = dist[u] + c;
if (dist[v] < N) {
at_dist[dist[v]].push_back(v);
}
}
}
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
build();
for (int i = 1; i <= N; i++) {
cin >> L[i] >> R[i];
connect_range(i);
dp_left[i] = (L[i] == 1 ? 0 : INF);
dp_right[i] = (R[i] == N ? 0 : INF);
}
Dijkstra(dp_left);
Dijkstra(dp_right);
for (int i = 1; i <= N; i++) {
dp_full[i] = min(INF, dp_left[i] + dp_right[i]);
}
Dijkstra(dp_full);
cin >> Q;
while (Q--) {
int i;
cin >> i;
cout << (dp_full[i] != INF ? dp_full[i] + 1 : -1) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
33112 KB |
Output is correct |
2 |
Correct |
7 ms |
33112 KB |
Output is correct |
3 |
Correct |
8 ms |
33292 KB |
Output is correct |
4 |
Correct |
456 ms |
84144 KB |
Output is correct |
5 |
Correct |
259 ms |
64956 KB |
Output is correct |
6 |
Correct |
125 ms |
61968 KB |
Output is correct |
7 |
Correct |
72 ms |
56004 KB |
Output is correct |
8 |
Correct |
90 ms |
62188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33112 KB |
Output is correct |
2 |
Correct |
7 ms |
33116 KB |
Output is correct |
3 |
Correct |
8 ms |
33116 KB |
Output is correct |
4 |
Correct |
7 ms |
33164 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
7 ms |
33116 KB |
Output is correct |
7 |
Correct |
6 ms |
33112 KB |
Output is correct |
8 |
Correct |
7 ms |
33116 KB |
Output is correct |
9 |
Correct |
7 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33216 KB |
Output is correct |
11 |
Correct |
8 ms |
33368 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33116 KB |
Output is correct |
14 |
Correct |
7 ms |
33116 KB |
Output is correct |
15 |
Correct |
7 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33112 KB |
Output is correct |
2 |
Correct |
7 ms |
33116 KB |
Output is correct |
3 |
Correct |
8 ms |
33116 KB |
Output is correct |
4 |
Correct |
7 ms |
33164 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
7 ms |
33116 KB |
Output is correct |
7 |
Correct |
6 ms |
33112 KB |
Output is correct |
8 |
Correct |
7 ms |
33116 KB |
Output is correct |
9 |
Correct |
7 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33216 KB |
Output is correct |
11 |
Correct |
8 ms |
33368 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33116 KB |
Output is correct |
14 |
Correct |
7 ms |
33116 KB |
Output is correct |
15 |
Correct |
7 ms |
33116 KB |
Output is correct |
16 |
Correct |
10 ms |
33628 KB |
Output is correct |
17 |
Correct |
8 ms |
33624 KB |
Output is correct |
18 |
Correct |
10 ms |
33816 KB |
Output is correct |
19 |
Correct |
9 ms |
33884 KB |
Output is correct |
20 |
Correct |
11 ms |
33628 KB |
Output is correct |
21 |
Correct |
8 ms |
33628 KB |
Output is correct |
22 |
Correct |
8 ms |
33496 KB |
Output is correct |
23 |
Correct |
8 ms |
33668 KB |
Output is correct |
24 |
Correct |
8 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33112 KB |
Output is correct |
2 |
Correct |
7 ms |
33116 KB |
Output is correct |
3 |
Correct |
8 ms |
33116 KB |
Output is correct |
4 |
Correct |
7 ms |
33164 KB |
Output is correct |
5 |
Correct |
7 ms |
33116 KB |
Output is correct |
6 |
Correct |
7 ms |
33116 KB |
Output is correct |
7 |
Correct |
6 ms |
33112 KB |
Output is correct |
8 |
Correct |
7 ms |
33116 KB |
Output is correct |
9 |
Correct |
7 ms |
33116 KB |
Output is correct |
10 |
Correct |
7 ms |
33216 KB |
Output is correct |
11 |
Correct |
8 ms |
33368 KB |
Output is correct |
12 |
Correct |
7 ms |
33116 KB |
Output is correct |
13 |
Correct |
7 ms |
33116 KB |
Output is correct |
14 |
Correct |
7 ms |
33116 KB |
Output is correct |
15 |
Correct |
7 ms |
33116 KB |
Output is correct |
16 |
Correct |
10 ms |
33628 KB |
Output is correct |
17 |
Correct |
8 ms |
33624 KB |
Output is correct |
18 |
Correct |
10 ms |
33816 KB |
Output is correct |
19 |
Correct |
9 ms |
33884 KB |
Output is correct |
20 |
Correct |
11 ms |
33628 KB |
Output is correct |
21 |
Correct |
8 ms |
33628 KB |
Output is correct |
22 |
Correct |
8 ms |
33496 KB |
Output is correct |
23 |
Correct |
8 ms |
33668 KB |
Output is correct |
24 |
Correct |
8 ms |
33628 KB |
Output is correct |
25 |
Correct |
8 ms |
33112 KB |
Output is correct |
26 |
Correct |
8 ms |
33116 KB |
Output is correct |
27 |
Correct |
9 ms |
33624 KB |
Output is correct |
28 |
Correct |
8 ms |
33624 KB |
Output is correct |
29 |
Correct |
10 ms |
33628 KB |
Output is correct |
30 |
Correct |
9 ms |
33624 KB |
Output is correct |
31 |
Correct |
8 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
33112 KB |
Output is correct |
2 |
Correct |
7 ms |
33112 KB |
Output is correct |
3 |
Correct |
8 ms |
33292 KB |
Output is correct |
4 |
Correct |
456 ms |
84144 KB |
Output is correct |
5 |
Correct |
259 ms |
64956 KB |
Output is correct |
6 |
Correct |
125 ms |
61968 KB |
Output is correct |
7 |
Correct |
72 ms |
56004 KB |
Output is correct |
8 |
Correct |
90 ms |
62188 KB |
Output is correct |
9 |
Correct |
7 ms |
33112 KB |
Output is correct |
10 |
Correct |
7 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
7 ms |
33164 KB |
Output is correct |
13 |
Correct |
7 ms |
33116 KB |
Output is correct |
14 |
Correct |
7 ms |
33116 KB |
Output is correct |
15 |
Correct |
6 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33116 KB |
Output is correct |
17 |
Correct |
7 ms |
33116 KB |
Output is correct |
18 |
Correct |
7 ms |
33216 KB |
Output is correct |
19 |
Correct |
8 ms |
33368 KB |
Output is correct |
20 |
Correct |
7 ms |
33116 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
10 ms |
33628 KB |
Output is correct |
25 |
Correct |
8 ms |
33624 KB |
Output is correct |
26 |
Correct |
10 ms |
33816 KB |
Output is correct |
27 |
Correct |
9 ms |
33884 KB |
Output is correct |
28 |
Correct |
11 ms |
33628 KB |
Output is correct |
29 |
Correct |
8 ms |
33628 KB |
Output is correct |
30 |
Correct |
8 ms |
33496 KB |
Output is correct |
31 |
Correct |
8 ms |
33668 KB |
Output is correct |
32 |
Correct |
8 ms |
33628 KB |
Output is correct |
33 |
Correct |
8 ms |
33112 KB |
Output is correct |
34 |
Correct |
8 ms |
33116 KB |
Output is correct |
35 |
Correct |
9 ms |
33624 KB |
Output is correct |
36 |
Correct |
8 ms |
33624 KB |
Output is correct |
37 |
Correct |
10 ms |
33628 KB |
Output is correct |
38 |
Correct |
9 ms |
33624 KB |
Output is correct |
39 |
Correct |
8 ms |
33628 KB |
Output is correct |
40 |
Correct |
511 ms |
86836 KB |
Output is correct |
41 |
Correct |
214 ms |
67408 KB |
Output is correct |
42 |
Correct |
284 ms |
94500 KB |
Output is correct |
43 |
Correct |
265 ms |
93860 KB |
Output is correct |
44 |
Correct |
141 ms |
64708 KB |
Output is correct |
45 |
Correct |
197 ms |
70456 KB |
Output is correct |
46 |
Correct |
71 ms |
47444 KB |
Output is correct |
47 |
Correct |
225 ms |
68304 KB |
Output is correct |
48 |
Correct |
219 ms |
75820 KB |
Output is correct |