#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 10, K = 23;
int l[N], r[N], n, q;
int jump[N][K];
pair<int, int> t[4 * N];
void modif(int i, int l, int r, int pos, pair<int, int> x) {
if(l > pos || r < pos) return;
if(l == pos && r == pos) {
t[i] = min(t[i], x);
return;
}
int mid = l + r >> 1;
modif(2 * i, l, mid, pos, x);
modif(2 * i + 1, mid + 1, r, pos, x);
t[i] = min(t[2 * i], t[2 * i + 1]);
}
pair<int, int> query(int i, int l, int r, int tl, int tr) {
if(l > tr || r < tl) return {INT_MAX, -1};
if(l >= tl && r <= tr) return t[i];
int mid = l + r >> 1;
return min(query(2 * i, l, mid, tl, tr), query(2 * i + 1, mid + 1, r, tl, tr));
}
int main() {
for(int i = 0; i < 4 * N; ++i) t[i] = {INT_MAX, -1};
vector<int> v;
cin >> n >> q;
for(int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
v.push_back(l[i]);
v.push_back(r[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i = 0; i < n; ++i) {
l[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin();
r[i] = lower_bound(v.begin(), v.end(), r[i]) - v.begin();
modif(1, 0, N - 1, r[i], make_pair(l[i], i));
}
for(int i = 0; i < n; ++i) {
jump[i][0] = query(1, 0, N - 1, l[i], r[i]).second;
}
for(int j = 1; j < K; ++j) {
for(int i = 0; i < n; ++i) {
jump[i][j] = jump[jump[i][j - 1]][j - 1];
}
}
while(q--) {
int a, b; cin >> a >> b; --a, --b;
if(a == b) {
cout << "0\n";
continue;
}
if(r[a] > r[b]) {
cout << "impossible\n";
continue;
}
if(r[a] >= l[b]) {
cout << "1\n";
continue;
}
int ans = 0;
for(int j = K - 1; j >= 0; --j) {
if(l[jump[b][j]] > r[a]) {
b = jump[b][j];
ans += (1 << j);
}
}
ans += 2;
b = jump[b][0];
if(l[b] > r[a]) {
cout << "impossible\n";
continue;
}
cout << ans << "\n";
}
}
Compilation message
events.cpp: In function 'void modif(int, int, int, int, std::pair<int, int>)':
events.cpp:14:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | int mid = l + r >> 1;
| ~~^~~
events.cpp: In function 'std::pair<int, int> query(int, int, int, int, int)':
events.cpp:22:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
405 ms |
43292 KB |
Output is correct |
3 |
Correct |
360 ms |
43176 KB |
Output is correct |
4 |
Correct |
423 ms |
43156 KB |
Output is correct |
5 |
Correct |
368 ms |
43156 KB |
Output is correct |
6 |
Correct |
374 ms |
43228 KB |
Output is correct |
7 |
Correct |
375 ms |
43180 KB |
Output is correct |
8 |
Correct |
399 ms |
43664 KB |
Output is correct |
9 |
Correct |
382 ms |
43592 KB |
Output is correct |
10 |
Correct |
390 ms |
43456 KB |
Output is correct |
11 |
Correct |
399 ms |
43608 KB |
Output is correct |
12 |
Correct |
308 ms |
43640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31516 KB |
Output is correct |
3 |
Correct |
16 ms |
31668 KB |
Output is correct |
4 |
Correct |
16 ms |
31640 KB |
Output is correct |
5 |
Correct |
16 ms |
31700 KB |
Output is correct |
6 |
Correct |
19 ms |
31652 KB |
Output is correct |
7 |
Correct |
18 ms |
31700 KB |
Output is correct |
8 |
Correct |
19 ms |
31752 KB |
Output is correct |
9 |
Correct |
17 ms |
31752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31516 KB |
Output is correct |
3 |
Correct |
16 ms |
31668 KB |
Output is correct |
4 |
Correct |
16 ms |
31640 KB |
Output is correct |
5 |
Correct |
16 ms |
31700 KB |
Output is correct |
6 |
Correct |
19 ms |
31652 KB |
Output is correct |
7 |
Correct |
18 ms |
31700 KB |
Output is correct |
8 |
Correct |
19 ms |
31752 KB |
Output is correct |
9 |
Correct |
17 ms |
31752 KB |
Output is correct |
10 |
Correct |
15 ms |
31516 KB |
Output is correct |
11 |
Correct |
15 ms |
31596 KB |
Output is correct |
12 |
Correct |
16 ms |
31700 KB |
Output is correct |
13 |
Correct |
17 ms |
31700 KB |
Output is correct |
14 |
Correct |
16 ms |
31672 KB |
Output is correct |
15 |
Correct |
17 ms |
31736 KB |
Output is correct |
16 |
Correct |
17 ms |
31740 KB |
Output is correct |
17 |
Correct |
20 ms |
31700 KB |
Output is correct |
18 |
Correct |
17 ms |
31676 KB |
Output is correct |
19 |
Correct |
234 ms |
32920 KB |
Output is correct |
20 |
Correct |
195 ms |
32904 KB |
Output is correct |
21 |
Correct |
235 ms |
33228 KB |
Output is correct |
22 |
Correct |
196 ms |
33316 KB |
Output is correct |
23 |
Correct |
188 ms |
33096 KB |
Output is correct |
24 |
Correct |
198 ms |
33100 KB |
Output is correct |
25 |
Correct |
227 ms |
32740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
15 ms |
31516 KB |
Output is correct |
3 |
Correct |
16 ms |
31668 KB |
Output is correct |
4 |
Correct |
16 ms |
31640 KB |
Output is correct |
5 |
Correct |
16 ms |
31700 KB |
Output is correct |
6 |
Correct |
19 ms |
31652 KB |
Output is correct |
7 |
Correct |
18 ms |
31700 KB |
Output is correct |
8 |
Correct |
19 ms |
31752 KB |
Output is correct |
9 |
Correct |
17 ms |
31752 KB |
Output is correct |
10 |
Correct |
14 ms |
31572 KB |
Output is correct |
11 |
Correct |
14 ms |
31596 KB |
Output is correct |
12 |
Correct |
16 ms |
31660 KB |
Output is correct |
13 |
Correct |
19 ms |
31692 KB |
Output is correct |
14 |
Correct |
16 ms |
31640 KB |
Output is correct |
15 |
Correct |
32 ms |
31820 KB |
Output is correct |
16 |
Correct |
18 ms |
31656 KB |
Output is correct |
17 |
Correct |
18 ms |
31692 KB |
Output is correct |
18 |
Correct |
18 ms |
31720 KB |
Output is correct |
19 |
Correct |
189 ms |
42560 KB |
Output is correct |
20 |
Correct |
166 ms |
42576 KB |
Output is correct |
21 |
Correct |
204 ms |
42624 KB |
Output is correct |
22 |
Correct |
212 ms |
42468 KB |
Output is correct |
23 |
Correct |
209 ms |
42644 KB |
Output is correct |
24 |
Correct |
258 ms |
42596 KB |
Output is correct |
25 |
Correct |
123 ms |
42500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
43128 KB |
Output is correct |
2 |
Correct |
405 ms |
43096 KB |
Output is correct |
3 |
Correct |
415 ms |
43048 KB |
Output is correct |
4 |
Correct |
429 ms |
43596 KB |
Output is correct |
5 |
Correct |
415 ms |
43628 KB |
Output is correct |
6 |
Correct |
398 ms |
43292 KB |
Output is correct |
7 |
Correct |
429 ms |
43204 KB |
Output is correct |
8 |
Correct |
386 ms |
43420 KB |
Output is correct |
9 |
Correct |
222 ms |
42524 KB |
Output is correct |
10 |
Correct |
393 ms |
43100 KB |
Output is correct |
11 |
Correct |
396 ms |
42764 KB |
Output is correct |
12 |
Correct |
383 ms |
42948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
405 ms |
43292 KB |
Output is correct |
3 |
Correct |
360 ms |
43176 KB |
Output is correct |
4 |
Correct |
423 ms |
43156 KB |
Output is correct |
5 |
Correct |
368 ms |
43156 KB |
Output is correct |
6 |
Correct |
374 ms |
43228 KB |
Output is correct |
7 |
Correct |
375 ms |
43180 KB |
Output is correct |
8 |
Correct |
399 ms |
43664 KB |
Output is correct |
9 |
Correct |
382 ms |
43592 KB |
Output is correct |
10 |
Correct |
390 ms |
43456 KB |
Output is correct |
11 |
Correct |
399 ms |
43608 KB |
Output is correct |
12 |
Correct |
308 ms |
43640 KB |
Output is correct |
13 |
Correct |
15 ms |
31572 KB |
Output is correct |
14 |
Correct |
15 ms |
31516 KB |
Output is correct |
15 |
Correct |
16 ms |
31668 KB |
Output is correct |
16 |
Correct |
16 ms |
31640 KB |
Output is correct |
17 |
Correct |
16 ms |
31700 KB |
Output is correct |
18 |
Correct |
19 ms |
31652 KB |
Output is correct |
19 |
Correct |
18 ms |
31700 KB |
Output is correct |
20 |
Correct |
19 ms |
31752 KB |
Output is correct |
21 |
Correct |
17 ms |
31752 KB |
Output is correct |
22 |
Correct |
15 ms |
31516 KB |
Output is correct |
23 |
Correct |
15 ms |
31596 KB |
Output is correct |
24 |
Correct |
16 ms |
31700 KB |
Output is correct |
25 |
Correct |
17 ms |
31700 KB |
Output is correct |
26 |
Correct |
16 ms |
31672 KB |
Output is correct |
27 |
Correct |
17 ms |
31736 KB |
Output is correct |
28 |
Correct |
17 ms |
31740 KB |
Output is correct |
29 |
Correct |
20 ms |
31700 KB |
Output is correct |
30 |
Correct |
17 ms |
31676 KB |
Output is correct |
31 |
Correct |
234 ms |
32920 KB |
Output is correct |
32 |
Correct |
195 ms |
32904 KB |
Output is correct |
33 |
Correct |
235 ms |
33228 KB |
Output is correct |
34 |
Correct |
196 ms |
33316 KB |
Output is correct |
35 |
Correct |
188 ms |
33096 KB |
Output is correct |
36 |
Correct |
198 ms |
33100 KB |
Output is correct |
37 |
Correct |
227 ms |
32740 KB |
Output is correct |
38 |
Correct |
14 ms |
31572 KB |
Output is correct |
39 |
Correct |
14 ms |
31596 KB |
Output is correct |
40 |
Correct |
16 ms |
31660 KB |
Output is correct |
41 |
Correct |
19 ms |
31692 KB |
Output is correct |
42 |
Correct |
16 ms |
31640 KB |
Output is correct |
43 |
Correct |
32 ms |
31820 KB |
Output is correct |
44 |
Correct |
18 ms |
31656 KB |
Output is correct |
45 |
Correct |
18 ms |
31692 KB |
Output is correct |
46 |
Correct |
18 ms |
31720 KB |
Output is correct |
47 |
Correct |
189 ms |
42560 KB |
Output is correct |
48 |
Correct |
166 ms |
42576 KB |
Output is correct |
49 |
Correct |
204 ms |
42624 KB |
Output is correct |
50 |
Correct |
212 ms |
42468 KB |
Output is correct |
51 |
Correct |
209 ms |
42644 KB |
Output is correct |
52 |
Correct |
258 ms |
42596 KB |
Output is correct |
53 |
Correct |
123 ms |
42500 KB |
Output is correct |
54 |
Correct |
371 ms |
43128 KB |
Output is correct |
55 |
Correct |
405 ms |
43096 KB |
Output is correct |
56 |
Correct |
415 ms |
43048 KB |
Output is correct |
57 |
Correct |
429 ms |
43596 KB |
Output is correct |
58 |
Correct |
415 ms |
43628 KB |
Output is correct |
59 |
Correct |
398 ms |
43292 KB |
Output is correct |
60 |
Correct |
429 ms |
43204 KB |
Output is correct |
61 |
Correct |
386 ms |
43420 KB |
Output is correct |
62 |
Correct |
222 ms |
42524 KB |
Output is correct |
63 |
Correct |
393 ms |
43100 KB |
Output is correct |
64 |
Correct |
396 ms |
42764 KB |
Output is correct |
65 |
Correct |
383 ms |
42948 KB |
Output is correct |
66 |
Correct |
15 ms |
31572 KB |
Output is correct |
67 |
Correct |
391 ms |
43096 KB |
Output is correct |
68 |
Correct |
368 ms |
43152 KB |
Output is correct |
69 |
Correct |
466 ms |
43176 KB |
Output is correct |
70 |
Correct |
418 ms |
43152 KB |
Output is correct |
71 |
Correct |
395 ms |
43332 KB |
Output is correct |
72 |
Correct |
424 ms |
43224 KB |
Output is correct |
73 |
Correct |
385 ms |
43740 KB |
Output is correct |
74 |
Correct |
381 ms |
43668 KB |
Output is correct |
75 |
Correct |
404 ms |
43568 KB |
Output is correct |
76 |
Correct |
411 ms |
43584 KB |
Output is correct |
77 |
Correct |
314 ms |
43652 KB |
Output is correct |
78 |
Correct |
19 ms |
31528 KB |
Output is correct |
79 |
Correct |
17 ms |
31700 KB |
Output is correct |
80 |
Correct |
18 ms |
31668 KB |
Output is correct |
81 |
Correct |
18 ms |
31752 KB |
Output is correct |
82 |
Correct |
19 ms |
31644 KB |
Output is correct |
83 |
Correct |
17 ms |
31656 KB |
Output is correct |
84 |
Correct |
18 ms |
31712 KB |
Output is correct |
85 |
Correct |
16 ms |
31712 KB |
Output is correct |
86 |
Correct |
198 ms |
33036 KB |
Output is correct |
87 |
Correct |
196 ms |
32896 KB |
Output is correct |
88 |
Correct |
202 ms |
33240 KB |
Output is correct |
89 |
Correct |
193 ms |
33168 KB |
Output is correct |
90 |
Correct |
193 ms |
33004 KB |
Output is correct |
91 |
Correct |
189 ms |
33080 KB |
Output is correct |
92 |
Correct |
194 ms |
32640 KB |
Output is correct |
93 |
Correct |
184 ms |
42528 KB |
Output is correct |
94 |
Correct |
201 ms |
42496 KB |
Output is correct |
95 |
Correct |
212 ms |
42540 KB |
Output is correct |
96 |
Correct |
212 ms |
42544 KB |
Output is correct |
97 |
Correct |
211 ms |
42584 KB |
Output is correct |
98 |
Correct |
213 ms |
42480 KB |
Output is correct |
99 |
Correct |
121 ms |
42584 KB |
Output is correct |
100 |
Correct |
440 ms |
43308 KB |
Output is correct |
101 |
Correct |
432 ms |
43344 KB |
Output is correct |
102 |
Correct |
428 ms |
43444 KB |
Output is correct |
103 |
Correct |
391 ms |
42956 KB |
Output is correct |
104 |
Correct |
409 ms |
42744 KB |
Output is correct |
105 |
Correct |
391 ms |
43088 KB |
Output is correct |
106 |
Correct |
362 ms |
43196 KB |
Output is correct |
107 |
Correct |
406 ms |
43460 KB |
Output is correct |
108 |
Correct |
382 ms |
43304 KB |
Output is correct |
109 |
Correct |
399 ms |
43384 KB |
Output is correct |
110 |
Correct |
402 ms |
43196 KB |
Output is correct |
111 |
Correct |
429 ms |
43212 KB |
Output is correct |