Submission #902031

# Submission time Handle Problem Language Result Execution time Memory
902031 2024-01-10T05:32:34 Z LCJLY Railway Trip 2 (JOI22_ho_t4) C++14
100 / 100
1757 ms 296532 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<long long,long long>pii;

inline pii combine(const pii a,const pii b){
	return {min(a.first,b.first),max(a.second,b.second)};
}

struct node{
	int s,e,m;
	node *l,*r;
	pii v; //mini maxi
	int lazyMin,lazyMax;
	
	node(int ss, int ee):s(ss),e(ee),m((s+e)>>1){
		v={s,e};
		lazyMin=INT_MAX;
		lazyMax=INT_MIN;
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
			v=combine(l->v,r->v);
		}
	}
	
	void self_min(int x){
		v.first=min(v.first,x);
		lazyMin=min(lazyMin,x);
	}
	
	void self_max(int x){
		v.second=max(v.second,x);
		lazyMax=max(lazyMax,x);
	}
	
	void forceProp(){
		if(s==e) return;
		if(lazyMin!=INT_MAX){
			l->self_min(lazyMin); r->self_min(lazyMin);
			lazyMin=INT_MAX;
		}
		if(lazyMax!=INT_MIN){
			l->self_max(lazyMax); r->self_max(lazyMax);
			lazyMax=INT_MIN;
		}
	}
	
	void rangeUpd(int x, int y, pii c){
		if(x<=s&&y>=e){
			self_min(c.first);
			self_max(c.second);
			return;
		}
		forceProp();
		if(x<=m)l->rangeUpd(x,y,c);
		if(y>m)r->rangeUpd(x,y,c);
		v=combine(l->v,r->v);
	}
	
	pii query(int x, int y){
		if(x<=s&&y>=e){
			return v;
		}
		forceProp();
		if(y<=m)return l->query(x,y);
		if(x>m)return r->query(x,y);
		return combine(l->query(x,m),r->query(m+1,y));
	}
}*st[17];

void solve(){	
	int n,k;
	cin >> n >> k;
	int m;
	cin >> m;
	
	for(int x=0;x<17;x++){
		st[x]=new node(0,n+5);
	}
	
	int temp,temp2;
	for(int x=0;x<m;x++){
		cin >> temp >> temp2;
		if(temp<temp2){
			int hold=min(temp+k-1,temp2);
			st[0]->rangeUpd(temp,hold,{temp2,temp2});
		}
		else{
			int hold=max(temp-k+1,temp2);
			st[0]->rangeUpd(hold,temp,{temp2,temp2});
		}
	}
	
	pii two[17][n+5];
	for(int bit=0;bit<17;bit++){
		for(int x=0;x<=n;x++){
			if(bit==0){
				two[bit][x]=st[bit]->query(x,x);
			}
			else{
				two[bit][x]=st[bit-1]->query(two[bit-1][x].first,two[bit-1][x].second);
				st[bit]->rangeUpd(x,x,two[bit][x]);
				//show2(bit,bit,x,x);
				//show2(l,two[bit][x].first,r,two[bit][x].second);
				//show2(prev,two[bit-1][x].first,prev,two[bit-1][x].second);
			}
		}
	}
	
	int q;
	cin >> q;
	
	for(int x=0;x<q;x++){
		cin >> temp >> temp2;
		
		if(temp<temp2){
			int l=temp;
			int r=temp;
			
			int ans=0;
			for(int bit=16;bit>=0;bit--){
				pii hold=st[bit]->query(l,r);
				if(hold.second<temp2){
					ans+=1<<bit;
					l=hold.first;
					r=hold.second;
				}
			}
			
			if(st[0]->query(l,r).second<temp2){
				cout << -1 << "\n";
			}
			else{
				cout << ans+1 << "\n";
			}
		}
		else{
			int l=temp;
			int r=temp;
			
			int ans=0;
			for(int bit=16;bit>=0;bit--){
				pii hold=st[bit]->query(l,r);
				if(hold.first>temp2){
					ans+=1<<bit;
					l=hold.first;
					r=hold.second;
				}
			}
			
			if(st[0]->query(l,r).first>temp2){
				cout << -1 << "\n";
			}
			else{
				cout << ans+1 << "\n";
			}
		}
	}
}	
 
int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}


 
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1116 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
3 Correct 2 ms 1116 KB Output is correct
4 Correct 3 ms 1116 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1116 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 2 ms 1116 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 1116 KB Output is correct
12 Correct 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1116 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
3 Correct 2 ms 1116 KB Output is correct
4 Correct 3 ms 1116 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1116 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 2 ms 1116 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 1116 KB Output is correct
12 Correct 2 ms 1116 KB Output is correct
13 Correct 16 ms 6236 KB Output is correct
14 Correct 14 ms 6292 KB Output is correct
15 Correct 10 ms 6236 KB Output is correct
16 Correct 15 ms 6236 KB Output is correct
17 Correct 14 ms 6236 KB Output is correct
18 Correct 12 ms 6236 KB Output is correct
19 Correct 12 ms 5980 KB Output is correct
20 Correct 13 ms 6236 KB Output is correct
21 Correct 11 ms 6328 KB Output is correct
22 Correct 13 ms 6236 KB Output is correct
23 Correct 16 ms 6492 KB Output is correct
24 Correct 12 ms 6236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 767 ms 293120 KB Output is correct
2 Correct 697 ms 293200 KB Output is correct
3 Correct 783 ms 293216 KB Output is correct
4 Correct 684 ms 293168 KB Output is correct
5 Correct 860 ms 293208 KB Output is correct
6 Correct 690 ms 293456 KB Output is correct
7 Correct 790 ms 293200 KB Output is correct
8 Correct 762 ms 290384 KB Output is correct
9 Correct 783 ms 293204 KB Output is correct
10 Correct 722 ms 293200 KB Output is correct
11 Correct 748 ms 293212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1231 ms 293400 KB Output is correct
2 Correct 992 ms 293200 KB Output is correct
3 Correct 1222 ms 293712 KB Output is correct
4 Correct 997 ms 293452 KB Output is correct
5 Correct 1006 ms 290352 KB Output is correct
6 Correct 903 ms 293392 KB Output is correct
7 Correct 1040 ms 293456 KB Output is correct
8 Correct 1335 ms 293200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1509 ms 293204 KB Output is correct
2 Correct 1636 ms 293444 KB Output is correct
3 Correct 1509 ms 295008 KB Output is correct
4 Correct 1334 ms 294212 KB Output is correct
5 Correct 1089 ms 293972 KB Output is correct
6 Correct 969 ms 293984 KB Output is correct
7 Correct 907 ms 295420 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 12 ms 6240 KB Output is correct
10 Correct 820 ms 295508 KB Output is correct
11 Correct 929 ms 296020 KB Output is correct
12 Correct 927 ms 292992 KB Output is correct
13 Correct 968 ms 296232 KB Output is correct
14 Correct 2 ms 1116 KB Output is correct
15 Correct 14 ms 6236 KB Output is correct
16 Correct 700 ms 295668 KB Output is correct
17 Correct 1421 ms 296512 KB Output is correct
18 Correct 1383 ms 296224 KB Output is correct
19 Correct 1230 ms 296000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1116 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
3 Correct 2 ms 1116 KB Output is correct
4 Correct 3 ms 1116 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1116 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 2 ms 1116 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 1116 KB Output is correct
12 Correct 2 ms 1116 KB Output is correct
13 Correct 16 ms 6236 KB Output is correct
14 Correct 14 ms 6292 KB Output is correct
15 Correct 10 ms 6236 KB Output is correct
16 Correct 15 ms 6236 KB Output is correct
17 Correct 14 ms 6236 KB Output is correct
18 Correct 12 ms 6236 KB Output is correct
19 Correct 12 ms 5980 KB Output is correct
20 Correct 13 ms 6236 KB Output is correct
21 Correct 11 ms 6328 KB Output is correct
22 Correct 13 ms 6236 KB Output is correct
23 Correct 16 ms 6492 KB Output is correct
24 Correct 12 ms 6236 KB Output is correct
25 Correct 767 ms 293120 KB Output is correct
26 Correct 697 ms 293200 KB Output is correct
27 Correct 783 ms 293216 KB Output is correct
28 Correct 684 ms 293168 KB Output is correct
29 Correct 860 ms 293208 KB Output is correct
30 Correct 690 ms 293456 KB Output is correct
31 Correct 790 ms 293200 KB Output is correct
32 Correct 762 ms 290384 KB Output is correct
33 Correct 783 ms 293204 KB Output is correct
34 Correct 722 ms 293200 KB Output is correct
35 Correct 748 ms 293212 KB Output is correct
36 Correct 1231 ms 293400 KB Output is correct
37 Correct 992 ms 293200 KB Output is correct
38 Correct 1222 ms 293712 KB Output is correct
39 Correct 997 ms 293452 KB Output is correct
40 Correct 1006 ms 290352 KB Output is correct
41 Correct 903 ms 293392 KB Output is correct
42 Correct 1040 ms 293456 KB Output is correct
43 Correct 1335 ms 293200 KB Output is correct
44 Correct 1509 ms 293204 KB Output is correct
45 Correct 1636 ms 293444 KB Output is correct
46 Correct 1509 ms 295008 KB Output is correct
47 Correct 1334 ms 294212 KB Output is correct
48 Correct 1089 ms 293972 KB Output is correct
49 Correct 969 ms 293984 KB Output is correct
50 Correct 907 ms 295420 KB Output is correct
51 Correct 2 ms 1116 KB Output is correct
52 Correct 12 ms 6240 KB Output is correct
53 Correct 820 ms 295508 KB Output is correct
54 Correct 929 ms 296020 KB Output is correct
55 Correct 927 ms 292992 KB Output is correct
56 Correct 968 ms 296232 KB Output is correct
57 Correct 2 ms 1116 KB Output is correct
58 Correct 14 ms 6236 KB Output is correct
59 Correct 700 ms 295668 KB Output is correct
60 Correct 1421 ms 296512 KB Output is correct
61 Correct 1383 ms 296224 KB Output is correct
62 Correct 1230 ms 296000 KB Output is correct
63 Correct 1553 ms 294656 KB Output is correct
64 Correct 1757 ms 295072 KB Output is correct
65 Correct 1573 ms 294980 KB Output is correct
66 Correct 1083 ms 296212 KB Output is correct
67 Correct 1362 ms 296532 KB Output is correct
68 Correct 1148 ms 292992 KB Output is correct
69 Correct 1010 ms 296324 KB Output is correct
70 Correct 1170 ms 296260 KB Output is correct
71 Correct 1555 ms 296284 KB Output is correct