結果

問題 No.1297 銅像
ユーザー 沙耶花沙耶花
提出日時 2020-05-02 22:19:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,663 bytes
コンパイル時間 2,330 ms
コンパイル使用メモリ 210,360 KB
実行使用メモリ 18,964 KB
最終ジャッジ日時 2023-09-18 01:38:15
合計ジャッジ時間 7,286 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 107 ms
18,544 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 123 ms
18,604 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 138 ms
18,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000000

template <typename T>
struct CHT{

	vector<T> v;
	vector<T> a,b;
	int n;
	long long sign;
	
	T init_value;
	
	CHT(vector<T> &x,T iv,long long s=1){
		init_value = iv;
		sign = s;
		v = x;
		sort(v.begin(),v.end());
		int temp = distance(v.begin(),unique(v.begin(),v.end()));
		v.resize(temp);
		n=1;
		while(true){
			if(n>=v.size())break;
			n<<=1;
		}
		
		v.resize(n,1000000001);
		a.resize(2*n,0);
		b.resize(2*n,init_value);
	}
		
	void add(T A,T B,int start){
		int now = start;
		
		while(true){
			int ll = (now<<(__builtin_clz(now)-__builtin_clz(n)))-n;
			int rr = ll + (n>>(31-__builtin_clz(now))) -1;
			int m = (ll+rr)>>1;
			bool fl = sign*(A*v[ll]+B)<sign*(a[now]*v[ll]+b[now]);
			bool fm = sign*(A*v[m]+B)<sign*(a[now]*v[m]+b[now]);
			bool fr = sign*(A*v[rr]+B)<sign*(a[now]*v[rr]+b[now]);

			if(fl&&fr){
				a[now]=A;
				b[now]=B;
				break;
			}
			if(!fl&&!fr){
				break;
			}
			if(fm){
				swap(A,a[now]);
				swap(B,b[now]);
				swap(fl,fr);
			}
			if(fl){
				now<<=1;
			}
			else{
				now<<=1;
				now++;
			}
		}
	}

	void add_line(T A,T B){
		add(A,B,1);
	}
	
	void add_segment(T A,T B,T L,T R){
		L = distance(v.begin(),lower_bound(v.begin(),v.end(),L));
		R = distance(v.begin(),lower_bound(v.begin(),v.end(),R));
		if(R==L)return;
		L+=n;
		R+=n;
		
		while(true){
			if(L&1){
				add(A,B,L++);
			}
			if(R&1){
				add(A,B,--R);
			}
			if(L>=R)break;
			L>>=1;R>>=1;
		}
	}
	
	T query(T x){
		int now = n + distance(v.begin(),lower_bound(v.begin(),v.end(),x));
		T ret = init_value;
		while(true){
			ret = min(sign*ret,sign*(a[now]*x+b[now]))*sign;
			now>>=1;
			if(now<=0)break;
		}
		return ret;
	}
	
	void show(){
		int n = 1;
		for(int i=1;i<a.size();i++){
			for(int j=0;j<n;j++){
				if(j!=0)cout<<' ';
				cout<<a[i+j];
			}
			cout<<endl;
			i+=n-1;
			n*=2;
		}
	}
	
};

int main(){
	
	int N;
	cin>>N;
	
	long long D;
	cin>>D;
	
	vector<long long> a(N),b(N);
	for(int i=0;i<N;i++){
		cin>>a[i]>>b[i];
	}
	
	vector<long long> x1(N),x2(N+1);
	for(int i=0;i<=N;i++){
		if(i!=N)x1[i] = a[i] + D*(i+1);
		x2[i] = i;
	}
	
	vector<long long> dp1(N+1,Inf),dp2(N+1,Inf);
	
	CHT<long long> C1(x1,Inf),C2(x2,Inf);
	
	dp2[0] = 0;
	C1.add_line(0LL,0LL);
	
	for(long long i=1;i<=N;i++){
		dp1[i] = (C1.query(a[i-1]+D*i) + a[i-1]*i*2 + i*i*D - i*D + b[i-1]*2)/2;
		dp2[i] = min(dp1[i],(C2.query(i) + i*i*D + i*D)/2);
		C1.add_line(-2*i,dp2[i]*2 + i*i*D + i*D);
		C2.add_line((a[i-1]-i*D)*2,dp1[i]*2+(-a[i-1]*i)*2+i*i*D-i*D);
	}
	
	cout<<dp2.back()<<endl;

	return 0;
}
0