Submission #584255

#TimeUsernameProblemLanguageResultExecution timeMemory
584255alirezasamimi100Event Hopping (BOI22_events)C++17
100 / 100
174 ms16516 KiB
/*#pragma GCC optimize("Ofast,unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
#include <bits/stdc++.h>
using namespace std;
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using point=complex<double>;
#define F first
#define S second
//#define X real()
//#define Y imag()
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define kill(x) cout << x << '\n';exit(0)
#define killshayan kill("done!")
#define killmashtali kill("Hello, World!")
const int N=2e5+10,LN=20,M=1e6+10,SQ=700,BS=737,inf=1.05e9,NSQ=N/SQ+3;
const ll INF=1e18;
const double pi=acos(-1);
const ld ep=1e-7;
const int MH=1000696969,MD=998244353,MOD=1000000007;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pii, null_type,greater<pii>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
    ll ans=1;
    while (y != 0) {
        if (y & 1) ans = ans * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return ans;
}
int n,q,dp[LN][N],L[N],R[N],k;
pii f[N*4];
vector<int> cp;
void build(int v, int l, int r){
    f[v]={inf,inf};
    if(r-l>1){
        int m=(l+r)>>1;
        build(lc,l,m);
        build(rc,m,r);
    }
}
pii get(int v, int l, int r, int tl, int tr){
    if(l>=tr || tl>=r) return {inf,inf};
    if(l>=tl && r<=tr) return f[v];
    int m=(l+r)>>1;
    return min(get(lc,l,m,tl,tr),get(rc,m,r,tl,tr));
}
void upd(int v, int l, int r, int p, pii x){
    if(r-l==1){
        f[v]=min(f[v],x);
        return;
    }
    int m=(l+r)>>1;
    if(p<m) upd(lc,l,m,p,x);
    else upd(rc,m,r,p,x);
    f[v]=min(f[lc],f[rc]);
}
int main(){
    fast_io;
    cin >> n >> q;
    for(int i=1; i<=n; i++){
        cin >> L[i] >> R[i];
        cp.pb(L[i]);
        cp.pb(R[i]);
    }
    sort(cp.begin(),cp.end());
    cp.resize(unique(cp.begin(),cp.end())-cp.begin());
    k=cp.size();
    build(1,1,k+1);
    for(int i=1; i<=n; i++){
        L[i]=lower_bound(cp.begin(),cp.end(),L[i])-cp.begin()+1;
        R[i]=lower_bound(cp.begin(),cp.end(),R[i])-cp.begin()+1;
        upd(1,1,k+1,R[i],{L[i],i});
    }
    for(int i=1; i<=n; i++){
        dp[0][i]=get(1,1,k+1,L[i],R[i]+1).S;
    }
    for(int i=1; i<LN; i++){
        for(int j=1; j<=n; j++){
            dp[i][j]=dp[i-1][dp[i-1][j]];
        }
    }
    for(int i=1; i<=q; i++){
        int s,e,ans=0;
        cin >> s >> e;
        if(s==e){
            cout << 0 << '\n';
            continue;
        }
        if(R[s]>=L[e] && R[s]<=R[e]){
            cout << 1 << '\n';
            continue;
        }
        if(R[s]>R[e]){
            cout << "impossible\n";
            continue;
        }
        for(int j=LN; j--; ){
            if(L[dp[j][e]]>R[s]){
                ans+=(1<<j);
                e=dp[j][e];
            }
        }
        if(ans>n) cout << "impossible\n";
        else cout << ans+2 << '\n';
    }
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...