#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
const int LG = 17;
int main() {
int n, q; cin >> n >> q;
vector<vector<int>> nx(LG);
vector<vector<pair<int, int>>> rmq(LG);
for (int i = 0; i < LG; i++)
nx[i].resize(n), rmq[i].resize(n, {1'000'000'001, -1});
vector<pair<int, int>> a(n);
vector<int> b;
for (int i = 0; i < n; i++) {
cin >> a[i].st >> a[i].nd;
b.push_back(a[i].nd);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for (int i = 0; i < n; i++) {
int r = lower_bound(b.begin(), b.end(), a[i].nd) - b.begin();
rmq[0][r] = min(rmq[0][r], {a[i].st, i});
}
for (int i = 0; i + 1 < LG; i++)
for (int j = 0; j + (1 << (i + 1)) <= n; j++)
rmq[i + 1][j] = min(rmq[i][j], rmq[i][j + (1 << i)]);
for (int i = 0; i < n; i++) {
int l = lower_bound(b.begin(), b.end(), a[i].st) - b.begin();
int r = lower_bound(b.begin(), b.end(), a[i].nd) - b.begin();
int h = 31 - __builtin_clz(r - l + 1);
nx[0][i] = min(rmq[h][l], rmq[h][r + 1 - (1 << h)]).nd;
}
for (int i = 0; i + 1 < LG; i++)
for (int j = 0; j < n; j++)
nx[i + 1][j] = nx[i][nx[i][j]];
while (q--) {
int x, y, l = 1; cin >> x >> y; x--, y--;
if (x == y || (a[y].st <= a[x].nd && a[x].nd <= a[y].nd)) {
cout << (x == y ? 0 : 1) << "\n";
continue;
}
for (int i = LG - 1; ~i; i--) {
if (a[x].nd < a[nx[i][y]].st)
l += 1 << i, y = nx[i][y];
}
y = nx[0][y];
if (a[x].nd < a[y].st || a[x].nd > a[y].nd)
cout << "impossible\n";
else
cout << l + (a[x].nd >= a[y].st) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
386 ms |
25344 KB |
Output is correct |
3 |
Correct |
386 ms |
25316 KB |
Output is correct |
4 |
Correct |
507 ms |
25220 KB |
Output is correct |
5 |
Correct |
424 ms |
25364 KB |
Output is correct |
6 |
Correct |
475 ms |
25428 KB |
Output is correct |
7 |
Correct |
410 ms |
25396 KB |
Output is correct |
8 |
Correct |
435 ms |
25712 KB |
Output is correct |
9 |
Correct |
442 ms |
25736 KB |
Output is correct |
10 |
Correct |
484 ms |
25664 KB |
Output is correct |
11 |
Correct |
492 ms |
25716 KB |
Output is correct |
12 |
Correct |
297 ms |
24972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
520 KB |
Output is correct |
13 |
Correct |
2 ms |
532 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
524 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
206 ms |
2748 KB |
Output is correct |
20 |
Correct |
249 ms |
2796 KB |
Output is correct |
21 |
Correct |
233 ms |
3084 KB |
Output is correct |
22 |
Correct |
202 ms |
3108 KB |
Output is correct |
23 |
Correct |
234 ms |
2964 KB |
Output is correct |
24 |
Correct |
200 ms |
3044 KB |
Output is correct |
25 |
Correct |
228 ms |
2672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
528 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
520 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
528 KB |
Output is correct |
18 |
Correct |
3 ms |
520 KB |
Output is correct |
19 |
Correct |
176 ms |
23572 KB |
Output is correct |
20 |
Correct |
121 ms |
22764 KB |
Output is correct |
21 |
Correct |
158 ms |
23468 KB |
Output is correct |
22 |
Correct |
163 ms |
23456 KB |
Output is correct |
23 |
Correct |
180 ms |
23496 KB |
Output is correct |
24 |
Correct |
163 ms |
23604 KB |
Output is correct |
25 |
Correct |
95 ms |
22828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
425 ms |
25328 KB |
Output is correct |
2 |
Correct |
371 ms |
25280 KB |
Output is correct |
3 |
Correct |
503 ms |
25304 KB |
Output is correct |
4 |
Correct |
458 ms |
25768 KB |
Output is correct |
5 |
Correct |
432 ms |
25620 KB |
Output is correct |
6 |
Correct |
540 ms |
25356 KB |
Output is correct |
7 |
Correct |
438 ms |
25376 KB |
Output is correct |
8 |
Correct |
398 ms |
25504 KB |
Output is correct |
9 |
Correct |
132 ms |
23648 KB |
Output is correct |
10 |
Correct |
354 ms |
25072 KB |
Output is correct |
11 |
Correct |
316 ms |
25020 KB |
Output is correct |
12 |
Correct |
373 ms |
25052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
386 ms |
25344 KB |
Output is correct |
3 |
Correct |
386 ms |
25316 KB |
Output is correct |
4 |
Correct |
507 ms |
25220 KB |
Output is correct |
5 |
Correct |
424 ms |
25364 KB |
Output is correct |
6 |
Correct |
475 ms |
25428 KB |
Output is correct |
7 |
Correct |
410 ms |
25396 KB |
Output is correct |
8 |
Correct |
435 ms |
25712 KB |
Output is correct |
9 |
Correct |
442 ms |
25736 KB |
Output is correct |
10 |
Correct |
484 ms |
25664 KB |
Output is correct |
11 |
Correct |
492 ms |
25716 KB |
Output is correct |
12 |
Correct |
297 ms |
24972 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
520 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
520 KB |
Output is correct |
25 |
Correct |
2 ms |
532 KB |
Output is correct |
26 |
Correct |
4 ms |
468 KB |
Output is correct |
27 |
Correct |
3 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
524 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
206 ms |
2748 KB |
Output is correct |
32 |
Correct |
249 ms |
2796 KB |
Output is correct |
33 |
Correct |
233 ms |
3084 KB |
Output is correct |
34 |
Correct |
202 ms |
3108 KB |
Output is correct |
35 |
Correct |
234 ms |
2964 KB |
Output is correct |
36 |
Correct |
200 ms |
3044 KB |
Output is correct |
37 |
Correct |
228 ms |
2672 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
528 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
520 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
528 KB |
Output is correct |
46 |
Correct |
3 ms |
520 KB |
Output is correct |
47 |
Correct |
176 ms |
23572 KB |
Output is correct |
48 |
Correct |
121 ms |
22764 KB |
Output is correct |
49 |
Correct |
158 ms |
23468 KB |
Output is correct |
50 |
Correct |
163 ms |
23456 KB |
Output is correct |
51 |
Correct |
180 ms |
23496 KB |
Output is correct |
52 |
Correct |
163 ms |
23604 KB |
Output is correct |
53 |
Correct |
95 ms |
22828 KB |
Output is correct |
54 |
Correct |
425 ms |
25328 KB |
Output is correct |
55 |
Correct |
371 ms |
25280 KB |
Output is correct |
56 |
Correct |
503 ms |
25304 KB |
Output is correct |
57 |
Correct |
458 ms |
25768 KB |
Output is correct |
58 |
Correct |
432 ms |
25620 KB |
Output is correct |
59 |
Correct |
540 ms |
25356 KB |
Output is correct |
60 |
Correct |
438 ms |
25376 KB |
Output is correct |
61 |
Correct |
398 ms |
25504 KB |
Output is correct |
62 |
Correct |
132 ms |
23648 KB |
Output is correct |
63 |
Correct |
354 ms |
25072 KB |
Output is correct |
64 |
Correct |
316 ms |
25020 KB |
Output is correct |
65 |
Correct |
373 ms |
25052 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
309 ms |
25296 KB |
Output is correct |
68 |
Correct |
347 ms |
25336 KB |
Output is correct |
69 |
Correct |
371 ms |
25276 KB |
Output is correct |
70 |
Correct |
416 ms |
25464 KB |
Output is correct |
71 |
Correct |
329 ms |
25304 KB |
Output is correct |
72 |
Correct |
398 ms |
25628 KB |
Output is correct |
73 |
Correct |
338 ms |
25764 KB |
Output is correct |
74 |
Correct |
403 ms |
25768 KB |
Output is correct |
75 |
Correct |
342 ms |
25676 KB |
Output is correct |
76 |
Correct |
378 ms |
25812 KB |
Output is correct |
77 |
Correct |
258 ms |
25012 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
524 KB |
Output is correct |
83 |
Correct |
2 ms |
528 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
181 ms |
2804 KB |
Output is correct |
87 |
Correct |
192 ms |
2912 KB |
Output is correct |
88 |
Correct |
188 ms |
3088 KB |
Output is correct |
89 |
Correct |
179 ms |
3204 KB |
Output is correct |
90 |
Correct |
193 ms |
3028 KB |
Output is correct |
91 |
Correct |
172 ms |
2928 KB |
Output is correct |
92 |
Correct |
177 ms |
2772 KB |
Output is correct |
93 |
Correct |
135 ms |
23572 KB |
Output is correct |
94 |
Correct |
104 ms |
22832 KB |
Output is correct |
95 |
Correct |
124 ms |
23468 KB |
Output is correct |
96 |
Correct |
130 ms |
23560 KB |
Output is correct |
97 |
Correct |
142 ms |
23432 KB |
Output is correct |
98 |
Correct |
143 ms |
23560 KB |
Output is correct |
99 |
Correct |
76 ms |
22852 KB |
Output is correct |
100 |
Correct |
350 ms |
25432 KB |
Output is correct |
101 |
Correct |
374 ms |
25444 KB |
Output is correct |
102 |
Correct |
358 ms |
25528 KB |
Output is correct |
103 |
Correct |
351 ms |
25096 KB |
Output is correct |
104 |
Correct |
320 ms |
24864 KB |
Output is correct |
105 |
Correct |
339 ms |
25112 KB |
Output is correct |
106 |
Correct |
325 ms |
24748 KB |
Output is correct |
107 |
Correct |
360 ms |
24760 KB |
Output is correct |
108 |
Correct |
348 ms |
25380 KB |
Output is correct |
109 |
Correct |
336 ms |
25412 KB |
Output is correct |
110 |
Correct |
361 ms |
25452 KB |
Output is correct |
111 |
Correct |
333 ms |
25280 KB |
Output is correct |