結果

問題 No.913 木の燃やし方
ユーザー treeonetreeone
提出日時 2019-10-17 14:53:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,059 bytes
コンパイル時間 2,471 ms
コンパイル使用メモリ 210,084 KB
実行使用メモリ 9,524 KB
最終ジャッジ日時 2023-09-07 20:20:23
合計ジャッジ時間 18,344 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 398 ms
9,384 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int64 INF = 1e18;

template< typename T, bool isMin >
struct ConvexHullTrickAddMonotone {
#define F first
#define S second
	using P = pair< T, T >;
	deque< P > H;

	ConvexHullTrickAddMonotone() = default;

	bool empty() const { return H.empty(); }

	void clear() { H.clear(); }

	inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

	using D = long double;

	inline bool check(const P &a, const P &b, const P &c) {
		if(b.S == a.S || c.S == b.S)
			return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S);

		//return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F);
		return D(b.F - a.F) * sgn(c.S - b.S) / D(abs(b.S - a.S)) >=
				D(c.F - b.F) * sgn(b.S - a.S) / D(abs(c.S - b.S));
	}

	void add(T a, T b) {
		if(!isMin) a *= -1, b *= -1;
		P line(a, b);
		if(empty()) {
			H.emplace_front(line);
			return;
		}
		if(H.front().F <= a) {
			if(H.front().F == a) {
				if(H.front().S <= b) return;
				H.pop_front();
			}
			while(H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
			H.emplace_front(line);
		} else {
			assert(a <= H.back().F);
			if(H.back().F == a) {
				if(H.back().S <= b) return;
				H.pop_back();
			}
			while(H.size() >= 2 && check(H[H.size() - 2], H.back(), line)) H.pop_back();
			H.emplace_back(line);
		}
	}

	inline T get_y(const P &a, const T &x) {
		return a.F * x + a.S;
	}

	T query(T x) {
		assert(!empty());
		int l = -1, r = H.size() - 1;
		while(l + 1 < r) {
			int m = (l + r) >> 1;
			if(get_y(H[m], x) >= get_y(H[m + 1], x)) l = m;
			else r = m;
		}
		if(isMin) return get_y(H[r], x);
		return -get_y(H[r], x);
	}

	T query_monotone_inc(T x) {
		assert(!empty());
		while(H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x)) H.pop_front();
		if(isMin) return get_y(H.front(), x);
		return -get_y(H.front(), x);
	}

	T query_monotone_dec(T x) {
		assert(!empty());
		while(H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x)) H.pop_back();
		if(isMin) return get_y(H.back(), x);
		return -get_y(H.back(), x);
	}

#undef F
#undef S
};

int N;
vector<int64> A, S, ans;

void calc(int L, int R){
	if(L >= R) return;
	int M = (L + R) / 2;
	calc(L, M);
	calc(M + 1, R);

	if(L < M){
		ConvexHullTrickAddMonotone<int64, true> cht;
		for(int64 l = M + 1; l <= R; l++){
			cht.add(-2 * l, l * l - S[l]);
		}
		int64 MIN = INF;
		for(int64 k = L; k < M; k++){
			MIN = min(MIN, cht.query_monotone_inc(k) + k * k + S[k]);
			ans[k] = min(ans[k], MIN);
		}
	}
	if(M < R){
		ConvexHullTrickAddMonotone<int64, true> cht;
		for(int64 k = M; k >= L; k--){
			cht.add(-2 * k, k * k + S[k]);
		}
		int64 MIN = INF;
		for(int64 l = R; l > M; l--){
			MIN = min(MIN, cht.query_monotone_dec(l) + l * l - S[l]);
			ans[l - 1] = min(ans[l - 1], MIN);
		}
	}
}

int main(){
	cin >> N;
	A.resize(N);
	S.resize(N + 1, 0);
	ans.resize(N, INF);
	for(int i = 0; i < N; i++) cin >> A[i];
	for(int i = 1; i <= N; i++) S[i] = S[i - 1] + A[i - 1];

	calc(0, N);
	for(int i = 0; i < N; i++){
		cout << ans[i] << endl;
	}
}
0