#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 = lift[b][i];
}
}
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 |
152 ms |
33688 KB |
Output is correct |
3 |
Correct |
129 ms |
33764 KB |
Output is correct |
4 |
Correct |
135 ms |
33784 KB |
Output is correct |
5 |
Correct |
130 ms |
33848 KB |
Output is correct |
6 |
Correct |
132 ms |
33812 KB |
Output is correct |
7 |
Correct |
182 ms |
33948 KB |
Output is correct |
8 |
Correct |
133 ms |
34240 KB |
Output is correct |
9 |
Correct |
150 ms |
34248 KB |
Output is correct |
10 |
Correct |
168 ms |
34128 KB |
Output is correct |
11 |
Correct |
163 ms |
34116 KB |
Output is correct |
12 |
Correct |
128 ms |
33464 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 |
1 ms |
608 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
608 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
608 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
33816 KB |
Output is correct |
2 |
Correct |
151 ms |
33748 KB |
Output is correct |
3 |
Correct |
135 ms |
33700 KB |
Output is correct |
4 |
Correct |
129 ms |
34156 KB |
Output is correct |
5 |
Correct |
161 ms |
34016 KB |
Output is correct |
6 |
Correct |
166 ms |
33856 KB |
Output is correct |
7 |
Correct |
172 ms |
33808 KB |
Output is correct |
8 |
Correct |
128 ms |
34084 KB |
Output is correct |
9 |
Correct |
88 ms |
31944 KB |
Output is correct |
10 |
Correct |
118 ms |
33472 KB |
Output is correct |
11 |
Correct |
111 ms |
33256 KB |
Output is correct |
12 |
Correct |
122 ms |
33508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
152 ms |
33688 KB |
Output is correct |
3 |
Correct |
129 ms |
33764 KB |
Output is correct |
4 |
Correct |
135 ms |
33784 KB |
Output is correct |
5 |
Correct |
130 ms |
33848 KB |
Output is correct |
6 |
Correct |
132 ms |
33812 KB |
Output is correct |
7 |
Correct |
182 ms |
33948 KB |
Output is correct |
8 |
Correct |
133 ms |
34240 KB |
Output is correct |
9 |
Correct |
150 ms |
34248 KB |
Output is correct |
10 |
Correct |
168 ms |
34128 KB |
Output is correct |
11 |
Correct |
163 ms |
34116 KB |
Output is correct |
12 |
Correct |
128 ms |
33464 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
608 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |