답안 #574312

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
574312 2022-06-08T10:11:16 Z kshitij_sodani Event Hopping (BOI22_events) C++14
100 / 100
537 ms 37444 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
int aa[100001];
int bb[100001];
int par[100001][20];
int ans[100001];
pair<int,int> tree[100001*8];
vector<pair<int,int>> pre[100001];
void update(int no,int l,int r,int i,pair<int,int> j){
	if(l==r){
		tree[no]=min(tree[no],j);
	}
	else{
		int mid=(l+r)/2;
		if(i<=mid){
			update(no*2+1,l,mid,i,j);
		}
		else{
			update(no*2+2,mid+1,r,i,j);
		}
		tree[no]=min(tree[no*2+1],tree[no*2+2]);
	}
}
pair<int,int> query(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb){
		return {1e9,-1};
	}
	if(aa<=l and r<=bb){
		return tree[no];
	}
	int mid=(l+r)/2;
	return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,q;
	cin>>n>>q;
	for(int i=0;i<8*n;i++){
		tree[i]={1e9,-1};
	}

	vector<pair<int,pair<int,int>>> ss;
	map<int,int> tt;
	map<int,int> tt2;
	for(int i=0;i<n;i++){
		cin>>aa[i]>>bb[i];
		tt[aa[i]]++;
		tt[bb[i]]++;
		/*ss.pb({aa[i],{0,i}});
		ss.pb({bb[i],{1,i}});*/
	}
	int ind=-1;
	for(auto j:tt){
		ind++;
		tt2[j.a]=ind;
	}
	for(int i=0;i<n;i++){
		aa[i]=tt2[aa[i]];
		bb[i]=tt2[bb[i]];
		update(0,0,ind,bb[i],{aa[i],i});
	}
	for(int i=0;i<n;i++){
		pair<int,int> x=query(0,0,ind,aa[i],bb[i]);
		if(x.a<aa[i]){
			par[i][0]=x.b;
		}
		else{
			par[i][0]=-1;
		}
	}
	sort(ss.begin(),ss.end());
	
/*	for(int i=0;i<n;i++){
		cout<<par[i][0]<<",";
	}
	cout<<endl;*/
	for(int j=1;j<20;j++){
		for(int i=0;i<n;i++){
			if(par[i][j-1]==-1){
				par[i][j]=-1;
			}
			else{
				par[i][j]=par[par[i][j-1]][j-1];
			}
		}
	}
	for(int ii=0;ii<q;ii++){
		int l,r;
		cin>>l>>r;
		l--;
		r--;

		if(bb[r]<bb[l]){
			cout<<"impossible"<<endl;
			continue;
		}
		if(l==r){
			cout<<0<<endl;
			continue;
		}
		if(aa[r]<=bb[l] and bb[r]>=bb[l]){
			cout<<1<<endl;
			continue;
		}
		int cur=r;
		int su=0;
		for(int j=19;j>=0;j--){
			if(par[cur][j]>=0){
				if(aa[par[cur][j]]>bb[l]){
					cur=par[cur][j];
					su+=(1<<j);
				}
			}

		}
		if(par[cur][0]==-1){
			su=-1;
		}
		else{
			cur=par[cur][0];
			su+=2;
		}
		if(su==-1){
			cout<<"impossible"<<endl;
			continue;
		}
		cout<<su<<endl;
		
		





	}
	/*for(int ii=0;ii<n;ii++){
		if(pre[ii].size()){
			pair<int,int> ma={-1,-1};
			for(int i=0;i<n;i++){
				par[i][0]=-1;
			}
			for(auto j:ss){
				if(bb[j.b.b]>bb[ii]){
					continue;
				}
				if(j.b.a==0){
					//cur.insert({bb[j.b.b],j.b.b});
					ma=max(ma,{bb[j.b.b],j.b.b});
				}
				else{
					if(ma.a>j.a){

						par[j.b.b][0]=ma.b;
					}
				}
			}
			for(int j=1;j<20;j++){
				for(int i=0;i<n;i++){
					if(par[i][j-1]==-1){
						par[i][j]=-1;
					}
					else{
						par[i][j]=par[par[i][j-1]][j-1];
					}
				}
			}
			for(auto jj:pre[ii]){
				int r=ii;
				int l=jj.a;
				if(l==r){
					ans[jj.b]=0;
					continue;
				}
				if(bb[r]<bb[l]){
					ans[jj.b]=-1;
					continue;
				}
				int su=0;
				int cur=l;
				for(int j=19;j>=0;j--){
					if(par[cur][j]>=0){
						if(bb[par[cur][j]]<bb[r]){
							su+=(1<<j);
							cur=par[cur][j];
						}
					}
				}
				//if(par[cur][0]>=0){
					
					if(aa[r]<=bb[cur] and bb[r]>=bb[cur]){
						su++;
						cur=par[cur][0];
					}
					else{
						if(par[cur][0]>=0){
							cur=par[cur][0];
							if(aa[r]<=bb[cur] and bb[r]>=bb[cur]){
								su+=2;
							}
							else{
								su=-1;
							}
						}
						else{
							su=-1;
						}
					}
				//}
				//else{
				//	su=-1;
				//}

				ans[jj.b]=su;
			}
		}
	}
	for(int i=0;i<q;i++){
		if(ans[i]==-1){
			cout<<"impossible"<<endl;
		}
		else{
			cout<<ans[i]<<endl;
		}
	}

	



*/


	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 319 ms 27432 KB Output is correct
3 Correct 316 ms 27500 KB Output is correct
4 Correct 377 ms 27464 KB Output is correct
5 Correct 375 ms 28552 KB Output is correct
6 Correct 370 ms 29472 KB Output is correct
7 Correct 298 ms 29632 KB Output is correct
8 Correct 394 ms 37400 KB Output is correct
9 Correct 427 ms 37320 KB Output is correct
10 Correct 372 ms 27920 KB Output is correct
11 Correct 308 ms 31604 KB Output is correct
12 Correct 159 ms 27976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 4 ms 2936 KB Output is correct
4 Correct 3 ms 2900 KB Output is correct
5 Correct 4 ms 2900 KB Output is correct
6 Correct 4 ms 2896 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 5 ms 3028 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 4 ms 2936 KB Output is correct
4 Correct 3 ms 2900 KB Output is correct
5 Correct 4 ms 2900 KB Output is correct
6 Correct 4 ms 2896 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 5 ms 3028 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 3 ms 2644 KB Output is correct
12 Correct 4 ms 2900 KB Output is correct
13 Correct 5 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 5 ms 2900 KB Output is correct
16 Correct 4 ms 2944 KB Output is correct
17 Correct 3 ms 3028 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 37 ms 4540 KB Output is correct
20 Correct 36 ms 4388 KB Output is correct
21 Correct 41 ms 4572 KB Output is correct
22 Correct 34 ms 4708 KB Output is correct
23 Correct 36 ms 4948 KB Output is correct
24 Correct 46 ms 4900 KB Output is correct
25 Correct 35 ms 4564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 4 ms 2936 KB Output is correct
4 Correct 3 ms 2900 KB Output is correct
5 Correct 4 ms 2900 KB Output is correct
6 Correct 4 ms 2896 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 5 ms 3028 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 4 ms 2900 KB Output is correct
13 Correct 4 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 4 ms 3028 KB Output is correct
17 Correct 4 ms 3028 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 334 ms 26912 KB Output is correct
20 Correct 279 ms 26828 KB Output is correct
21 Correct 205 ms 26808 KB Output is correct
22 Correct 240 ms 30356 KB Output is correct
23 Correct 331 ms 36196 KB Output is correct
24 Correct 334 ms 36276 KB Output is correct
25 Correct 39 ms 17428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 325 ms 27572 KB Output is correct
2 Correct 288 ms 27516 KB Output is correct
3 Correct 354 ms 27520 KB Output is correct
4 Correct 344 ms 37444 KB Output is correct
5 Correct 295 ms 27840 KB Output is correct
6 Correct 397 ms 37068 KB Output is correct
7 Correct 396 ms 36976 KB Output is correct
8 Correct 410 ms 37104 KB Output is correct
9 Correct 421 ms 36304 KB Output is correct
10 Correct 447 ms 36692 KB Output is correct
11 Correct 381 ms 36520 KB Output is correct
12 Correct 411 ms 36700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 319 ms 27432 KB Output is correct
3 Correct 316 ms 27500 KB Output is correct
4 Correct 377 ms 27464 KB Output is correct
5 Correct 375 ms 28552 KB Output is correct
6 Correct 370 ms 29472 KB Output is correct
7 Correct 298 ms 29632 KB Output is correct
8 Correct 394 ms 37400 KB Output is correct
9 Correct 427 ms 37320 KB Output is correct
10 Correct 372 ms 27920 KB Output is correct
11 Correct 308 ms 31604 KB Output is correct
12 Correct 159 ms 27976 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 4 ms 2936 KB Output is correct
16 Correct 3 ms 2900 KB Output is correct
17 Correct 4 ms 2900 KB Output is correct
18 Correct 4 ms 2896 KB Output is correct
19 Correct 4 ms 3028 KB Output is correct
20 Correct 5 ms 3028 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 3 ms 2644 KB Output is correct
24 Correct 4 ms 2900 KB Output is correct
25 Correct 5 ms 2900 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Correct 5 ms 2900 KB Output is correct
28 Correct 4 ms 2944 KB Output is correct
29 Correct 3 ms 3028 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 37 ms 4540 KB Output is correct
32 Correct 36 ms 4388 KB Output is correct
33 Correct 41 ms 4572 KB Output is correct
34 Correct 34 ms 4708 KB Output is correct
35 Correct 36 ms 4948 KB Output is correct
36 Correct 46 ms 4900 KB Output is correct
37 Correct 35 ms 4564 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 4 ms 2900 KB Output is correct
41 Correct 4 ms 2900 KB Output is correct
42 Correct 3 ms 2900 KB Output is correct
43 Correct 4 ms 2900 KB Output is correct
44 Correct 4 ms 3028 KB Output is correct
45 Correct 4 ms 3028 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 334 ms 26912 KB Output is correct
48 Correct 279 ms 26828 KB Output is correct
49 Correct 205 ms 26808 KB Output is correct
50 Correct 240 ms 30356 KB Output is correct
51 Correct 331 ms 36196 KB Output is correct
52 Correct 334 ms 36276 KB Output is correct
53 Correct 39 ms 17428 KB Output is correct
54 Correct 325 ms 27572 KB Output is correct
55 Correct 288 ms 27516 KB Output is correct
56 Correct 354 ms 27520 KB Output is correct
57 Correct 344 ms 37444 KB Output is correct
58 Correct 295 ms 27840 KB Output is correct
59 Correct 397 ms 37068 KB Output is correct
60 Correct 396 ms 36976 KB Output is correct
61 Correct 410 ms 37104 KB Output is correct
62 Correct 421 ms 36304 KB Output is correct
63 Correct 447 ms 36692 KB Output is correct
64 Correct 381 ms 36520 KB Output is correct
65 Correct 411 ms 36700 KB Output is correct
66 Correct 2 ms 2644 KB Output is correct
67 Correct 245 ms 27508 KB Output is correct
68 Correct 235 ms 27428 KB Output is correct
69 Correct 273 ms 27400 KB Output is correct
70 Correct 264 ms 28428 KB Output is correct
71 Correct 317 ms 29376 KB Output is correct
72 Correct 342 ms 29676 KB Output is correct
73 Correct 376 ms 37348 KB Output is correct
74 Correct 341 ms 37368 KB Output is correct
75 Correct 315 ms 27936 KB Output is correct
76 Correct 340 ms 31584 KB Output is correct
77 Correct 150 ms 27992 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2900 KB Output is correct
80 Correct 3 ms 2900 KB Output is correct
81 Correct 3 ms 2908 KB Output is correct
82 Correct 3 ms 2900 KB Output is correct
83 Correct 4 ms 3012 KB Output is correct
84 Correct 3 ms 3028 KB Output is correct
85 Correct 2 ms 2772 KB Output is correct
86 Correct 38 ms 4500 KB Output is correct
87 Correct 52 ms 4356 KB Output is correct
88 Correct 44 ms 4668 KB Output is correct
89 Correct 30 ms 4692 KB Output is correct
90 Correct 40 ms 5004 KB Output is correct
91 Correct 32 ms 4912 KB Output is correct
92 Correct 44 ms 4604 KB Output is correct
93 Correct 269 ms 26904 KB Output is correct
94 Correct 208 ms 26920 KB Output is correct
95 Correct 294 ms 26832 KB Output is correct
96 Correct 300 ms 30352 KB Output is correct
97 Correct 406 ms 36296 KB Output is correct
98 Correct 436 ms 36320 KB Output is correct
99 Correct 41 ms 17528 KB Output is correct
100 Correct 475 ms 36984 KB Output is correct
101 Correct 488 ms 37060 KB Output is correct
102 Correct 473 ms 37192 KB Output is correct
103 Correct 422 ms 36684 KB Output is correct
104 Correct 498 ms 36464 KB Output is correct
105 Correct 426 ms 36724 KB Output is correct
106 Correct 266 ms 25204 KB Output is correct
107 Correct 300 ms 27708 KB Output is correct
108 Correct 348 ms 27696 KB Output is correct
109 Correct 334 ms 27744 KB Output is correct
110 Correct 537 ms 36948 KB Output is correct
111 Correct 517 ms 36948 KB Output is correct