Submission #1330979

#TimeUsernameProblemLanguageResultExecution timeMemory
1330979JuanJLRailway Trip 2 (JOI22_ho_t4)C++20
11 / 100
2035 ms145580 KiB
#include <bits/stdc++.h>

#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
typedef long long ll;

const int MAXN = 200000+5;
const int MAXP = 20;

#ifdef D
    #define debug(x) cout << x
#else 
    #define debug(x) //nothing
#endif

struct STreeMin{
    ll n;
    vector<ll> st;
    STreeMin(ll n=0):n(n), st(4*n+5,1000000000){}
    void upd(ll k, ll l, ll r, ll p, ll v){
        if(l+1==r){ st[k]=v; return; }
        ll mid = (l+r)/2;
        if(mid>p) upd(2*k,l,mid,p,v);
        else upd(2*k+1,mid,r,p,v);
        st[k]=min(st[2*k],st[2*k+1]);
    }
    ll query(ll k, ll l, ll r, ll L, ll R){
        if(l>=R || r<=L) return 1000000000;
        if(l>=L && r<=R) return st[k];
        ll mid = (l+r)/2;
        return min(query(2*k,l,mid,L,R),query(2*k+1,mid,r,L,R));
    }
    void upd(ll p, ll v){ upd(1,0,n,p,v); }
    ll query(ll l, ll r){ return query(1,0,n,l,r); }
};

struct STreeMax{
    ll n;
    vector<ll> st;
    STreeMax(ll n=0):n(n), st(4*n+5,(ll)0){}
    void upd(ll k, ll l, ll r, ll p, ll v){
        if(l+1==r){ st[k]=v; return; }
        ll mid = (l+r)/2;
        if(mid>p) upd(2*k,l,mid,p,v);
        else upd(2*k+1,mid,r,p,v);
        st[k]=max(st[2*k],st[2*k+1]);
    }
    ll query(ll k, ll l, ll r, ll L, ll R){
        if(l>=R || r<=L) return (ll)0;
        if(l>=L && r<=R) return st[k];
        ll mid = (l+r)/2;
        return max(query(2*k,l,mid,L,R),query(2*k+1,mid,r,L,R));
    }
    void upd(ll p, ll v){ upd(1,0,n,p,v); }
    ll query(ll l, ll r){ return query(1,0,n,l,r); }
};

ll n,K;
ll m,q;

ll a[MAXN];
ll b[MAXN];
ll s[MAXN];
ll t[MAXN];

STreeMin lleft[MAXP];
STreeMax rright[MAXP];

void precalc(){

    forn(i,0,MAXP) rright[i]=STreeMax(n), lleft[i]=STreeMin(n);
    forn(k,0,MAXP){
        forn(i,0,n) lleft[k].upd(i,i), rright[k].upd(i,i);
    }

    set<pair<ll,ll>> inp;
    multiset<ll> disp;
    set<pair<ll,ll>> out;
    forn(i,0,m){
        if(a[i]<b[i]) inp.insert({a[i],b[i]});
    }

    
    forn(i,0,n){
        while(!inp.empty() && inp.begin()->fst<=i){
            disp.insert(inp.begin()->snd);
            out.insert({inp.begin()->fst+K, inp.begin()->snd});
            inp.erase(inp.begin());
        }

        rright[0].upd(i,i);
        if(!disp.empty()) rright[0].upd(i,*(--disp.end()));

        while(!out.empty() && out.begin()->fst<=i){
            disp.erase(disp.find(out.begin()->snd));
            out.erase(out.begin());
        }
    }


    inp.clear();
    disp.clear();
    out.clear();

    forn(i,0,m){
        if(a[i]>b[i]) inp.insert({a[i]*-1,b[i]*-1});
    }

    for(int i = n-1; i>=0; i--){

        debug("i: "<<i<<'\n');

        while(!inp.empty() && inp.begin()->fst*-1>=i){
            disp.insert(inp.begin()->snd*-1);
            debug("Ingreso "<<inp.begin()->fst*-1<<" "<<inp.begin()->snd*-1<<'\n');
            out.insert({(inp.begin()->fst*-1 - K)*-1 ,inp.begin()->snd});
            inp.erase(inp.begin());
        }


        lleft[0].upd(i,i);
        if(!disp.empty()) lleft[0].upd(i,*disp.begin());

        while(!out.empty() && out.begin()->fst*-1>=i){
            disp.erase(disp.find(out.begin()->snd*-1));
            debug("Sale "<<out.begin()->fst*-1+K<<" "<<out.begin()->snd*-1<<'\n');
            out.erase(out.begin());
        }
    }



    
    forn(k,1,MAXP){
        forn(i,0,n){
            debug("(Left) Para "<<i<<" "<<"paso "<<k<<" "<<"tomamos desde "<<lleft[k-1].query(i,i+1)<<" hasta "<< rright[k-1].query(i,i+1)+1<<'\n');
            rright[k].upd(i, rright[k-1].query(lleft[k-1].query(i,i+1), rright[k-1].query(i,i+1)+1));
            lleft[k].upd(i, lleft[k-1].query(lleft[k-1].query(i,i+1), rright[k-1].query(i,i+1)+1));
        }        
    }
    

    forn(i,0,n){
        debug("Desde i (Right): "<<i<<'\n');
        forn(k,0,MAXP){
            debug("( Paso "<<(1<<k)<<" ) "<<rright[k].query(i,i+1)<<"\n");
        } debug('\n');

        debug("Desde i (Left): "<<i<<'\n');
        forn(k,0,MAXP){
            debug("( Paso "<<(1<<k)<<" ) "<<lleft[k].query(i,i+1)<<"\n");
        } debug('\n');
    }
}

int main(){
    cin>>n>>K;  K--;
    cin>>m;
    forn(i,0,m) cin>>a[i]>>b[i], a[i]--, b[i]--;
    
    cin>>q;
    forn(i,0,q) cin>>s[i]>>t[i], s[i]--, t[i]--;

    precalc();

    forn(i,0,q){
        
            ll p = s[i];
            ll l = s[i];
            ll r = s[i];
            ll cnt = 0;
            for(int k = MAXP-1; k>=0; k--){
                ll nl = lleft[k].query(l,r+1);
                ll nr = rright[k].query(l,r+1);
                if(nl<=t[i] && t[i]<=nr) continue;
                l=nl;
                r=nr;
                cnt+=1<<k;
            }
            debug(l<<" "<<r<<" - "<<cnt<<'\n');
            ll nl = lleft[0].query(l,r+1);
            ll nr = rright[0].query(l,r+1);
            if(nl<=t[i] && t[i]<=nr) cout<<cnt+1<<'\n';
            else cout<<-1<<'\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...