Submission #904932

# Submission time Handle Problem Language Result Execution time Memory
904932 2024-01-12T11:33:31 Z 089487 Railway Trip 2 (JOI22_ho_t4) C++14
100 / 100
1363 ms 510168 KB
#include<bits/stdc++.h>
#define int long long
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define lowbit(x) (x&-x)
#define sz(x) (int)(x.size())
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define mp make_pair
#define eb emplace_back
using namespace std;
typedef pair<int,int> pii;
void debug(){
    cout<<"\n";
}
template<class T,class ...U>
void debug(T a,U ...b){
    cout<<a<<" ",debug(b...);
}
const int N=2e5+7;
const int K=18;
const int INF=1e18;
pii dis[K][N];
pii ans[K][N*4];
pii tag[K][N*4];
pii p[N];
#define ll 2*x+1
#define rr 2*x+2
#define L ll,lx,mid
#define R rr,mid,rx
void Upd(pii &x,int val){
    x=mp(min(x.F,val),max(x.S,val));
}
void Upd(pii &x,pii val){
    x=mp(min(x.F,val.F),max(x.S,val.S));
}
void push(int i,int x,int lx,int rx){
    if(lx==rx-1||tag[i][x].F>tag[i][x].S){
        return ;
    }
    Upd(ans[i][ll],tag[i][x]);
    Upd(ans[i][rr],tag[i][x]);
    Upd(tag[i][ll],tag[i][x]);
    Upd(tag[i][rr],tag[i][x]);
    tag[i][x]=mp(INF,-INF);
}
void pull(int i,int x){
    ans[i][x]=mp(min(ans[i][ll].F,ans[i][rr].F),max(ans[i][ll].S,ans[i][rr].S));
}
void upd(int i,int x,int lx,int rx,int l,int r,int val){
    //debug("upd i,lx,rx,l,r,val",i,lx,rx,l,r,val);
    if(l<=lx&&rx<=r){
        Upd(ans[i][x],val);
        Upd(tag[i][x],val);
        return ;
    }
    if(l>=rx||lx>=r) return ;

    int mid=(lx+rx)>>1;
    push(i,x,lx,rx);
    upd(i,L,l,r,val);
    upd(i,R,l,r,val);
    pull(i,x);
    return ;
}
pii qry(int i,int x,int lx,int rx,int l,int r){
    if(l>r) return mp(INF,-INF);
    //debug("qry i lx,rx,l,r",i,lx,rx,l,r);
    if(l<=lx&&rx<=r) return ans[i][x];
    if(l>=rx||lx>=r) return mp(INF,-INF);
    int mid=(lx+rx)>>1;
    push(i,x,lx,rx);
    pii a=qry(i,L,l,r);
    pii b=qry(i,R,l,r);
    return mp(min(a.F,b.F),max(a.S,b.S));
}
void build(int i,int x,int lx,int rx){
    if(lx==rx-1){
        ans[i][x]=dis[i][lx];
        return ;
    }
    int mid=(lx+rx)>>1;
    build(i,L);
    build(i,R);
    pull(i,x);
}
signed main(){
	quick
    
    int n,k,m;
    cin>>n>>k>>m;
    rep(i,0,m-1) cin>>p[i].F>>p[i].S;
    fill(ans[0],ans[0]+N*4,mp(INF,-INF));
    fill(tag[0],tag[0]+N*4,mp(INF,-INF));
    rep(i,0,m-1){ 
        if(p[i].F<p[i].S){
            upd(0,0,1,n+1,p[i].F,min(p[i].F+k,p[i].S+1),p[i].S);
        }
        else{
            swap(p[i].F,p[i].S);
            //debug("upd",max(p[i].F,p[i].S-k+1),p[i].S+1,p[i].F);
            upd(0,0,1,n+1,max(p[i].F,p[i].S-k+1),p[i].S+1,p[i].F);
        }
    }
    //debug("ok");
    //return 0;
    rep(i,1,n){
        dis[0][i]=qry(0,0,1,n+1,i,i+1);
        Upd(dis[0][i],i);
    }
    build(0,0,1,n+1);
    rep(i,1,K-1){
        rep(j,1,n){
            dis[i][j]=qry(i-1,0,1,n+1,dis[i-1][j].F,dis[i-1][j].S+1);
            dis[i][j]=mp(min(dis[i][j].F,dis[i-1][j].F),max(dis[i][j].S,dis[i-1][j].S));
        }
        fill(ans[i],ans[i]+N*4,mp(INF,-INF));
        fill(tag[i],tag[i]+N*4,mp(INF,-INF));
        build(i,0,1,n+1);
    }
    /*rep(i,1,n){
        cout<<"("<<dis[0][i].F<<","<<dis[0][i].S<<")"<<" \n"[i==n];
    }*/
    int q;
    cin>>q;
    while(q--){
        int s,t;
        cin>>s>>t;
        if(s==t) {
            cout<<"0\n";
            continue;
        }
        pii now=mp(s,s);
        int step=0;
        repd(i,K-1,0){
            pii nxt=qry(i,0,1,n+1,now.F,now.S+1);
            //debug("now",now.F,now.S,"move",1<<i,nxt.F,nxt.S);
            if(nxt.F>t||nxt.S<t){
                //debug("do",now.F,now.S,"mv=>",nxt.F,nxt.S);
                step+=(1<<i);
                now=nxt;
            }
        }
        step+=1;
        now=qry(0,0,1,n+1,now.F,now.S+1);
        //debug("step",step,now.F,now.S);
        if(now.F>t||now.S<t) cout<<"-1\n";
        else cout<<step<<"\n";
        
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1363 ms 488896 KB Output is correct
2 Correct 111 ms 488668 KB Output is correct
3 Correct 106 ms 488784 KB Output is correct
4 Correct 97 ms 488600 KB Output is correct
5 Correct 96 ms 488740 KB Output is correct
6 Correct 95 ms 488692 KB Output is correct
7 Correct 97 ms 488640 KB Output is correct
8 Correct 95 ms 488744 KB Output is correct
9 Correct 94 ms 488788 KB Output is correct
10 Correct 95 ms 488784 KB Output is correct
11 Correct 97 ms 488708 KB Output is correct
12 Correct 96 ms 488784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1363 ms 488896 KB Output is correct
2 Correct 111 ms 488668 KB Output is correct
3 Correct 106 ms 488784 KB Output is correct
4 Correct 97 ms 488600 KB Output is correct
5 Correct 96 ms 488740 KB Output is correct
6 Correct 95 ms 488692 KB Output is correct
7 Correct 97 ms 488640 KB Output is correct
8 Correct 95 ms 488744 KB Output is correct
9 Correct 94 ms 488788 KB Output is correct
10 Correct 95 ms 488784 KB Output is correct
11 Correct 97 ms 488708 KB Output is correct
12 Correct 96 ms 488784 KB Output is correct
13 Correct 108 ms 488784 KB Output is correct
14 Correct 111 ms 488784 KB Output is correct
15 Correct 101 ms 488784 KB Output is correct
16 Correct 100 ms 488672 KB Output is correct
17 Correct 106 ms 488752 KB Output is correct
18 Correct 100 ms 488784 KB Output is correct
19 Correct 104 ms 488784 KB Output is correct
20 Correct 99 ms 488784 KB Output is correct
21 Correct 96 ms 488788 KB Output is correct
22 Correct 107 ms 488784 KB Output is correct
23 Correct 107 ms 488780 KB Output is correct
24 Correct 108 ms 488788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 635 ms 509416 KB Output is correct
2 Correct 611 ms 509604 KB Output is correct
3 Correct 674 ms 509448 KB Output is correct
4 Correct 565 ms 509356 KB Output is correct
5 Correct 372 ms 509296 KB Output is correct
6 Correct 618 ms 509352 KB Output is correct
7 Correct 267 ms 509308 KB Output is correct
8 Correct 248 ms 509344 KB Output is correct
9 Correct 250 ms 509360 KB Output is correct
10 Correct 666 ms 509524 KB Output is correct
11 Correct 578 ms 509264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 943 ms 510120 KB Output is correct
2 Correct 706 ms 509604 KB Output is correct
3 Correct 1045 ms 510168 KB Output is correct
4 Correct 520 ms 509856 KB Output is correct
5 Correct 560 ms 509532 KB Output is correct
6 Correct 539 ms 509780 KB Output is correct
7 Correct 925 ms 509780 KB Output is correct
8 Correct 912 ms 509848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1046 ms 509800 KB Output is correct
2 Correct 1292 ms 510028 KB Output is correct
3 Correct 1189 ms 509796 KB Output is correct
4 Correct 1210 ms 509764 KB Output is correct
5 Correct 1045 ms 507556 KB Output is correct
6 Correct 953 ms 507436 KB Output is correct
7 Correct 784 ms 509656 KB Output is correct
8 Correct 95 ms 488596 KB Output is correct
9 Correct 108 ms 488788 KB Output is correct
10 Correct 629 ms 509300 KB Output is correct
11 Correct 763 ms 509736 KB Output is correct
12 Correct 771 ms 509968 KB Output is correct
13 Correct 818 ms 509688 KB Output is correct
14 Correct 97 ms 488596 KB Output is correct
15 Correct 105 ms 488788 KB Output is correct
16 Correct 659 ms 509652 KB Output is correct
17 Correct 1165 ms 509732 KB Output is correct
18 Correct 1107 ms 509960 KB Output is correct
19 Correct 955 ms 509920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1363 ms 488896 KB Output is correct
2 Correct 111 ms 488668 KB Output is correct
3 Correct 106 ms 488784 KB Output is correct
4 Correct 97 ms 488600 KB Output is correct
5 Correct 96 ms 488740 KB Output is correct
6 Correct 95 ms 488692 KB Output is correct
7 Correct 97 ms 488640 KB Output is correct
8 Correct 95 ms 488744 KB Output is correct
9 Correct 94 ms 488788 KB Output is correct
10 Correct 95 ms 488784 KB Output is correct
11 Correct 97 ms 488708 KB Output is correct
12 Correct 96 ms 488784 KB Output is correct
13 Correct 108 ms 488784 KB Output is correct
14 Correct 111 ms 488784 KB Output is correct
15 Correct 101 ms 488784 KB Output is correct
16 Correct 100 ms 488672 KB Output is correct
17 Correct 106 ms 488752 KB Output is correct
18 Correct 100 ms 488784 KB Output is correct
19 Correct 104 ms 488784 KB Output is correct
20 Correct 99 ms 488784 KB Output is correct
21 Correct 96 ms 488788 KB Output is correct
22 Correct 107 ms 488784 KB Output is correct
23 Correct 107 ms 488780 KB Output is correct
24 Correct 108 ms 488788 KB Output is correct
25 Correct 635 ms 509416 KB Output is correct
26 Correct 611 ms 509604 KB Output is correct
27 Correct 674 ms 509448 KB Output is correct
28 Correct 565 ms 509356 KB Output is correct
29 Correct 372 ms 509296 KB Output is correct
30 Correct 618 ms 509352 KB Output is correct
31 Correct 267 ms 509308 KB Output is correct
32 Correct 248 ms 509344 KB Output is correct
33 Correct 250 ms 509360 KB Output is correct
34 Correct 666 ms 509524 KB Output is correct
35 Correct 578 ms 509264 KB Output is correct
36 Correct 943 ms 510120 KB Output is correct
37 Correct 706 ms 509604 KB Output is correct
38 Correct 1045 ms 510168 KB Output is correct
39 Correct 520 ms 509856 KB Output is correct
40 Correct 560 ms 509532 KB Output is correct
41 Correct 539 ms 509780 KB Output is correct
42 Correct 925 ms 509780 KB Output is correct
43 Correct 912 ms 509848 KB Output is correct
44 Correct 1046 ms 509800 KB Output is correct
45 Correct 1292 ms 510028 KB Output is correct
46 Correct 1189 ms 509796 KB Output is correct
47 Correct 1210 ms 509764 KB Output is correct
48 Correct 1045 ms 507556 KB Output is correct
49 Correct 953 ms 507436 KB Output is correct
50 Correct 784 ms 509656 KB Output is correct
51 Correct 95 ms 488596 KB Output is correct
52 Correct 108 ms 488788 KB Output is correct
53 Correct 629 ms 509300 KB Output is correct
54 Correct 763 ms 509736 KB Output is correct
55 Correct 771 ms 509968 KB Output is correct
56 Correct 818 ms 509688 KB Output is correct
57 Correct 97 ms 488596 KB Output is correct
58 Correct 105 ms 488788 KB Output is correct
59 Correct 659 ms 509652 KB Output is correct
60 Correct 1165 ms 509732 KB Output is correct
61 Correct 1107 ms 509960 KB Output is correct
62 Correct 955 ms 509920 KB Output is correct
63 Correct 1154 ms 509656 KB Output is correct
64 Correct 1259 ms 510036 KB Output is correct
65 Correct 1163 ms 509772 KB Output is correct
66 Correct 387 ms 509520 KB Output is correct
67 Correct 1000 ms 510008 KB Output is correct
68 Correct 807 ms 509788 KB Output is correct
69 Correct 520 ms 509524 KB Output is correct
70 Correct 913 ms 509636 KB Output is correct
71 Correct 1125 ms 509860 KB Output is correct