Submission #868098

# Submission time Handle Problem Language Result Execution time Memory
868098 2023-10-30T12:21:05 Z willychan Bitaro's travel (JOI23_travel) C++14
0 / 100
63 ms 68652 KB
#include<bits/stdc++.h>
using namespace std;
//#include<bits/extc++.h>
//__gnu_pbds
vector<long long> x;
vector<int> rlim[20];
vector<int> llim[20];
int n;
long long ans = 0;

int getmin(int l,int r){
	int len = (r-l+1);
	len = __lg(len);
	if(len==0) return rlim[0][l];
	return min(rlim[len][l],rlim[len][r-(1<<(len))+1]);
}
int getmax(int l,int r){
	int len = (r-l+1);
	len = __lg(len);
	if(len==0) return llim[0][l];
	return max(llim[len][l],llim[len][r-(1<<len)+1]);
}

void solve(){
	int a;cin>>a;	
	int k = lower_bound(x.begin(),x.end(),a)-x.begin();
	if(abs(x[k-1]-a)<=abs(x[k]-a)) k--;
	ans=abs(x[k]-a);
	int cur = k;
	int l = k;int r = k;
	while(l!=1 || r!=n){
	//	cout<<ans<<"\n";
	//  cout<<l<<" "<<r<<"\n";
		if(abs(x[l-1]-x[cur])<=abs(x[r+1]-x[cur])){
			ans+=abs(x[l-1]-x[cur]);
			cur = l-1;
			int rr = l-1;
			int ll = 1;
			while(rr-ll>1){
				int mid = (rr+ll)/2;
				if(getmin(mid,l-1)<=r) ll = mid;
				else rr = mid;
			}
			ans+=abs(x[ll]-x[cur]);
			l = ll;
			cur = l;
		}else{
			ans+=abs(x[r+1]-x[cur]);		
			cur = r+1;
			int rr = n;
			int ll = r+1;
			while(rr-ll>1){
				int mid = (rr+ll)/2;
				if(getmax(r+1,mid)>=l) rr = mid;
				else ll = mid;
			}
			ans+=abs(x[rr]-x[cur]);
			r = rr;
			cur = r;
		}
	}
	cout<<ans<<"\n";
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;	
	x.resize(n+2);
	for(int p=0;p<20;p++){
		rlim[p].resize(n+2);
		llim[p].resize(n+2);
	}
	x[0]=-1e15;
	x[n+1]=1e15; 
	for(int i=1;i<=n;i++) cin>>x[i];
	for(int i=1;i<=n;i++){
		int l = i;int r = n+1;	
		while(r-l>1){
			int mid = (l+r)/2;
			if(abs(x[i]-x[i-1])>abs(x[mid]-x[i])) l = mid;
			else r = mid;
		}
		rlim[i][0]=l;
	}
	for(int i=1;i<=n;i++){
		int r = i;int l = 0;
		while(r-l>1){
			int mid = (l+r)/2;
			if(abs(x[i]-x[i+1])>=abs(x[mid]-x[i]))  r = mid;
			else l = mid;
		}
		llim[i][0]=r;
	}
	for(int p=1;p<20;p++){
		for(int i=1;i+(1<<(p-1))<=n;i++){
			rlim[p][i] = min(rlim[p-1][i],rlim[p-1][i+(1<<(p-1))]);
			llim[p][i] = max(llim[p-1][i],llim[p-1][i+(1<<(p-1))]);
		}
	}
	int q;cin>>q;
	while(q--){
		solve();
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 1 ms 1116 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 1 ms 1116 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Runtime error 63 ms 68652 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Runtime error 1 ms 1116 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -