結果

問題 No.704 ゴミ拾い Medium
ユーザー leaf_1415leaf_1415
提出日時 2019-09-25 21:07:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 164 ms / 1,500 ms
コード長 2,010 bytes
コンパイル時間 1,597 ms
コンパイル使用メモリ 69,512 KB
実行使用メモリ 16,568 KB
最終ジャッジ日時 2023-10-23 22:04:53
合計ジャッジ時間 9,231 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,304 KB
testcase_01 AC 5 ms
13,304 KB
testcase_02 AC 6 ms
13,304 KB
testcase_03 AC 5 ms
13,304 KB
testcase_04 AC 6 ms
13,304 KB
testcase_05 AC 5 ms
13,304 KB
testcase_06 AC 6 ms
13,304 KB
testcase_07 AC 6 ms
13,304 KB
testcase_08 AC 5 ms
13,304 KB
testcase_09 AC 6 ms
13,304 KB
testcase_10 AC 5 ms
13,304 KB
testcase_11 AC 6 ms
13,304 KB
testcase_12 AC 5 ms
13,304 KB
testcase_13 AC 5 ms
13,304 KB
testcase_14 AC 7 ms
13,304 KB
testcase_15 AC 6 ms
13,304 KB
testcase_16 AC 6 ms
13,304 KB
testcase_17 AC 6 ms
13,304 KB
testcase_18 AC 7 ms
13,304 KB
testcase_19 AC 6 ms
13,304 KB
testcase_20 AC 6 ms
13,304 KB
testcase_21 AC 6 ms
13,304 KB
testcase_22 AC 6 ms
13,304 KB
testcase_23 AC 6 ms
13,304 KB
testcase_24 AC 164 ms
16,568 KB
testcase_25 AC 163 ms
16,568 KB
testcase_26 AC 162 ms
16,568 KB
testcase_27 AC 162 ms
16,568 KB
testcase_28 AC 163 ms
16,568 KB
testcase_29 AC 163 ms
16,568 KB
testcase_30 AC 163 ms
16,568 KB
testcase_31 AC 163 ms
16,568 KB
testcase_32 AC 163 ms
16,568 KB
testcase_33 AC 163 ms
16,568 KB
testcase_34 AC 107 ms
16,568 KB
testcase_35 AC 107 ms
16,568 KB
testcase_36 AC 108 ms
16,568 KB
testcase_37 AC 106 ms
16,568 KB
testcase_38 AC 107 ms
16,568 KB
testcase_39 AC 107 ms
16,568 KB
testcase_40 AC 106 ms
16,568 KB
testcase_41 AC 107 ms
16,568 KB
testcase_42 AC 107 ms
16,568 KB
testcase_43 AC 107 ms
16,568 KB
testcase_44 AC 6 ms
13,304 KB
testcase_45 AC 5 ms
13,304 KB
testcase_46 AC 142 ms
16,568 KB
testcase_47 AC 152 ms
16,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

struct LiChaoTree{
	int size;
	vector<P> seg;
	
	LiChaoTree(){}
	LiChaoTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = make_pair(0, inf);
	}
	llint calc(P f, llint x)
	{
		return f.first * x + f.second;
	}
	llint query(int x) //xにおける最小値を取得
	{
		int i = x + (1 << size);
		llint ret = inf;
		while(i >= 1){
			ret = min(ret, calc(seg[i], x));
			i /= 2;
		}
		return ret;
	}
	void add(int k, int l, int r, P f)
	{
		int m = (l+r)/2;
		if(calc(f, m) < calc(seg[k], m)) swap(seg[k], f);
		bool L = (calc(f, l) < calc(seg[k], l)), R = (calc(f, r) < calc(seg[k], r));
		if(L == R) return;
		if(L) add(k*2, l, m, f);
		if(R) add(k*2+1, m+1, r, f);
	}
	void addSegment(int a, int b, int k, int l, int r, P f)
	{
		if(b < l || r < a) return;
		if(a <= l && r <= b){
			add(k, l, r, f);
			return;
		}
		addSegment(a, b, k*2, l, (l+r)/2, f);
		addSegment(a, b, k*2+1, (l+r)/2+1, r, f);
	}
	void addSegment(int a, int b, llint p, llint q) //区間[a, b]に線分px+qを追加
	{
		addSegment(a, b, 1, 0, (1<<size)-1, make_pair(p, q));
	}
	void addLine(llint p, llint q) //直線px+qを追加
	{
		return addSegment(0, (1<<size)-1, p, q);
	}
};

llint n;
llint a[300005], x[300005], y[300005];
llint dp[300005];
LiChaoTree lct(17);

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for(int i = n; i >= 1; i--) cin >> a[i];
	for(int i = n; i >= 1; i--) cin >> x[i];
	for(int i = n; i >= 1; i--) cin >> y[i];
	
	lct.init();
	dp[0] = 0;
	lct.addSegment(a[1], (1<<17)-1, 1, -a[1]+dp[0]);
	lct.addSegment(0, a[1]-1, -1, a[1]+dp[0]);
	
	for(int i = 1; i <= n; i++){
		dp[i] = lct.query(x[i]) + y[i];
		lct.addSegment(a[i+1], (1<<17)-1, 1, -a[i+1]+dp[i]);
		lct.addSegment(0, a[i+1]-1, -1, a[i+1]+dp[i]);
		
	}
	cout << dp[n] << endl;
	
	return 0;
}
0