#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
int f[N];
int l[N], r[N];
bool can_switch(int i, int j){
return l[j] <= r[i] && r[i] <= r[j];
}
void solve(){
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++){
cin >> l[i] >> r[i];
}
for (int i = 0; i < n; i++){
f[i] = -1;
for (int j = 0; j < n; j++){
if (i == j) continue;
if (can_switch(j, i)){
if (f[i] == -1 || l[j] < l[f[i]]) f[i] = j;
}
}
if (l[f[i]] >= l[i]) f[i] = -1;
}
while (q--){
int x, y;
cin >> x >> y;
--x, --y;
if (x == y){
cout << 0 << endl;
continue;
}
if (can_switch(x, y)){
cout << 1 << endl;
continue;
}
int cnt = 1;
while (l[y] > r[x]){
y = f[y];
cnt++;
if (y == -1) break;
}
if (y != -1){
cout << cnt << endl;
} else {
cout << "impossible" << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1553 ms |
3208 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1562 ms |
2824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1553 ms |
3208 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |