#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n"){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int INF = 1e9 + 69, LOG_N = 17;
bool isIn(int x, pair<int, int> range){
return range.first <= x && x <= range.second;
}
struct SegmentTree{
int n;
vector<pair<int,int>> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 2 + 2, {INF, -1});
}
void update(int i, pair<int, int> v){
i += n; a[i] = v;
while(i > 1){
i >>= 1;
a[i] = min(a[i * 2], a[i * 2 + 1]);
}
}
pair<int, int> get(int l, int r){
l += n; r += n + 1;
pair<int, int> ans = {INF, -1};
while(l < r){
if (l & 1) minimize(ans, a[l++]);
if (r & 1) minimize(ans, a[--r]);
l >>= 1; r >>= 1;
}
return ans;
}
};
void solve(){
int n, q; cin >> n >> q;
vector<pair<int, int>> a(n);
for(int i =0; i<n; ++i) cin >> a[i].first >> a[i].second;
vector<int> x;
for(pair<int, int> i: a) x.push_back(i.second);
remove_dup(x);
int m = x.size();
vector<pair<int, int>> best(m, {INF, -1});
for(int i = 0; i < n; ++i) {
int idx = lower_bound(ALL(x), a[i].second) - x.begin();
minimize(best[idx], make_pair(a[i].second, i));
}
SegmentTree st(m);
for(int i = 1; i <= m; ++i) st.update(i, best[i-1]);
int nxt[LOG_N][n]; memset(nxt, 0, sizeof nxt);
for(int i = 0; i < n; ++i){
int l = lower_bound(ALL(x), a[i].first) - x.begin();
int r = lower_bound(ALL(x), a[i].second) - x.begin();
nxt[0][i] = st.get(l+1, r+1).second;
}
for(int j = 1; j < LOG_N; ++j) for(int i = 0; i < n; ++i) nxt[j][i] = nxt[j-1][nxt[j-1][i]];
while(q--){
int u, v; cin >> u >> v;
u--; v--;
if (u == v) cout << 0 << "\n";
else if (a[v].second < a[u].second) cout << "impossible\n";
else if (isIn(a[u].second, a[v])) cout << 1 << "\n";
else{
swap(u, v);
int ans = 0;
for(int j = LOG_N - 1; j >= 0; --j){
int _u = nxt[j][u];
if (a[_u].first > a[v].second){
u = _u;
ans += MASK(j);
}
}
if (ans == MASK(LOG_N) - 1) cout << "impossible\n";
else cout << ans + 2 << "\n";
}
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\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... |