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>
#define f first
#define s second
#define ent '\n'
//#define int long long
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 12;
const int mod = 1e9 + 7;
int up[22][maxn];
int l[maxn], r[maxn];
pair<int, int> t[maxn * 4];
int n, m, q;
void upd(int v, int tl, int tr, int pos, pair<int, int> x) {
if(tl == tr) {
t[v] = min(t[v], x);
}
else {
int mid = tl + tr >> 1;
if(pos <= mid) {
upd(v * 2, tl, mid, pos, x);
}
else {
upd(v * 2 + 1, mid + 1, tr, pos, x);
}
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
}
pair<int, int> get(int v, int tl, int tr, int l, int r) {
if(tl > r || l > tr) return {1e18, 1e18};
if(tl >= l && tr <= r) return t[v];
int mid = tl + tr >> 1;
return min(get(v * 2, tl, mid, l, r), get(v * 2 + 1, mid + 1, tr, l, r));
}
void solve() {
cin >> n >> q;
vector<int> pos;
for(int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
pos.push_back(l[i]);
pos.push_back(r[i]);
}
sort(pos.begin(), pos.end());
pos.resize(unique(pos.begin(), pos.end()) - pos.begin());
m = (int)pos.size();
for(int i = 1; i <= m * 4; i++) {
t[i] = {1e18, 1e18};
}
for(int i = 1; i <= n; i++) {
l[i] = lower_bound(pos.begin(), pos.end(), l[i]) - pos.begin();
r[i] = lower_bound(pos.begin(), pos.end(), r[i]) - pos.begin();
l[i]++, r[i]++;
upd(1, 1, m, r[i], {l[i], i});
}
for(int i = 1; i <= n; i++) {
up[0][i] = get(1, 1, m, l[i], r[i]).second;
}
for(int k = 1; k < 20; k++) {
for(int i = 1; i <= n; i++) {
up[k][i] = up[k - 1][up[k - 1][i]];
}
}
while(q--) {
int i, j;
cin >> i >> j;
if(r[i] > r[j]) {
cout << "impossible\n";
continue;
}
if(i == j) {
cout << "0\n";
continue;
}
if(r[i] >= l[j]) {
cout << "1\n";
continue;
}
int ans = 0;
for(int k = 19; k >= 0; k--) {
if(up[k][j] != 0 && l[up[k][j]] > r[i]) {
j = up[k][j];
ans += (1 << k);
}
}
ans++;
j = up[0][j];
if(i != j) {
ans++;
}
if(l[j] > r[i] || ans > n) {
cout << "impossible\n";
}
else {
cout << ans << ent;
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
Compilation message (stderr)
events.cpp: In function 'void upd(int, int, int, int, std::pair<int, int>)':
events.cpp:22:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = tl + tr >> 1;
| ~~~^~~~
events.cpp: In function 'std::pair<int, int> get(int, int, int, int, int)':
events.cpp:36:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid = tl + tr >> 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... |