#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define debug(x) cout << #x << " = " << x << '\n'
#define all(a) a.begin(), a.end()
#define SZ(a) (int)(a).size()
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const ll inf64 = 3e18;
const int inf32 = 2e9 + 5;
struct FenwickTree {
int n;
vector<pii> node;
FenwickTree() {}
FenwickTree(int n) : n(n), node(n + 3, make_pair(0, 0)) {}
void upd(int i, pii p) {
for(; i <= n; i += i & (-i))
node[i] = max(node[i], p);
}
pii get(int i) {
pii res = make_pair(0, 0);
for(; i; i -= i & (-i))
res = max(res, node[i]);
return res;
}
int getidx(int i) { return get(i).se; }
};
int n, q, pos[N];
struct Event { int l, r, idx; } a[N];
const int LG = 18;
int jump[LG][N], st[LG][N * 2];
int Jumping(int s, int d) {
if(d == 0) return s;
for(int k = 0; (1 << k) <= d; k++)
if(d >> k & 1) s = jump[k][s];
return s;
}
int getMin(int l, int r) {
int k = __lg(r - l + 1);
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
void solve() {
cin >> n >> q;
vector<int> cprs;
for(int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
a[i].idx = i;
cprs.push_back(a[i].l);
cprs.push_back(a[i].r);
}
sort(all(cprs)); cprs.erase(unique(all(cprs)), cprs.end());
for(int i = 1; i <= n; i++) {
a[i].l = lower_bound(all(cprs), a[i].l) - cprs.begin() + 1;
a[i].r = lower_bound(all(cprs), a[i].r) - cprs.begin() + 1;
}
sort(a + 1, a + 1 + n, [](Event x, Event y) { return x.r < y.r; });
for(int i = 1; i <= n; i++)
pos[a[i].idx] = i;
FenwickTree fen(SZ(cprs));
for(int i = n, j = n; i >= 1; i--) {
while(j > i && a[j].r > a[i].r) {
fen.upd(a[j].l, make_pair(a[j].r, j));
j--;
}
jump[0][i] = fen.getidx(a[i].r);
if(!jump[0][i]) jump[0][i] = n + 1;
}
jump[0][n + 1] = n + 1;
for(int k = 1; k < LG; k++) {
for(int i = 1; i <= n + 1; i++)
jump[k][i] = jump[k - 1][jump[k - 1][i]];
}
for(int i = 1; i <= SZ(cprs); i++)
st[0][i] = inf32;
for(int i = 1; i <= n; i++)
st[0][a[i].r] = min(st[0][a[i].r], a[i].l);
for(int k = 1; k < LG; k++) {
for(int i = 1; i + (1 << k) - 1 <= SZ(cprs); i++)
st[k][i] = min(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}
while(q--) {
int S, E; cin >> S >> E;
S = pos[S], E = pos[E];
if(S == E) {
cout << "0\n";
continue;
}
if(a[S].r > a[E].r) {
cout << "impossible\n";
continue;
}
if(a[S].r >= a[E].l) {
cout << "1\n";
continue;
}
int le = 1, ri = n, d = 0;
while(le <= ri) {
int mid = (le + ri) >> 1;
int p = Jumping(S, mid);
if(p == n + 1 || a[p].r >= a[E].l) ri = mid - 1;
else d = mid, le = mid + 1;
}
int i = Jumping(S, d);
if(getMin(a[E].l, a[E].r) <= a[i].r) cout << d + 2 << '\n';
else cout << "impossible\n";
}
}
int main() {
auto start = chrono::steady_clock::now();
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) solve();
chrono::duration<double> elapsed {chrono::steady_clock::now() - start};
cerr << "\n>> Runtime: " << elapsed.count() << "s\n";
}
컴파일 시 표준 에러 (stderr) 메시지
events.cpp: In function 'int main()':
events.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen("output.txt", "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... |