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 pb push_back
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define vout(v) cout<<#v<<' '; for(auto u : v){cout<<u<<' ';} cout<<'\n'
#define dout(a) cout<<a<<' '<<#a<<'\n'
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<'\n'
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
#define left second
#define right first
using namespace std;
const int leg = 1e9 + 7;
const int mod = 998244353;
const int mxn = 1e5 + 5;
int tree[mxn * 4];
int n,q;
vector<pair<pii,int>> v;
int mn(int a, int b){
if(a == -1)return b;
if(b == -1)return a;
return v[a].first.left < v[b].first.left ? a : b;
}
void update(int k, int x){
k += n;
tree[k] = x;
for(k /=2; k>=1; k/=2){
tree[k] = mn(tree[k*2], tree[k*2+1]);
}
}
int quer(int l, int r){
l += n;
r += n;
int ret = -1;
while(l <= r){
if(l % 2 == 1){
ret = mn(ret, tree[l++]);
}
if(r % 2 == 0){
ret = mn(ret, tree[r--]);
}
l/=2;
r/=2;
}
return ret;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
cin>>n>>q;
f0r(i,n){
in2(a,b);
v.pb({{b,a},i});
}
sort(rall(v));
f0r(i,n){
update(i, i);
}
vi dex(n);
f0r(i,n)dex[v[i].second] = i;
f0r(i,n){
//out2(v[i].first.first, v[i].first.second);
}
vi nxt(n);
f0r(i,n){
int l = i;
int r = n-1;
while(l < r){
int mid = l + (r - l + 1)/2;
if(v[i].right.left <= v[mid].right.right){
l = mid;
}
else{
r = mid - 1;
}
}
if(l != i){
//out2(i+1, l);
nxt[i] = quer(i+1, l);
}
else{
nxt[i] = -1;
}
}
//vout(nxt);
vvi jmp(n, vi(17));
f0r(i,n){
jmp[i][0] = nxt[i];
}
FOR(i, 1, 17){
f0r(j, n){
if(jmp[j][i-1] == -1)jmp[j][i] = -1;
else jmp[j][i] = jmp[jmp[j][i-1]][i-1];
}
}
//vout(jmp[0]);
f0r(i,n){
//cout<<jmp[i][0]<<' ';
}
//out("");
f0r(i, q){
in2(a, b);
if(a == b)out(0);
else{
a--;
b--;
swap(a,b);
a = dex[a];
b = dex[b];
int goal = v[b].right.right;
if(v[a].first.left <= goal && goal <= v[a].first.right){
out(1);
}
else{
//out3(a,b,goal);
int cnt = 0;
for(int i = 16; i>=0; i--){
if(jmp[a][i] != -1){
if(v[jmp[a][i]].right.left > goal){
//out("HI");
cnt += (1 << i);
a = jmp[a][i];
}
}
}
if(nxt[a] == -1 || !(v[nxt[a]].right.left <= goal && goal <= v[nxt[a]].right.right))out("impossible");
else out(cnt+2);
}
}
}
}
# | 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... |