#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&] (int i, int j) {
if (r[i] != r[j]) return r[i] < r[j];
return l[i] < l[j];
});
vector<int> gdesibreti(n);
for (int i = 0; i < n; i++) gdesibreti[order[i]] = i;
vector<int> tl(n), tr(n);
for (int i = 0; i < n; i++) {
tl[i] = l[order[i]];
tr[i] = r[order[i]];
}
swap(l, tl); swap(r, tr);
vector<vector<pair<int, int>>> rmq(n, vector<pair<int, int>>(17));
for (int i = 0; i < n; i++) rmq[i][0] = {l[i], i};
for (int j = 1; j < 17; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
vector<int> logs(n + 1);
int z = 0;
for (int i = 1; i <= n; i++) {
if (i == (1 << (z + 1))) z += 1;
logs[i] = z;
}
auto Query = [&] (int l, int r) {
int o = logs[r - l + 1];
return min(rmq[l][o], rmq[r - (1 << o) + 1][o]);
};
vector<int> dokle(n, -1);
for (int i = 0; i < n; i++) {
int lo = 0, hi = i - 1, idx = i;
while (lo <= hi) {
int mid = lo + hi >> 1;
if (l[i] <= r[mid]) {
hi = mid - 1;
idx = mid;
}
else lo = mid + 1;
}
dokle[i] = idx;
}
vector<vector<int>> lift(n, vector<int>(17));
for (int i = 0; i < n; i++) lift[i][0] = dokle[i];
for (int j = 1; j < 17; j++) {
for (int i = 0; i < n; i++) {
lift[i][j] = lift[Query(lift[i][j - 1], i).second][j - 1];
}
}
while (q--) {
int a, b;
cin >> a >> b;
a -= 1; b -= 1;
a = gdesibreti[a]; b = gdesibreti[b];
if (a == b) {
cout << 0 << en;
continue;
}
if (l[b] <= r[a] && r[a] <= r[b]) {
cout << 1 << en;
continue;
}
if (a > b) {
cout << "impossible" << en;
continue;
}
int ans = 0;
for (int i = 16; i >= 0; i--) {
if (a < lift[b][i]) {
ans += (1 << i);
b = Query(lift[b][i], b).second;
}
}
if (a < lift[b][0]) cout << "impossible" << en;
else cout << ans + 1 << en;
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:56:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = lo + hi >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
142 ms |
30628 KB |
Output is correct |
3 |
Correct |
125 ms |
30584 KB |
Output is correct |
4 |
Correct |
180 ms |
30536 KB |
Output is correct |
5 |
Correct |
161 ms |
30692 KB |
Output is correct |
6 |
Correct |
159 ms |
30652 KB |
Output is correct |
7 |
Correct |
173 ms |
30708 KB |
Output is correct |
8 |
Correct |
154 ms |
31052 KB |
Output is correct |
9 |
Correct |
135 ms |
31060 KB |
Output is correct |
10 |
Correct |
197 ms |
31052 KB |
Output is correct |
11 |
Correct |
208 ms |
31056 KB |
Output is correct |
12 |
Correct |
144 ms |
31180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
34 ms |
3276 KB |
Output is correct |
20 |
Correct |
35 ms |
3268 KB |
Output is correct |
21 |
Correct |
44 ms |
3504 KB |
Output is correct |
22 |
Correct |
31 ms |
3608 KB |
Output is correct |
23 |
Correct |
28 ms |
3376 KB |
Output is correct |
24 |
Correct |
27 ms |
3456 KB |
Output is correct |
25 |
Correct |
28 ms |
3020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
584 KB |
Output is correct |
18 |
Correct |
1 ms |
584 KB |
Output is correct |
19 |
Correct |
101 ms |
32028 KB |
Output is correct |
20 |
Correct |
89 ms |
31160 KB |
Output is correct |
21 |
Correct |
116 ms |
31928 KB |
Output is correct |
22 |
Correct |
116 ms |
32068 KB |
Output is correct |
23 |
Correct |
90 ms |
32008 KB |
Output is correct |
24 |
Correct |
98 ms |
31968 KB |
Output is correct |
25 |
Correct |
79 ms |
31308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
30616 KB |
Output is correct |
2 |
Correct |
127 ms |
30640 KB |
Output is correct |
3 |
Correct |
149 ms |
30544 KB |
Output is correct |
4 |
Correct |
135 ms |
31068 KB |
Output is correct |
5 |
Correct |
204 ms |
31012 KB |
Output is correct |
6 |
Correct |
207 ms |
30796 KB |
Output is correct |
7 |
Correct |
202 ms |
30940 KB |
Output is correct |
8 |
Correct |
167 ms |
30976 KB |
Output is correct |
9 |
Correct |
98 ms |
30016 KB |
Output is correct |
10 |
Correct |
154 ms |
30420 KB |
Output is correct |
11 |
Correct |
114 ms |
30248 KB |
Output is correct |
12 |
Correct |
128 ms |
30396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
142 ms |
30628 KB |
Output is correct |
3 |
Correct |
125 ms |
30584 KB |
Output is correct |
4 |
Correct |
180 ms |
30536 KB |
Output is correct |
5 |
Correct |
161 ms |
30692 KB |
Output is correct |
6 |
Correct |
159 ms |
30652 KB |
Output is correct |
7 |
Correct |
173 ms |
30708 KB |
Output is correct |
8 |
Correct |
154 ms |
31052 KB |
Output is correct |
9 |
Correct |
135 ms |
31060 KB |
Output is correct |
10 |
Correct |
197 ms |
31052 KB |
Output is correct |
11 |
Correct |
208 ms |
31056 KB |
Output is correct |
12 |
Correct |
144 ms |
31180 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
34 ms |
3276 KB |
Output is correct |
32 |
Correct |
35 ms |
3268 KB |
Output is correct |
33 |
Correct |
44 ms |
3504 KB |
Output is correct |
34 |
Correct |
31 ms |
3608 KB |
Output is correct |
35 |
Correct |
28 ms |
3376 KB |
Output is correct |
36 |
Correct |
27 ms |
3456 KB |
Output is correct |
37 |
Correct |
28 ms |
3020 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
320 KB |
Output is correct |
40 |
Correct |
1 ms |
592 KB |
Output is correct |
41 |
Correct |
2 ms |
592 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
584 KB |
Output is correct |
46 |
Correct |
1 ms |
584 KB |
Output is correct |
47 |
Correct |
101 ms |
32028 KB |
Output is correct |
48 |
Correct |
89 ms |
31160 KB |
Output is correct |
49 |
Correct |
116 ms |
31928 KB |
Output is correct |
50 |
Correct |
116 ms |
32068 KB |
Output is correct |
51 |
Correct |
90 ms |
32008 KB |
Output is correct |
52 |
Correct |
98 ms |
31968 KB |
Output is correct |
53 |
Correct |
79 ms |
31308 KB |
Output is correct |
54 |
Correct |
144 ms |
30616 KB |
Output is correct |
55 |
Correct |
127 ms |
30640 KB |
Output is correct |
56 |
Correct |
149 ms |
30544 KB |
Output is correct |
57 |
Correct |
135 ms |
31068 KB |
Output is correct |
58 |
Correct |
204 ms |
31012 KB |
Output is correct |
59 |
Correct |
207 ms |
30796 KB |
Output is correct |
60 |
Correct |
202 ms |
30940 KB |
Output is correct |
61 |
Correct |
167 ms |
30976 KB |
Output is correct |
62 |
Correct |
98 ms |
30016 KB |
Output is correct |
63 |
Correct |
154 ms |
30420 KB |
Output is correct |
64 |
Correct |
114 ms |
30248 KB |
Output is correct |
65 |
Correct |
128 ms |
30396 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
129 ms |
33720 KB |
Output is correct |
68 |
Correct |
143 ms |
33728 KB |
Output is correct |
69 |
Correct |
164 ms |
33692 KB |
Output is correct |
70 |
Correct |
173 ms |
33724 KB |
Output is correct |
71 |
Correct |
153 ms |
33756 KB |
Output is correct |
72 |
Correct |
160 ms |
33816 KB |
Output is correct |
73 |
Correct |
148 ms |
34252 KB |
Output is correct |
74 |
Correct |
135 ms |
34268 KB |
Output is correct |
75 |
Correct |
196 ms |
34104 KB |
Output is correct |
76 |
Correct |
183 ms |
34216 KB |
Output is correct |
77 |
Correct |
125 ms |
33484 KB |
Output is correct |
78 |
Correct |
0 ms |
316 KB |
Output is correct |
79 |
Correct |
1 ms |
596 KB |
Output is correct |
80 |
Correct |
1 ms |
596 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
1 ms |
596 KB |
Output is correct |
83 |
Correct |
1 ms |
596 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
33 ms |
3152 KB |
Output is correct |
87 |
Correct |
32 ms |
3300 KB |
Output is correct |
88 |
Correct |
40 ms |
3444 KB |
Output is correct |
89 |
Correct |
30 ms |
3532 KB |
Output is correct |
90 |
Correct |
27 ms |
3448 KB |
Output is correct |
91 |
Correct |
24 ms |
3416 KB |
Output is correct |
92 |
Correct |
26 ms |
3028 KB |
Output is correct |
93 |
Correct |
95 ms |
32020 KB |
Output is correct |
94 |
Correct |
87 ms |
31220 KB |
Output is correct |
95 |
Correct |
110 ms |
31864 KB |
Output is correct |
96 |
Correct |
109 ms |
31952 KB |
Output is correct |
97 |
Correct |
91 ms |
32056 KB |
Output is correct |
98 |
Correct |
95 ms |
31940 KB |
Output is correct |
99 |
Correct |
80 ms |
31308 KB |
Output is correct |
100 |
Correct |
249 ms |
33844 KB |
Output is correct |
101 |
Correct |
217 ms |
33920 KB |
Output is correct |
102 |
Correct |
149 ms |
33984 KB |
Output is correct |
103 |
Correct |
134 ms |
33444 KB |
Output is correct |
104 |
Correct |
130 ms |
33340 KB |
Output is correct |
105 |
Correct |
138 ms |
33480 KB |
Output is correct |
106 |
Correct |
180 ms |
33176 KB |
Output is correct |
107 |
Correct |
238 ms |
33268 KB |
Output is correct |
108 |
Correct |
152 ms |
33816 KB |
Output is correct |
109 |
Correct |
158 ms |
33864 KB |
Output is correct |
110 |
Correct |
140 ms |
33728 KB |
Output is correct |
111 |
Correct |
134 ms |
33864 KB |
Output is correct |