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>
#define f first
#define s second
#define ent '\n'
#define int long long
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 12;
const int mod = 1e9 + 7;
int up[20][maxn];
int l[maxn], r[maxn];
int n, q;
void solve() {
cin >> n >> q;
vector<pair<int, int>> u;
for(int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
u.push_back({l[i], -i});
u.push_back({r[i], i});
}
sort(u.begin(), u.end());
while(q--) {
int i, j;
cin >> i >> j;
if(r[i] > r[j]) {
cout << "impossible\n";
continue;
}
if(i == j) {
cout << "0\n";
continue;
}
int ans = 0;
pair<int, int> mx = {-1, -1};
for(auto [x, pos] : u) {
if(pos < 0) {
if(r[-pos] > r[j]) continue;
mx = max(mx, {r[-pos], -pos});
}
else {
up[0][pos] = mx.second;
}
}
for(int _ = 0; _ <= n && i != j; _++) {
ans++;
i = up[0][i];
}
if(ans > n) {
cout << "impossible\n";
}
else {
cout << ans << ent;
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
# | 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... |