// author: erray
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) void(37)
#endif
using namespace std;
struct SegmentExtractor {
vector<vector<int>> segs;
int n;
SegmentExtractor(int _n) : n(_n) {
segs.resize(n << 1);
}
void push(int l, int r, int id) {
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) segs[l++].push_back(id);
if (r & 1) segs[--r].push_back(id);
}
}
template<typename F>
void extract(int p, F op) {
for (p += n; p > 0; p >>= 1) {
for (auto x : segs[p]) {
op(x);
}
segs[p].clear();
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<int> L(N), R(N);
for (int i = 0; i < N; ++i) {
cin >> L[i] >> R[i];
--L[i], --R[i];
}
auto Bfs = [&](vector<array<int, 2>> sources) {
debug(sources);
vector<int> dist(N, -1);
vector<vector<int>> que(N + 1);
SegmentExtractor st(N);
for (auto[v, d] : sources) {
if (d <= N) {
dist[v] = d;
que[d].push_back(v);
}
}
for (int i = 0; i < N; ++i) {
st.push(L[i], R[i], i);
}
for (int d = 0; d < N; ++d) {
for (auto v : que[d]) {
if (dist[v] < d) {
continue;
}
st.extract(v, [&](int u) {
if (dist[u] == -1 || dist[u] > d + 1) {
dist[u] = d + 1;
que[d + 1].push_back(u);
}
});
}
}
return dist;
};
auto dist_L = Bfs(vector<array<int, 2>>{array<int, 2>{0, 0}});
auto dist_R = Bfs(vector<array<int, 2>>{array<int, 2>{N - 1, 0}});
debug(dist_L, dist_R);
vector<array<int, 2>> sources;
for (int i = 0; i < N; ++i) {
if (dist_L[i] != -1 && dist_R[i] != -1) {
sources.push_back({i, dist_L[i] + dist_R[i] - (i != 0 && i != N - 1)});
}
}
auto dist = Bfs(sources);
debug(dist);
int Q;
cin >> Q;
while (Q--) {
int X;
cin >> X;
--X;
cout << (dist[X] == -1 ? -1 : dist[X]) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
447 ms |
46572 KB |
Output is correct |
5 |
Correct |
207 ms |
33664 KB |
Output is correct |
6 |
Correct |
116 ms |
33124 KB |
Output is correct |
7 |
Correct |
97 ms |
37580 KB |
Output is correct |
8 |
Correct |
67 ms |
29728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
544 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
544 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
856 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
924 KB |
Output is correct |
20 |
Correct |
1 ms |
788 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
816 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
544 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
856 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
924 KB |
Output is correct |
20 |
Correct |
1 ms |
788 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
816 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
884 KB |
Output is correct |
29 |
Correct |
2 ms |
792 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
447 ms |
46572 KB |
Output is correct |
5 |
Correct |
207 ms |
33664 KB |
Output is correct |
6 |
Correct |
116 ms |
33124 KB |
Output is correct |
7 |
Correct |
97 ms |
37580 KB |
Output is correct |
8 |
Correct |
67 ms |
29728 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
544 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
856 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
924 KB |
Output is correct |
28 |
Correct |
1 ms |
788 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
816 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
860 KB |
Output is correct |
36 |
Correct |
3 ms |
884 KB |
Output is correct |
37 |
Correct |
2 ms |
792 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
472 ms |
48424 KB |
Output is correct |
41 |
Correct |
229 ms |
38760 KB |
Output is correct |
42 |
Correct |
273 ms |
49652 KB |
Output is correct |
43 |
Correct |
273 ms |
48468 KB |
Output is correct |
44 |
Correct |
146 ms |
36776 KB |
Output is correct |
45 |
Correct |
173 ms |
41492 KB |
Output is correct |
46 |
Correct |
66 ms |
15216 KB |
Output is correct |
47 |
Correct |
196 ms |
43748 KB |
Output is correct |
48 |
Correct |
201 ms |
37516 KB |
Output is correct |