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 x first
#define y second
#define pii pair<int,int>
using namespace std;
using ll=long long;
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
const int N=100010,MOD=1e9+7,M=1<<17;
const char en='\n';
const ll LLINF=1ll<<60;
int n,q,l[N],r[N],par[20][N];
pii seg[M*2+10];
pii ge(int l,int r,int lo=0,int hi=M,int i=1)
{
if (lo>=l && hi<=r) return seg[i];
if (lo>=r || hi<=l) return {MOD,MOD};
int mid=(lo+hi)/2;
return min(ge(l,r,lo,mid,i*2),ge(l,r,mid,hi,i*2+1));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i=0;i<2*M;++i) seg[i]={MOD,MOD};
cin>>n>>q;
vector<pair<pii,int>> v;
for (int i=0;i<n;++i)
{
cin>>l[i]>>r[i];
v.pb({{r[i],l[i]},i});
}
sort(all(v));
for (int i=0;i<n;++i) seg[i+M]={v[i].x.y,v[i].y};
for (int i=M-1;i;--i) seg[i]=min(seg[i*2],seg[i*2+1]);
for (int i=0;i<n;++i)
{
int poc=lower_bound(all(v),pair<pii,int>(pii(l[i],0),0))-v.begin();
int kr=lower_bound(all(v),pair<pii,int>(pii(r[i],MOD),MOD))-v.begin();
pii re=ge(poc,kr);
par[0][i]=re.y;
//cout<<i<<' '<<re.y<<en;
}
for (int j=0;j<17;++j)
{
for (int i=0;i<n;++i) par[j+1][i]=par[j][par[j][i]];
}
while (q--)
{
int a,b;
cin>>a>>b;
--a;
--b;
if (a==b)
{
cout<<0<<en;
continue;
}
if (r[a]>r[b])
{
cout<<"impossible\n";
continue;
}
int an=0;
for (int j=17;j>=0;--j) if (l[par[j][b]]>r[a]) an+=1<<j,b=par[j][b];
if (l[b]>r[a]) ++an,b=par[0][b];
if (l[b]>r[a])
{
cout<<"impossible\n";
continue;
}
cout<<an+1<<en;
}
}
# | 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... |