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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=1e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
struct node
{
ll val,id;
node()
{
val=inf,id=inf;
}
node operator+(const node&o)
{
node ans;
if(val<o.val) ans.val=val,ans.id=id;
else ans=o;
return ans;
}
}st[4*maxN];
ll n,q;
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
st[id].val=val;
st[id].id=l;
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
node get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return node();
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return get(i,j,id*2,l,mid)+get(i,j,id*2+1,mid+1,r);
}
struct qq
{
ll s,e,id;
bool operator<(const qq&o)
{
if(e==o.e) return s<o.s;
return e<o.e;
}
}a[maxN];
ll pos[maxN];
ll pre[maxN][19];
void solve()
{
cin >> n >> q;
for(int i=1;i<=n;i++)
{
cin >> a[i].s >> a[i].e;
a[i].id=i;
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
pos[a[i].id]=i;
ll low=1,high=i-1;
while(low<=high)
{
ll mid=low+high>>1;
if(a[mid].e<a[i].s) low=mid+1;
else high=mid-1;
}
auto val=get(low,i-1);
if(val.val==inf||val.val>=a[i].s) pre[i][0]=i;
else pre[i][0]=val.id;
update(i,a[i].s);
}
for(int j=1;j<=18;j++)
{
for(int i=1;i<=n;i++)
{
pre[i][j]=pre[pre[i][j-1]][j-1];
}
}
for(int i=1;i<=q;i++)
{
ll x,y;
cin >> x >> y;
x=pos[x];
y=pos[y];
if(x==y)
{
cout << 0<<'\n';
}
else if(a[x].e==a[y].e)
{
cout << 1<<'\n';
}
else if(x>y)
{
cout << "impossible\n";
}
else if(a[x].e<=a[y].e&&a[x].e>=a[y].s)
{
cout << 1 << '\n';
}
else
{
ll ans=0;
for(int j=18;j>=0;j--)
{
if(a[pre[y][j]].s>a[x].e)
{
y=pre[y][j],ans+=1<<j;
}
}
if(a[pre[y][0]].s<=a[x].e&&a[pre[y][0]].e>=a[x].e)
{
ans+=2;
cout << ans << '\n';
}
else
{
cout << "impossible\n";
}
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
events.cpp: In function 'void update(ll, ll, ll, ll, ll)':
events.cpp:37:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | ll mid=l+r>>1;
| ~^~
events.cpp: In function 'node get(ll, ll, ll, ll, ll)':
events.cpp:46:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | ll mid=l+r>>1;
| ~^~
events.cpp: In function 'void solve()':
events.cpp:75:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | ll mid=low+high>>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... |