#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)
cout << "impossible\n";
else
cout << l + 1 << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
266 ms |
22492 KB |
Output is correct |
3 |
Correct |
271 ms |
22216 KB |
Output is correct |
4 |
Correct |
295 ms |
23004 KB |
Output is correct |
5 |
Correct |
277 ms |
22224 KB |
Output is correct |
6 |
Correct |
278 ms |
22300 KB |
Output is correct |
7 |
Correct |
286 ms |
22588 KB |
Output is correct |
8 |
Correct |
278 ms |
22776 KB |
Output is correct |
9 |
Correct |
301 ms |
22852 KB |
Output is correct |
10 |
Incorrect |
295 ms |
22276 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
275 ms |
22232 KB |
Output is correct |
2 |
Correct |
271 ms |
22300 KB |
Output is correct |
3 |
Correct |
286 ms |
22352 KB |
Output is correct |
4 |
Correct |
279 ms |
22804 KB |
Output is correct |
5 |
Incorrect |
274 ms |
22200 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
266 ms |
22492 KB |
Output is correct |
3 |
Correct |
271 ms |
22216 KB |
Output is correct |
4 |
Correct |
295 ms |
23004 KB |
Output is correct |
5 |
Correct |
277 ms |
22224 KB |
Output is correct |
6 |
Correct |
278 ms |
22300 KB |
Output is correct |
7 |
Correct |
286 ms |
22588 KB |
Output is correct |
8 |
Correct |
278 ms |
22776 KB |
Output is correct |
9 |
Correct |
301 ms |
22852 KB |
Output is correct |
10 |
Incorrect |
295 ms |
22276 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |