#include <bits/stdc++.h>
using namespace std;
using idata = vector<int>;
using igrid = vector<idata>;
using bdata = vector<bool>;
using di = pair<int, int>;
using vd = vector<di>;
struct VSegTree {
igrid tree;
bdata visited;
int size, height, start;
VSegTree (int _size) {
size = _size;
height = ceil(log2(size));
start = 1 << height;
tree .resize(start << 1);
visited.resize(size);
}
void insert (int node, int l, int r) {
l += start; r += start;
while (l < r) {
if ((l & 1) == 1) tree[l].push_back(node);
if ((r & 1) == 0) tree[r].push_back(node);
l = (l + 1) >> 1;
r = (r - 1) >> 1;
}
if (l == r) tree[l].push_back(node);
}
idata query (int node) {
idata answer;
node += start;
while (node != 0) {
for (int u : tree[node]) if (!visited[u]) {
visited[u] = true;
answer.push_back(u);
}
tree[node].clear();
node >>= 1;
}
return answer;
}
};
int N;
idata bfs (vd &start, VSegTree tree) {
igrid todo_list(2 * N + 3);
for (const auto &st : start) {
//cout << st.first << " = dist(" << st.second << ")\n";
todo_list[st.first].push_back(st.second);
}
idata answer (N, -1);
for (int i = 0; i < todo_list.size(); i ++) {
for (int curr : todo_list[i]) {
if (answer[curr] != -1) continue ;
answer[curr] = i;
for (int next : tree.query(curr))
todo_list[i + 1].push_back(next);
}
}
return answer;
}
int main () {
cin >> N;
VSegTree tree(N);
vd left_start;
vd right_start;
for (int i = 0; i < N; i ++) {
int l, r;
cin >> l >> r;
l --; r --;
tree.insert(i, l, r);
//cout << l << " " << r << ": " << (l == 0) << " " << (r == N - 1) << endl;
if (l == 0 ) left_start.push_back({ 0, i });
if (r == N - 1) right_start.push_back({ 0, i });
}
idata dp_left = bfs( left_start, tree);
idata dp_right = bfs(right_start, tree);
//for (int u : dp_left) cout << u << " "; cout << endl;
//for (int u : dp_right) cout << u << " "; cout << endl;
vd final_start;
for (int i = 0; i < N; i ++) {
if (dp_left[i] == -1 || dp_right[i] == -1) continue;
final_start.push_back({ dp_left[i] + dp_right[i] + 1, i });
}
idata dp_answer = bfs(final_start, tree);
int Q; cin >> Q;
for (int q = 0; q < Q; q ++) {
int x; cin >> x;
x --;
cout << dp_answer[x] << "\n";
}
}
Compilation message
passport.cpp: In function 'idata bfs(vd&, VSegTree)':
passport.cpp:68:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0; i < todo_list.size(); i ++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
410 ms |
83612 KB |
Output is correct |
5 |
Correct |
291 ms |
68800 KB |
Output is correct |
6 |
Correct |
212 ms |
66520 KB |
Output is correct |
7 |
Correct |
151 ms |
66396 KB |
Output is correct |
8 |
Correct |
108 ms |
55200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
3 ms |
1168 KB |
Output is correct |
17 |
Correct |
3 ms |
1168 KB |
Output is correct |
18 |
Correct |
3 ms |
1168 KB |
Output is correct |
19 |
Correct |
3 ms |
1168 KB |
Output is correct |
20 |
Correct |
2 ms |
1168 KB |
Output is correct |
21 |
Correct |
3 ms |
1168 KB |
Output is correct |
22 |
Correct |
2 ms |
1040 KB |
Output is correct |
23 |
Correct |
3 ms |
1168 KB |
Output is correct |
24 |
Correct |
2 ms |
1168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
3 ms |
1168 KB |
Output is correct |
17 |
Correct |
3 ms |
1168 KB |
Output is correct |
18 |
Correct |
3 ms |
1168 KB |
Output is correct |
19 |
Correct |
3 ms |
1168 KB |
Output is correct |
20 |
Correct |
2 ms |
1168 KB |
Output is correct |
21 |
Correct |
3 ms |
1168 KB |
Output is correct |
22 |
Correct |
2 ms |
1040 KB |
Output is correct |
23 |
Correct |
3 ms |
1168 KB |
Output is correct |
24 |
Correct |
2 ms |
1168 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
6 ms |
1168 KB |
Output is correct |
28 |
Correct |
6 ms |
1168 KB |
Output is correct |
29 |
Correct |
6 ms |
1196 KB |
Output is correct |
30 |
Correct |
7 ms |
1336 KB |
Output is correct |
31 |
Correct |
6 ms |
1168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
410 ms |
83612 KB |
Output is correct |
5 |
Correct |
291 ms |
68800 KB |
Output is correct |
6 |
Correct |
212 ms |
66520 KB |
Output is correct |
7 |
Correct |
151 ms |
66396 KB |
Output is correct |
8 |
Correct |
108 ms |
55200 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 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 |
3 ms |
1168 KB |
Output is correct |
25 |
Correct |
3 ms |
1168 KB |
Output is correct |
26 |
Correct |
3 ms |
1168 KB |
Output is correct |
27 |
Correct |
3 ms |
1168 KB |
Output is correct |
28 |
Correct |
2 ms |
1168 KB |
Output is correct |
29 |
Correct |
3 ms |
1168 KB |
Output is correct |
30 |
Correct |
2 ms |
1040 KB |
Output is correct |
31 |
Correct |
3 ms |
1168 KB |
Output is correct |
32 |
Correct |
2 ms |
1168 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
6 ms |
1168 KB |
Output is correct |
36 |
Correct |
6 ms |
1168 KB |
Output is correct |
37 |
Correct |
6 ms |
1196 KB |
Output is correct |
38 |
Correct |
7 ms |
1336 KB |
Output is correct |
39 |
Correct |
6 ms |
1168 KB |
Output is correct |
40 |
Correct |
691 ms |
85548 KB |
Output is correct |
41 |
Correct |
550 ms |
71740 KB |
Output is correct |
42 |
Correct |
568 ms |
87308 KB |
Output is correct |
43 |
Correct |
588 ms |
86276 KB |
Output is correct |
44 |
Correct |
461 ms |
69212 KB |
Output is correct |
45 |
Correct |
552 ms |
73300 KB |
Output is correct |
46 |
Correct |
187 ms |
30496 KB |
Output is correct |
47 |
Correct |
515 ms |
77276 KB |
Output is correct |
48 |
Correct |
474 ms |
72416 KB |
Output is correct |