Submission #678789

# Submission time Handle Problem Language Result Execution time Memory
678789 2023-01-06T15:04:49 Z guagua0407 Railway Trip 2 (JOI22_ho_t4) C++17
16 / 100
11 ms 8804 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end() 
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);


struct node{
	int mx=-1, mn=1e5+5;
	int stmx=-1,stmn=1e5+5;
};

const int mxn=1e5+5;
pair<int,int> jump[17][mxn];
int n,k,m;
int up[17][mxn][17],down[17][mxn][17];
node segtree[1][mxn];
int lg[mxn];

void push(int id,int v){
	segtree[id][v*2].mn=min(segtree[id][v*2].mn,segtree[id][v].stmn);
	segtree[id][v*2+1].mn=min(segtree[id][v*2+1].mn,segtree[id][v].stmn);
	segtree[id][v*2].mx=max(segtree[id][v*2].mx,segtree[id][v].stmx);
	segtree[id][v*2+1].mx=max(segtree[id][v*2+1].mx,segtree[id][v].stmx);
	segtree[id][v*2].stmn=min(segtree[id][v*2].stmn,segtree[id][v].stmn);
	segtree[id][v*2+1].stmn=min(segtree[id][v*2+1].stmn,segtree[id][v].stmn);
	segtree[id][v*2].stmx=max(segtree[id][v*2].stmx,segtree[id][v].stmx);
	segtree[id][v*2+1].stmx=max(segtree[id][v*2+1].stmx,segtree[id][v].stmx);
	segtree[id][v].stmn=1e5+5;
	segtree[id][v].stmx=-1;
}

void init(int id,int l=1,int r=n,int v=1){
	segtree[id][v].mn=l,segtree[id][v].mx=r;
	if(l==r){
		return;
	}
	int mid=(l+r)/2;
	init(id,l,mid,v*2);
	init(id,mid+1,r,v*2+1);
}

void update1(int id,int tl,int tr,int val,int l=1,int r=n,int v=1){
	if(tl>tr){
		return;
	}
	if(tl<=l and r<=tr){
		segtree[id][v].stmx=max(segtree[id][v].stmx,val);
		segtree[id][v].mx=max(segtree[id][v].mx,val);
		return;
	}
	push(id,v);
	int mid=(l+r)/2;
	update1(id,tl,min(mid,tr),val,l,mid,v*2);
	update1(id,max(mid+1,tl),tr,val,mid+1,r,v*2+1);
	segtree[id][v].mx=max(segtree[id][v*2].mx,segtree[id][v*2+1].mx);
	segtree[id][v].mn=min(segtree[id][v*2].mn,segtree[id][v*2+1].mn);
}

void update2(int id,int tl,int tr,int val,int l=1,int r=n,int v=1){
	if(tl>tr){
		return;
	}
	if(tl<=l and r<=tr){
		segtree[id][v].stmn=min(segtree[id][v].stmn,val);
		segtree[id][v].mn=min(segtree[id][v].mn,val);
		return;
	}
	push(id,v);
	int mid=(l+r)/2;
	update2(id,tl,min(mid,tr),val,l,mid,v*2);
	update2(id,max(mid+1,tl),tr,val,mid+1,r,v*2+1);
	segtree[id][v].mn=min(segtree[id][v*2].mn,segtree[id][v*2+1].mn);
	segtree[id][v].mx=max(segtree[id][v*2].mx,segtree[id][v*2+1].mx);
}

pair<int,int> query(int id,int tl,int tr,int l=1,int r=n,int v=1){
	if(tl>tr){
		return {n+1,-1};
	}
	if(tl<=l and r<=tr){
		return {segtree[id][v].mn,segtree[id][v].mx};
	}
	push(id,v);
	int mid=(l+r)/2;
	pair<int,int> x=query(id,tl,min(mid,tr),l,mid,v*2);
	pair<int,int> y=query(id,max(mid+1,tl),tr,mid+1,r,v*2+1);
	return {min(x.f,y.f),max(x.s,y.s)};
}

bool ok(int a,int b,int k){
	pair<int,int> cur={a,a};
	for(int i=0;i<17;i++){
		if(k&(1<<i)){
            int bit=lg[cur.s-cur.f+1];
            int x=min(down[i][cur.f][bit],down[i][cur.s-(1<<bit)+1][bit]);
            int y=max(up[i][cur.f][bit],up[i][cur.s-(1<<bit)+1][bit]);
			cur.f=x;
            cur.s=y;
            //cout<<cur.f<<' '<<cur.s<<'\n';
		}
	}
	return (cur.f<=b and b<=cur.s);
}

int main() {_
	cin>>n>>k;
	cin>>m;
	init(0);
    for(int i=2;i<=n;i++){
        lg[i]=lg[i/2]+1;
    }
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		if(a<b){
			int tmp=min(a+k-1,b);
			update1(0,a,tmp,b);
		}
		else{
			int tmp=max(a-k+1,b);
			update2(0,tmp,a,b);
		}
	}
    for(int i=1;i<=n;i++){
        up[0][i][0]=query(0,i,i).s;
        down[0][i][0]=query(0,i,i).f;
        //cout<<i<<' '<<down[0][i][0]<<' '<<up[0][i][0]<<'\n';
    }
    for(int i=1;i<17;i++){
        for(int j=1;j+(1<<i)-1<=n;j++){
            up[0][j][i]=max(up[0][j][i-1],up[0][j+(1<<(i-1))][i-1]);
            down[0][j][i]=min(down[0][j][i-1],down[0][j+(1<<(i-1))][i-1]);
        }
    }
	for(int bit=1;bit<17;bit++){
        for(int i=1;i<=n;i++){
            int tmp=lg[up[bit-1][i][0]-down[bit-1][i][0]+1];
            up[bit][i][0]=max(up[bit-1][down[bit-1][i][0]][tmp],up[bit-1][up[bit-1][i][0]-(1<<tmp)+1][tmp]);
            down[bit][i][0]=min(down[bit-1][down[bit-1][i][0]][tmp],down[bit-1][up[bit-1][i][0]-(1<<tmp)+1][tmp]);
        }
        for(int i=1;i<17;i++){
            for(int j=1;j+(1<<i)-1<=n;j++){
                up[bit][j][i]=max(up[bit][j][i-1],up[bit][j+(1<<(i-1))][i-1]);
                down[bit][j][i]=min(down[bit][j][i-1],down[bit][j+(1<<(i-1))][i-1]);
            }
        }
    }
	int q;
	cin>>q;
	for(int i=0;i<q;i++){
		int a,b;
		cin>>a>>b;
		//binary search ans
		int l=0,r=n+1;
		while(l<r){
			int mid=(l+r)/2;
			if(ok(a,b,mid)){
				r=mid;
			}
			else{
				l=mid+1;
			}
		}
		cout<<(l==n+1?-1:l)<<'\n';
	}
return 0;
}
//maybe its multiset not set
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 1 ms 1236 KB Output is correct
4 Correct 1 ms 1236 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 2 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1236 KB Output is correct
9 Correct 1 ms 1236 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 1236 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 1 ms 1236 KB Output is correct
4 Correct 1 ms 1236 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 2 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1236 KB Output is correct
9 Correct 1 ms 1236 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 1236 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
13 Correct 7 ms 5204 KB Output is correct
14 Correct 6 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 7 ms 5188 KB Output is correct
17 Correct 7 ms 5248 KB Output is correct
18 Correct 7 ms 5204 KB Output is correct
19 Correct 6 ms 4948 KB Output is correct
20 Correct 7 ms 5204 KB Output is correct
21 Correct 5 ms 5204 KB Output is correct
22 Correct 7 ms 5204 KB Output is correct
23 Correct 7 ms 5204 KB Output is correct
24 Correct 6 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 8788 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 8788 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 8804 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1236 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 1 ms 1236 KB Output is correct
4 Correct 1 ms 1236 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 2 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1236 KB Output is correct
9 Correct 1 ms 1236 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 1236 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
13 Correct 7 ms 5204 KB Output is correct
14 Correct 6 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 7 ms 5188 KB Output is correct
17 Correct 7 ms 5248 KB Output is correct
18 Correct 7 ms 5204 KB Output is correct
19 Correct 6 ms 4948 KB Output is correct
20 Correct 7 ms 5204 KB Output is correct
21 Correct 5 ms 5204 KB Output is correct
22 Correct 7 ms 5204 KB Output is correct
23 Correct 7 ms 5204 KB Output is correct
24 Correct 6 ms 5204 KB Output is correct
25 Runtime error 11 ms 8788 KB Execution killed with signal 11
26 Halted 0 ms 0 KB -