Submission #1000390

# Submission time Handle Problem Language Result Execution time Memory
1000390 2024-06-17T11:13:33 Z pcc Event Hopping (BOI22_events) C++17
30 / 100
281 ms 15596 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>

const int mxn = 1e5+10;

int ord[mxn],perm[mxn];
pii arr[mxn];

struct SEG{
#define ls now*2+1
#define rs now*2+2
#define mid ((l+r)>>1)
	int seg[mxn*4];
	void modify(int now,int l,int r,int p,int v){
		if(l == r){
			seg[now] = v;
			return;
		}
		if(mid>=p)modify(ls,l,mid,p,v);
		else modify(rs,mid+1,r,p,v);
		seg[now] = min(seg[ls],seg[rs]);
	}
	int getval(int now,int l,int r,int s,int e){
		if(l>=s&&e>=r)return seg[now];
		if(mid>=e)return getval(ls,l,mid,s,e);
		else if(mid<s)return getval(rs,mid+1,r,s,e);
		else return min(getval(ls,l,mid,s,e),getval(rs,mid+1,r,s,e));
	}
#undef ls
#undef rs
#undef mid
};

SEG seg;
int dp[mxn][20];
int N,Q;

int get_range(int rp,int lim){
	int l = 1,r = rp;
	while(l != r){
		int mid = (l+r)>>1;
		int now = ord[mid];
		if(arr[now].sc<lim)l = mid+1;
		else r = mid;
	}
	return l;
}

void calc(int b){
	for(int i = 1;i<=N;i++)seg.modify(0,1,N,i,dp[i][b-1]);
	for(int i = N;i>=1;i--){
		dp[i][b] = seg.getval(0,1,N,dp[i][b-1],i);
	}
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>Q;
	for(int i = 1;i<=N;i++)cin>>arr[i].fs>>arr[i].sc,ord[i] = i;
	sort(ord+1,ord+N+1,[](int a,int b){return arr[a].sc == arr[b].sc?arr[a].fs<arr[b].fs:arr[a].sc<arr[b].sc;});
	for(int i = 1;i<=N;i++)perm[ord[i]] = i;
	for(int i = N;i>=1;i--){
		int now = ord[i];
		int lp = get_range(i,arr[now].fs);
		dp[i][0] = lp;
	}
	for(int i = 1;i<20;i++)calc(i);
	while(Q--){
		int s,t;
		cin>>s>>t;
		if(s == t){
			cout<<"0\n";
			continue;
		}
		if(arr[s].sc == arr[t].sc){
			cout<<"1\n";
			continue;
		}
		s = perm[s],t = perm[t];
		if(s>t){
			cout<<"impossible\n";
			continue;
		}
		int ans = 0;
		for(int i = 19;i>=0;i--){
			if(dp[t][i]>s)t = dp[t][i],ans|=1<<i;
		}
		if(ans>N){
			cout<<"impossible\n";
		}
		else cout<<ans+1<<'\n';
	}

	/*
	for(int i = 1;i<=N;i++)cerr<<ord[i]<<' ';cout<<'\n';
	for(int i = 1;i<=N;i++){
		for(int j = 0;j<20;j++)cerr<<dp[i][j]<<' ';cout<<'\n';
	}
   */

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 253 ms 14992 KB Output is correct
3 Correct 250 ms 15088 KB Output is correct
4 Correct 252 ms 15096 KB Output is correct
5 Correct 253 ms 15092 KB Output is correct
6 Correct 255 ms 14932 KB Output is correct
7 Correct 281 ms 15200 KB Output is correct
8 Correct 227 ms 15444 KB Output is correct
9 Correct 209 ms 15448 KB Output is correct
10 Correct 255 ms 15352 KB Output is correct
11 Correct 246 ms 15316 KB Output is correct
12 Correct 207 ms 14840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Incorrect 2 ms 4444 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Incorrect 2 ms 4444 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4696 KB Output is correct
6 Incorrect 2 ms 4444 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 248 ms 14932 KB Output is correct
2 Correct 250 ms 14932 KB Output is correct
3 Correct 253 ms 14932 KB Output is correct
4 Correct 212 ms 15596 KB Output is correct
5 Correct 244 ms 15444 KB Output is correct
6 Correct 254 ms 15096 KB Output is correct
7 Correct 272 ms 15028 KB Output is correct
8 Correct 270 ms 15348 KB Output is correct
9 Correct 211 ms 13536 KB Output is correct
10 Correct 255 ms 14836 KB Output is correct
11 Correct 216 ms 14672 KB Output is correct
12 Correct 248 ms 14668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 253 ms 14992 KB Output is correct
3 Correct 250 ms 15088 KB Output is correct
4 Correct 252 ms 15096 KB Output is correct
5 Correct 253 ms 15092 KB Output is correct
6 Correct 255 ms 14932 KB Output is correct
7 Correct 281 ms 15200 KB Output is correct
8 Correct 227 ms 15444 KB Output is correct
9 Correct 209 ms 15448 KB Output is correct
10 Correct 255 ms 15352 KB Output is correct
11 Correct 246 ms 15316 KB Output is correct
12 Correct 207 ms 14840 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4696 KB Output is correct
18 Incorrect 2 ms 4444 KB Output isn't correct
19 Halted 0 ms 0 KB -