This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 && can_switch(x, y)){
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |