Submission #1365998

#TimeUsernameProblemLanguageResultExecution timeMemory
1365998vjudge1Railway Trip 2 (JOI22_ho_t4)C++17
100 / 100
381 ms64320 KiB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <queue>
#ifdef _WIN32
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#else
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define pll pair<ll,ll>
#define pld pair<ld,ld>
typedef int ll;
typedef long double ld;
typedef __int128 i128;
namespace io {
    using namespace std;
    template <typename T> void debug (T x) {
        cerr<<x<<'\n';
    }
    template <typename T> void debuglen (T x) {
        cerr<<x<<' ';
    }
    template <typename T,typename...Args> void debug (T x,Args...args) {
        cerr<<x<<' ';
        debug(args...);
    }
    template <typename T> void debug (T*lt,T*rt) {
        ll len=rt-lt;
        for (ll i=0;i<len;i++) {
            debuglen(*(lt+i));
        }
        cerr<<'\n';
    }
    inline ll read () {
        char x=getchar();
        ll ans=0,f=1;
        while (x<'0'||x>'9') {
            if (x=='-') {
                f=-1;
            }
            x=getchar();
        }
        while (x>='0'&&x<='9') {
            ans=(ans<<1)+(ans<<3);
            ans+=(x^'0');
            x=getchar();
        }
        return ans*f;
    }
    void print (ll x) {
        if (x<0) {
            x=-x;
            putchar('-');
        }
        if (x>=10) {
            print(x/10);
        }
        putchar(x%10+'0');
    }
}
using namespace io;
const ll N=1e5+5,mod=1e9+7,inf=2e9;
const ld eps=1e-6;
ll n,k,m,dis[N],f[N][21],g[N][21];
struct Segtree {
    ll t[N<<2],tl[N<<2],lt[N<<2],ltt[N<<2];
    ll st[N<<2][21],stt[N<<2][21];
    inline void push_up (ll pos) {
        t[pos]=max(t[pos<<1],t[pos<<1|1]);
        tl[pos]=min(tl[pos<<1],tl[pos<<1|1]);
    }
    inline void push_down (ll pos) {
        t[pos<<1]=max(t[pos<<1],lt[pos]);
        t[pos<<1|1]=max(t[pos<<1|1],lt[pos]);
        lt[pos<<1]=max(lt[pos<<1],lt[pos]);
        lt[pos<<1|1]=max(lt[pos<<1|1],lt[pos]);
        lt[pos]=-inf;
        tl[pos<<1]=min(tl[pos<<1],ltt[pos]);
        tl[pos<<1|1]=min(tl[pos<<1|1],ltt[pos]);
        ltt[pos<<1]=min(ltt[pos<<1],ltt[pos]);
        ltt[pos<<1|1]=min(ltt[pos<<1|1],ltt[pos]);
        ltt[pos]=inf;
    }
    void build (ll pos,ll op,ll l,ll r) {
        lt[pos]=-inf,ltt[pos]=inf;
        if (l==r) {
            t[pos]=f[l][op],tl[pos]=g[l][op];
            return ;
        }
        ll mid=(l+r)>>1;
        build(pos<<1,op,l,mid);
        build(pos<<1|1,op,mid+1,r);
        push_up(pos);
    }
    void add (ll pos,ll l,ll r,ll L,ll R,ll val) {
        if (L<=l&&r<=R) {
            t[pos]=max(t[pos],val);
            tl[pos]=min(tl[pos],val);
            lt[pos]=max(lt[pos],val);
            ltt[pos]=min(ltt[pos],val);
            return ;
        }
        push_down(pos);
        ll mid=(l+r)>>1;
        if (L<=mid) {
            add(pos<<1,l,mid,L,R,val);
        }
        if (R>mid) {
            add(pos<<1|1,mid+1,r,L,R,val);
        }
        push_up(pos);
    }
    void query (ll pos,ll l,ll r) {
        if (l==r) {
            f[l][0]=t[pos],g[l][0]=tl[pos];
            return ;
        }
        push_down(pos);
        ll mid=(l+r)>>1;
        query(pos<<1,l,mid);
        query(pos<<1|1,mid+1,r);
    }
    pll pos_find (ll pos,ll l,ll r,ll L,ll R) {
        if (L<=l&&r<=R) {
            return {t[pos],tl[pos]};
        }
        ll mid=(l+r)>>1;
        pll cnt={-inf,inf};
        if (L<=mid) {
            cnt=pos_find(pos<<1,l,mid,L,R);
        }
        if (R>mid) {
            pll pl=pos_find(pos<<1|1,mid+1,r,L,R);
            cnt={max(cnt.first,pl.first),min(cnt.second,pl.second)};
        }
        return cnt;
    }
    inline void pushup (ll pos,ll op) {
        st[pos][op]=max(st[pos<<1][op],st[pos<<1|1][op]);
        stt[pos][op]=min(stt[pos<<1][op],stt[pos<<1|1][op]);
    }
    void buildl (ll pos,ll op,ll l,ll r) {
        if (l==r) {
            st[pos][op]=f[l][op];
            stt[pos][op]=g[l][op];
            return ;
        }
        ll mid=(l+r)>>1;
        buildl(pos<<1,op,l,mid);
        buildl(pos<<1|1,op,mid+1,r);
        pushup(pos,op);
    }
    pll num_find (ll pos,ll op,ll l,ll r,ll L,ll R) {
        if (L<=l&&r<=R) {
            return {st[pos][op],stt[pos][op]};
        }
        ll mid=(l+r)>>1;
        pll cnt={-inf,inf};
        if (L<=mid) {
            cnt=num_find(pos<<1,op,l,mid,L,R);
        }
        if (R>mid) {
            pll pl=num_find(pos<<1|1,op,mid+1,r,L,R);
            cnt={max(cnt.first,pl.first),min(cnt.second,pl.second)};
        }
        return cnt;
    }
} tr;
inline void solve () {
    n=read(),k=read(),m=read();
    for (ll i=1;i<=n;i++) {
        f[i][0]=g[i][0]=i;
    }
    tr.build(1,0,1,n);
    for (ll i=1;i<=m;i++) {
        ll x=read(),y=read();
        if (x>y) {
            ll pos=max(y+1,x-k+1);
            tr.add(1,1,n,pos,x,y);
        }
        else {
            ll pos=min(x+k-1,y-1);
            tr.add(1,1,n,x,pos,y);
        }
    }
    tr.query(1,1,n);
    for (ll j=1;j<=20;j++) {
        tr.build(1,j-1,1,n);
        for (ll i=1;i<=n;i++) {
            pll pl=tr.pos_find(1,1,n,g[i][j-1],f[i][j-1]);
            f[i][j]=pl.first;
            g[i][j]=pl.second;
        }
    }
    for (ll i=0;i<=20;i++) {
        tr.buildl(1,i,1,n);
    }
    ll q=read();
    while (q--) {
        ll x=read(),y=read();
        if (y<g[x][20]||y>f[x][20]) {
            puts("-1");
            continue;
        }
        ll l=x,r=x,ans=0;
        for (ll i=20;i>=0;i--) {
            pll pl=tr.num_find(1,i,1,n,l,r);
            ll xl=pl.first,yl=pl.second;
            if (yl<=y&&y<=xl) {
                continue;
            }
            ans+=(1<<i);
            l=yl,r=xl;
        }
        print(ans+1);
        puts("");
    }
}
int main () {
    // freopen("railway.in","r",stdin);
    // freopen("railway.out","w",stdout);
    ll T=1;
    // T=read();
    while (T--) {
        solve();
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...