#include <bits/stdc++.h>
using namespace std;
const int Log = 18;
const int N = 100005;
int up[N][Log];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
map<int, int> mp;
vector<int> l(n + 1), r(n + 1);
vector<array<int, 3>> pts(n * 2); //L / R, idx, 0 / 1
for (int i = 1; i <= n; ++i) {
cin >> l[i] >> r[i];
pts[(i - 1) * 2] = {l[i], 0, i};
pts[(i - 1) * 2 + 1] = {r[i], 1, i};
mp[l[i]]; mp[r[i]];
}
// int idd = 0;
// for (auto& it : mp) {
// it.second = ++idd;
// }
// for (int i = 0; i < n * 2; ++i) {
// pts[i][0] = mp[pts[i][0]];
// }
sort(pts.begin(), pts.end(), [&](array<int, 3> x, array<int, 3> y) {
return x[0] == y[0] ? x[1] < y[1] : x[0] < y[0];
});
// for (int i = 0; i < n * 2; ++i) {
// cout << pts[i][0] << ' ' << pts[i][1] << ' ' << pts[i][2] << '\n';
// }
int mx = 0;
for (int i = 0; i < n * 2; ++i) {
if (pts[i][1] == 0) {//isL
mx = (r[pts[i][2]] > r[mx] ? pts[i][2] : mx);
} else {//isR
up[pts[i][2]][0] = mx;
}
}
// for (int i = 1; i <= n; ++i) {
// cout << up[i][0] << ' ';
// }
for (int i = 1; i < Log; ++i) {
for (int j = 1; j <= n; ++j) {
up[j][i] = up[up[j][i - 1]][i - 1];
assert(up[j][i] != 0);
}
}
auto intersect = [&]( int y, int i, int j) -> bool {
return y >= i && y <= j;
};
while (q--) {
int u, v;
cin >> u >> v;
if (u == v) {
cout << 0 << '\n';
continue;
}
int ans = 0;
for (int i = Log - 1; i >= 0; --i) {
if (r[up[u][i]] < l[v]) {
ans += 1 << i;
u = up[u][i];
}
}
ans++;
u = up[u][0];
if (!intersect(r[u], l[v], r[v])) {
cout << "impossible" << '\n';
} else {
cout << ans + 1 << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
135 ms |
19004 KB |
Output is correct |
3 |
Correct |
127 ms |
18916 KB |
Output is correct |
4 |
Incorrect |
167 ms |
18764 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
18848 KB |
Output is correct |
2 |
Correct |
150 ms |
18908 KB |
Output is correct |
3 |
Incorrect |
163 ms |
18812 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
135 ms |
19004 KB |
Output is correct |
3 |
Correct |
127 ms |
18916 KB |
Output is correct |
4 |
Incorrect |
167 ms |
18764 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |