#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 1e5 + 5;
const int inf = 1e18 + 7;
const int LOG = 20;
const int mod = 1e9 + 7;
int n, q;
pii a[maxN];
int up[maxN][LOG], cost[maxN][LOG];
struct ST {
struct Node {
int l, pos;
bool operator < (const Node &other) const {
return l < other.l;
}
};
vector <Node> st;
void init (int n) {
st.resize (n << 2, {inf, inf});
}
void upd (int id, int l, int r, int pos, int val, int curSeg) {
if (pos < l || pos > r) {
return;
}
if (l == r) {
st[id] = min (st[id], {val, curSeg});
return;
}
int mid = (l + r) >> 1;
upd (id * 2, l, mid, pos, val, curSeg);
upd (id * 2 + 1, mid + 1, r, pos, val, curSeg);
st[id] = min (st[id * 2], st[id * 2 + 1]);
}
Node get (int id, int l, int r, int u, int v) {
if (u > r || v < l) {
return {inf, inf};
}
if (u <= l && r <= v) {
return st[id];
}
int mid = (l + r) >> 1;
auto tL = get (id * 2, l, mid, u, v);
auto tR = get (id * 2 + 1, mid + 1, r, u, v);
return min (tL, tR);
}
};
ST T;
void solve () {
cin >> n >> q;
vector <int> zip;
for (int i = 1; i <= n; i ++) {
cin >> a[i].fi >> a[i].se;
zip.push_back (a[i].fi);
zip.push_back (a[i].se);
}
sort (zip.begin (), zip.end ());
zip.erase (unique (zip.begin (), zip.end ()), zip.end ());
int lim = zip.size ();
T.init (lim);
for (int i = 1; i <= n; i ++) {
a[i].fi = lower_bound (zip.begin (), zip.end (), a[i].fi) - zip.begin () + 1;
a[i].se = lower_bound (zip.begin (), zip.end (), a[i].se) - zip.begin () + 1;
T.upd (1, 1, lim, a[i].se, a[i].fi, i);
}
for (int i = 1; i <= n; i ++) {
auto [bestL, bestid] = T.get (1, 1, lim, a[i].fi, a[i].se);
up[i][0] = ((bestL == a[i].fi) ? i : bestid);
//cout << bestL << ' ' << bestid << '\n';
cost[i][0] = ((bestL == a[i].fi) ? 0 : 1);
}
for (int j = 1; j < LOG; j ++) {
for (int i = 1; i <= n; i ++) {
up[i][j] = up[up[i][j - 1]][j - 1];
cost[i][j] = cost[up[i][j - 1]][j - 1] + cost[i][j - 1];
}
}
while (q --) {
int s, t;
cin >> s >> t;
if (s == t) {
cout << "0\n";
continue;
}
if (a[s].fi >= a[t].se) {
cout << "impossible\n";
continue;
}
int res = 0;
for (int j = LOG - 1; j >= 0; j --) {
if (a[up[t][j]].fi > a[s].se) {
res += cost[t][j];
t = up[t][j];
}
}
int flg = 1;
for (int i = 1; i <= 3; i ++) {
if (a[s].se >= a[t].fi && a[s].se <= a[t].se) {
cout << res + i << '\n';
flg = 0;
break;
}
t = up[t][0];
}
if (flg) {
cout << "impossible\n";
}
}
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfdgb
Compilation message (stderr)
events.cpp: In function 'int main()':
events.cpp:135:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:136:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |