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;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define all(x) (x).begin(),(x).end()
const int maxn=1e5+10;
const int LOG=int(log2(maxn))+1;
vector<pi> events(maxn);
vector<vi> table(maxn,vi(LOG,-1));
vector<vector<pi>> rmq(LOG,vector<pi>(maxn,{1000000010,-1}));
vi pow2(LOG,1);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin >> n >> q;
vi b;
for (int i = 0; i < n; i++) {
cin >> events[i].fi >> events[i].se;
b.push_back(events[i].se);
}
sort(all(b));
b.erase(unique(all(b)),b.end());
for (int i=0; i<n; i++) {
int r=lower_bound(all(b),events[i].se)-b.begin();
rmq[0][r]=min(rmq[0][r],{events[i].fi,i});
}
for (int i=0; i+1<LOG; i++) {
for (int j=0; j+(1<<(i+1))<=n; j++) {
rmq[i+1][j]=min(rmq[i][j],rmq[i][j+(1<<i)]);
}
}
for (int i=0; i<n; i++) {
int l=lower_bound(all(b),events[i].fi)-b.begin();
int r=lower_bound(all(b),events[i].se)-b.begin();
int h=31-__builtin_clz(r-l+1);
table[i][0]=min(rmq[h][l],rmq[h][r+1-(1<<h)]).se;
}
for (int i=0; i+1<LOG; i++) {
for (int j = 0; j < n; j++) {
table[j][i+1]=table[table[j][i]][i];
}
}
int x,y,l;
for (int i=0; i<q; i++) {
l=1;
cin >> x >> y;
x--;
y--;
if (x==y) {
cout << "0\n";
continue;
}
if (events[y].fi<=events[x].se && events[x].se<=events[y].se) {
cout << "1\n";
continue;
}
for (int i=LOG-1; i>=0; i--) {
if (events[x].se<events[table[y][i]].fi) {
l+=1<<i;
y=table[y][i];
}
}
y=table[y][0];
if (events[x].se<events[y].fi || events[x].se>events[y].se) {
cout << "impossible\n";
}
else {
cout << l+(events[x].se>=events[y].fi) << "\n";
}
}
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... |