제출 #1104690

#제출 시각아이디문제언어결과실행 시간메모리
1104690alexander707070Shortcut (IOI16_shortcut)C++14
97 / 100
1865 ms59480 KiB
#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;

const long long inf=1e15;

int n,len[MAXN],d[MAXN],c;
long long pos[MAXN];

int where[MAXN],cnt;
pair<long long,int> mp[MAXN];

struct rect{
	long long minx,maxx;
	long long miny,maxy;

	inline friend rect operator + (rect fr,rect sc){
		return {max(fr.minx,sc.minx),min(fr.maxx,sc.maxx),max(fr.miny,sc.miny),min(fr.maxy,sc.maxy)};
	}
};

struct Fenwick{
	long long fenwick[MAXN];

	void init(long long s){
		for(int i=1;i<=n;i++)fenwick[i]=s;
	}

	void updatemin(int x,long long val){
		while(x<=n){
			fenwick[x]=min(fenwick[x],val);
			x+=(x & (-x));
		}
	}

	void updatemax(int x,long long val){
		while(x<=n){
			fenwick[x]=max(fenwick[x],val);
			x+=(x & (-x));
		}
	}

	long long getmin(int x){
		long long res=inf;
		while(x>=1){
			res=min(res,fenwick[x]);
			x^=(x & (-x));
		}
		return res;
	}

	long long getmax(int x){
		long long res=-inf;
		while(x>=1){
			res=max(res,fenwick[x]);
			x^=(x & (-x));
		}
		return res;
	}

}fenx,feny;

int bin(long long x){
	int l=0,r=n+1,tt;

	while(l+1<r){
		tt=(l+r)/2;
		if(x<=mp[tt].first){
			r=tt;
		}else{
			l=tt;
		}
	}

	if(r==n+1)return cnt+1;
	return where[mp[r].second];
}

bool ok(long long fix){
	rect s={-inf,inf,-inf,inf},curr;

	fenx.init(-inf);
	feny.init(inf);

	for(int i=n;i>=1;i--){

		int from=bin(fix+pos[i]-d[i]+1);

		long long s1=fenx.getmax(cnt+1-from);
		long long s2=feny.getmin(cnt+1-from);
		
		curr={pos[i]+d[i]+s1-fix+c , pos[i]-d[i]+s2+fix-c , -pos[i]+d[i]+s1-fix+c , -pos[i]-d[i]+s2+fix-c};
		s=s+curr;

		if(s.minx>s.maxx or s.miny>s.maxy)return false;

		fenx.updatemax(cnt+1-where[i],pos[i]+d[i]);
		feny.updatemin(cnt+1-where[i],pos[i]-d[i]);
	}

	for(int i=1;i<=n;i++){
		int l=i+1,r=n+1,tt;

		while(l+1<r){
			tt=(l+r)/2;
			if(pos[tt]<=min(s.maxx-pos[i],s.maxy+pos[i])){
				l=tt;
			}else{
				r=tt;
			}
		}

		if(pos[l]-pos[i]>=s.miny and pos[l]-pos[i]<=s.maxy and pos[l]+pos[i]>=s.minx and pos[l]+pos[i]<=s.maxx)return true;
	}

	return false;
}

long long find_shortcut(int N,vector<int> L,vector<int> D, int C){
	n=N; c=C;

	for(int i=1;i<=n-1;i++){
		len[i]=L[i-1];
	}

	for(int i=2;i<=n;i++){
		pos[i]=pos[i-1]+len[i-1];
	}

	for(int i=1;i<=n;i++){
		d[i]=D[i-1];
	}

	for(int i=1;i<=n;i++)mp[i]={pos[i]+d[i],i};
	sort(mp+1,mp+n+1);

	for(int i=1;i<=n;i++){
		if(i==1 or mp[i].first!=mp[i-1].first)cnt++;
		where[mp[i].second]=cnt;
	}

	long long l=0,r=inf,tt;
	while(l+1<r){
		tt=(l+r)/2;
		if(ok(tt)){
			r=tt;
		}else{
			l=tt;
		}
	}

	return r;
}

/*int main(){

	//cout<<find_shortcut(9, {10, 10, 10, 10, 10, 10, 10, 10},{20, 0, 30, 0, 0, 40, 0, 40, 0}, 30)<<"\n";
	//cout<<find_shortcut(4, {2, 2, 2},{1, 10, 10, 1}, 1)<<"\n";
	//cout<<find_shortcut(3, {1, 1},{1, 1, 1}, 3)<<"\n";



	return 0;
}*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...