#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 + (u != v) << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
127 ms |
15724 KB |
Output is correct |
3 |
Correct |
138 ms |
15692 KB |
Output is correct |
4 |
Correct |
175 ms |
15760 KB |
Output is correct |
5 |
Correct |
151 ms |
19316 KB |
Output is correct |
6 |
Correct |
141 ms |
19860 KB |
Output is correct |
7 |
Correct |
143 ms |
19928 KB |
Output is correct |
8 |
Correct |
135 ms |
23988 KB |
Output is correct |
9 |
Correct |
142 ms |
24068 KB |
Output is correct |
10 |
Correct |
175 ms |
19296 KB |
Output is correct |
11 |
Incorrect |
167 ms |
21060 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
472 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
472 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
472 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
15784 KB |
Output is correct |
2 |
Correct |
161 ms |
15692 KB |
Output is correct |
3 |
Correct |
152 ms |
15856 KB |
Output is correct |
4 |
Correct |
176 ms |
24056 KB |
Output is correct |
5 |
Correct |
137 ms |
19272 KB |
Output is correct |
6 |
Incorrect |
205 ms |
23708 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
127 ms |
15724 KB |
Output is correct |
3 |
Correct |
138 ms |
15692 KB |
Output is correct |
4 |
Correct |
175 ms |
15760 KB |
Output is correct |
5 |
Correct |
151 ms |
19316 KB |
Output is correct |
6 |
Correct |
141 ms |
19860 KB |
Output is correct |
7 |
Correct |
143 ms |
19928 KB |
Output is correct |
8 |
Correct |
135 ms |
23988 KB |
Output is correct |
9 |
Correct |
142 ms |
24068 KB |
Output is correct |
10 |
Correct |
175 ms |
19296 KB |
Output is correct |
11 |
Incorrect |
167 ms |
21060 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |