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;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
#define MASK(x) (1LL << (x))
const int N = (int) 1e5 + 5;
const int LOG = 17;
int n, m, q;
pair<int, int> event[N];
int up[LOG][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> n >> q;
vector<int> R;
for (int i = 1; i <= n; ++i) {
cin >> event[i].first >> event[i].second;
R.emplace_back(event[i].second);
}
auto cmb = [&](int x, int y) {
if (x == -1) {
return y;
}
if (y == -1) {
return x;
}
return event[x].first < event[y].first ? x : y;
};
sort(R.begin(), R.end());
R.erase(unique(R.begin(), R.end()), R.end());
m = (int) R.size();
vector<int> tree(4 << __lg(m), -1);
auto upd = [&](int i, int x) {
int id = 1, l = 1, r = m;
while (l < r) {
int mid = l + r >> 1;
id <<= 1;
if (i <= mid) {
r = mid;
} else {
id |= 1;
l = mid + 1;
}
}
tree[id] = cmb(tree[id], x);
while (id > 1) {
id >>= 1;
tree[id] = cmb(tree[id << 1], tree[id << 1 | 1]);
}
};
auto get = [&](auto get, int u, int v, int id = 1, int l = 1, int r = m) {
if (u <= l && r <= v) {
return tree[id];
}
int mid = l + r >> 1;
if (u <= mid && mid < v) {
return cmb(get(get, u, v, id << 1, l, mid), get(get, u, v, id << 1 | 1, mid + 1, r));
}
if (u <= mid) {
return get(get, u, v, id << 1, l, mid);
}
return get(get, u, v, id << 1 | 1, mid + 1, r);
};
for (int i = 1; i <= n; ++i) {
int tr = upper_bound(R.begin(), R.end(), event[i].second) - R.begin();
upd(tr, i);
}
for (int i = 1; i <= n; ++i) {
int L = lower_bound(R.begin(), R.end(), event[i].first) - R.begin() + 1;
int tr = upper_bound(R.begin(), R.end(), event[i].second) - R.begin();
up[0][i] = get(get, L, tr);
}
for (int j = 1; j < LOG; ++j) {
for (int i = 1; i <= n; ++i) {
up[j][i] = up[j - 1][up[j - 1][i]];
}
}
auto reach = [&](int u, int v) {
if (v == -1) {
return false;
}
return event[v].first <= event[u].second && event[u].second <= event[v].second;
};
auto qry = [&](int s, int t) {
if (t == s) {
cout << 0 << "\n";
return;
}
if (event[s].second > event[t].second) {
cout << "impossible\n";
return;
}
if (event[t].first <= event[s].second) {
cout << (reach(s, t) ? "1" : "impossible") << "\n";
return;
}
int res = 0;
for (int i = LOG - 1; i >= 0; --i) {
int u = up[i][t];
if (u != -1 && event[u].first > event[s].second) {
t = u;
res += MASK(i);
}
}
t = up[0][t];
if (reach(s, t)) {
cout << res + 2 << "\n";
} else {
cout << "impossible\n";
}
};
while (q--) {
int s, t;
cin >> s >> t;
qry(s, t);
}
}
Compilation message (stderr)
events.cpp: In lambda function:
events.cpp:49:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int mid = l + r >> 1;
| ~~^~~
events.cpp: In instantiation of 'main()::<lambda(auto:23, int, int, int, int, int)> [with auto:23 = main()::<lambda(auto:23, int, int, int, int, int)>]':
events.cpp:84:34: required from here
events.cpp:68:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
68 | int mid = l + r >> 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... |