#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 25;
const int B = 18;
int dp[B][MAXN];
int n, q;
pair <int, int> a[MAXN];
int jump (int a, int b) {
for (int i = B - 1; i >= 0; i--) {
if (b & (1 << i)) {
a = dp[i][a];
}
}
return a;
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
for (int i = 1; i <= n; i++) {
dp[0][i] = i;
for (int j = 1; j <= n; j++) {
if (a[j].second >= a[i].first && a[j].second <= a[i].second) {
if (a[j].first < a[dp[0][i]].first) dp[0][i] = j;
}
}
}
for (int i = 1; i < B; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
while (q--) {
int x, y; cin >> x >> y;
if (x == y) {
cout << "0\n";
continue;
}
int l = 0, r = (1 << (B + 1)) - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
int z = jump(y, mid);
if (z == 0) {
r = mid - 1; continue;
}
if (a[z].first > a[x].second) {
l = mid + 1; ans = mid;
} else {
r = mid - 1;
}
}
y = jump(y, ans + 1);
if (a[x].second <= a[y].second && a[x].second >= a[y].first) {
cout << ans + 2 << '\n';
} else {
cout << "impossible\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Execution timed out |
1545 ms |
4600 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
4 ms |
6624 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
4 ms |
6644 KB |
Output is correct |
7 |
Correct |
5 ms |
6492 KB |
Output is correct |
8 |
Correct |
5 ms |
6648 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
4 ms |
6624 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
4 ms |
6644 KB |
Output is correct |
7 |
Correct |
5 ms |
6492 KB |
Output is correct |
8 |
Correct |
5 ms |
6648 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
4 ms |
6488 KB |
Output is correct |
13 |
Correct |
4 ms |
6492 KB |
Output is correct |
14 |
Correct |
4 ms |
6616 KB |
Output is correct |
15 |
Correct |
4 ms |
6492 KB |
Output is correct |
16 |
Correct |
5 ms |
6488 KB |
Output is correct |
17 |
Correct |
4 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
167 ms |
7964 KB |
Output is correct |
20 |
Correct |
146 ms |
7996 KB |
Output is correct |
21 |
Correct |
154 ms |
8344 KB |
Output is correct |
22 |
Correct |
144 ms |
8428 KB |
Output is correct |
23 |
Correct |
156 ms |
8276 KB |
Output is correct |
24 |
Correct |
135 ms |
8164 KB |
Output is correct |
25 |
Correct |
140 ms |
8004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
4 ms |
6624 KB |
Output is correct |
4 |
Correct |
4 ms |
6492 KB |
Output is correct |
5 |
Correct |
5 ms |
6492 KB |
Output is correct |
6 |
Correct |
4 ms |
6644 KB |
Output is correct |
7 |
Correct |
5 ms |
6492 KB |
Output is correct |
8 |
Correct |
5 ms |
6648 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
4 ms |
6492 KB |
Output is correct |
13 |
Correct |
4 ms |
6492 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
4 ms |
6492 KB |
Output is correct |
16 |
Correct |
5 ms |
6624 KB |
Output is correct |
17 |
Correct |
4 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Execution timed out |
1558 ms |
4444 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1559 ms |
4700 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Execution timed out |
1545 ms |
4600 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |