This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC optimize("conserve-stack")
#include<bits/stdc++.h>
#define int  long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP     ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define mp make_pair
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
#define getchar_unlocked getchar
inline int scan()
{
    char c = getchar_unlocked();
    int x = 0;
    while (c < '0' || c > '9')
    {
        c = getchar_unlocked();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + c - '0';
        c = getchar_unlocked();
    }
    return x;
}
void phongbeo();
const int inf = 1e14;
const ll mod2 = 1e9 + 7;
const int  mod1 = 998244353;
const ll base=67;
int add(int x,int y)
{
    if(x+y>=mod2) x-=mod2;
    if(x+y<0)x+=mod2;
    return x+y;
}
struct icd
{
    long double a;
    int b;
};
struct ib
{
    int a;
    int b;
};
struct ic
{
    int a, b, c;
};
struct id
{
    int a, b, c, d;
};
struct ie
{
    int a, b, c, d, e;
};
ll n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int  i, s10, s12,k1,k2,k3,s11,t,lim,w,l,r,p ;
int kk;
int el = 19;
main()
{
    if(fopen(task".inp","r"))
    {
        fin(task);
        fou(task);
    }
    NHP
    /// cin>>s1;
    // modwwe
    phongbeo();
    // checktime
}
ib minn[18][200001];
int st[17][100001];
ib a[100001];
vector<int> v;
ib mer(ib a,ib b)
{
    if(a.a<=b.a) return a;
    return b;
}
int getmin(int l,int r)
{
    int k=31-__builtin_clz(r-l+1);
    return mer(minn[k][l],minn[k][r-mask(k)+1]).b;
}
void phongbeo()
{
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>a[i].a>>a[i].b,v.pb(a[i].b),v.pb(a[i].a);
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    m=v.size();
    for(int i=1; i<=n; i++)
    {
        a[i].a=lower_bound(v.begin(),v.end(),a[i].a)-v.begin()+1;
        a[i].b=lower_bound(v.begin(),v.end(),a[i].b)-v.begin()+1;
    }
    for(int i=1; i<=m; i++)
        minn[0][i]= {m+1,i};
    for(int i=1; i<=n; i++)
    {
        if(a[i].a<=minn[0][a[i].b].a)
            minn[0][a[i].b]= {a[i].a,i};
    }
    for(int i=1; i<18; i++)
        for(int j=1; j+mask(i)-1<=m; j++)
            minn[i][j]=mer(minn[i-1][j],minn[i-1][j+mask(i-1)]);
    for(int i=1; i<=n; i++)
    {
        st[0][i]=getmin(a[i].a,a[i].b);
    }
    for(int i=1; i<17; i++)
        for(int j=1; j<=n; j++)
            st[i][j]=st[i-1][st[i-1][j]];
    while(k--)
    {
        down
        cin>>l>>r;
        if(a[l].b>a[r].b)
        {
            cout<<"impossible";
            continue;
        }
        else if(l==r)
        {
            cout<<0;
            continue;
        }
        else if(a[l].b>=a[r].a&&a[r].b>=a[l].b)
        {
            cout<<1;
            continue;
        }
        else
        {
            s4=0;
            for(int i=16; i>=0; --i)
                if(a[st[i][r]].a>a[l].b)
                {
                    s4=s4+(1<<i);
                    r=st[i][r];
                }
            if(s4==131071)
            {
                cout<<"impossible";
                continue;
            }
            cout<<s4+2;
        }
    }
}
Compilation message (stderr)
events.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   75 | main()
      | ^~~~
events.cpp: In function 'void phongbeo()':
events.cpp:124:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  124 |             minn[i][j]=mer(minn[i-1][j],minn[i-1][j+mask(i-1)]);
      |                                                          ~^~
events.cpp:16:21: note: in definition of macro 'mask'
   16 | #define mask(k) (1<<k)
      |                     ^
events.cpp: In function 'int main()':
events.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
events.cpp:79:9: note: in expansion of macro 'fin'
   79 |         fin(task);
      |         ^~~
events.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define fou(x) freopen(x".ans","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
events.cpp:80:9: note: in expansion of macro 'fou'
   80 |         fou(task);
      |         ^~~| # | 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... |