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;
using ll = long long;
int n, q, sp[100100][20], mn[100100][20], R[100100], rev[100100];
array<int, 3> a[100100];
int find(int a, int b){
int k = 31 - __builtin_clz(b - a);
return min(mn[a][k], mn[b-(1<<k)+1][k]);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for(int i=1;i<=n;i++) cin >> a[i][1] >> a[i][0], a[i][2] = i;
sort(a+1, a+1+n);
for(int i=n;i>=1;i--){
auto [e, s, id] = a[i];
R[i] = a[i+1][0] == e ? R[i+1] : i;
rev[id] = i;
mn[i][0] = lower_bound(a+1, a+1+n, array<int, 3>({s, 0, 0})) - a;
}
for(int j=1;j<20;j++) for(int i=1;i<=n;i++) if(i+(1<<j)-1 <= n) mn[i][j] = min(mn[i][j-1], mn[i+(1<<j-1)][j-1]);
for(int i=1;i<=n;i++) sp[i][0] = find(i, R[i]);
for(int j=1;j<20;j++) for(int i=1;i<=n;i++) sp[i][j] = min(sp[i][j-1], sp[sp[i][j-1]][j-1]);
while(q--){
int a, b; cin >> a >> b;
a = rev[a], b = rev[b];
if(R[b] < a) {
cout << "impossible\n";
} else if(a == b) {
cout << "0\n";
} else {
int ans = 0;
for(int i=19;i>=0;i--) if(sp[b][i] > a) b = sp[b][i], ans += 1 << i;
ans++, b = sp[b][0];
if(a < b) cout << "impossible\n";
else cout << ans << "\n";
}
}
}
Compilation message (stderr)
events.cpp: In function 'int main()':
events.cpp:27:106: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
27 | for(int j=1;j<20;j++) for(int i=1;i<=n;i++) if(i+(1<<j)-1 <= n) mn[i][j] = min(mn[i][j-1], mn[i+(1<<j-1)][j-1]);
| ~^~
# | 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... |