結果

問題 No.913 木の燃やし方
ユーザー chocoruskchocorusk
提出日時 2019-10-19 09:02:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 251 ms / 3,000 ms
コード長 2,869 bytes
コンパイル時間 2,835 ms
コンパイル使用メモリ 115,372 KB
実行使用メモリ 14,716 KB
最終ジャッジ日時 2023-09-08 18:15:36
合計ジャッジ時間 13,592 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,500 KB
testcase_01 AC 2 ms
5,492 KB
testcase_02 AC 2 ms
5,548 KB
testcase_03 AC 4 ms
5,616 KB
testcase_04 AC 5 ms
5,596 KB
testcase_05 AC 4 ms
5,536 KB
testcase_06 AC 3 ms
5,572 KB
testcase_07 AC 3 ms
5,512 KB
testcase_08 AC 241 ms
8,028 KB
testcase_09 AC 231 ms
7,968 KB
testcase_10 AC 232 ms
7,916 KB
testcase_11 AC 238 ms
8,028 KB
testcase_12 AC 251 ms
8,192 KB
testcase_13 AC 236 ms
8,088 KB
testcase_14 AC 227 ms
7,904 KB
testcase_15 AC 225 ms
7,972 KB
testcase_16 AC 233 ms
8,112 KB
testcase_17 AC 223 ms
8,052 KB
testcase_18 AC 222 ms
8,024 KB
testcase_19 AC 223 ms
14,660 KB
testcase_20 AC 242 ms
8,124 KB
testcase_21 AC 226 ms
8,180 KB
testcase_22 AC 220 ms
8,512 KB
testcase_23 AC 212 ms
9,188 KB
testcase_24 AC 200 ms
11,856 KB
testcase_25 AC 169 ms
14,716 KB
testcase_26 AC 247 ms
8,192 KB
testcase_27 AC 237 ms
11,164 KB
testcase_28 AC 218 ms
8,176 KB
testcase_29 AC 218 ms
8,200 KB
testcase_30 AC 221 ms
8,136 KB
testcase_31 AC 205 ms
10,920 KB
testcase_32 AC 209 ms
10,840 KB
testcase_33 AC 216 ms
11,324 KB
testcase_34 AC 226 ms
8,316 KB
testcase_35 AC 223 ms
8,200 KB
testcase_36 AC 218 ms
8,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;
template<typename T, bool ismin=true>
struct ConvexHullTrickMonotone{
	using P=pair<T, T>;
	deque<P> deq;
	ConvexHullTrickMonotone(){}
	void add(T a, T b){
		if(!ismin) a*=(-1), b*=(-1);
		if(deq.empty()){
			deq.push_back({a, b});
			return;
		}
		if(deq.back().first>=a){
			if(deq.back().first==a && deq.back().second<=b) return;
			while(deq.size()>1){
				T a1=deq.back().first, b1=deq.back().second;
				T a2=deq[deq.size()-2].first, b2=deq[deq.size()-2].second;
				if((b1-b2)*(a1-a)<(b-b1)*(a2-a1)) break;
				deq.pop_back();
			}
			deq.push_back({a, b});
		}else{
			while(deq.size()>1){
				T a1=deq.front().first, b1=deq.front().second;
				T a2=deq[1].first, b2=deq[1].second;
				if((b1-b2)*(a1-a)>(b-b1)*(a2-a1)) break;
				deq.pop_front();
			}
			deq.push_front({a, b});
		}
	}
	T get(T x, bool isinc){
		if(isinc){
			while(deq.size()>1){
				T a1=deq.front().first, b1=deq.front().second;
				T a2=deq[1].first, b2=deq[1].second;
				if(a1*x+b1<a2*x+b2) break;
				deq.pop_front();
			}
			T ret=deq.front().first*x+deq.front().second;
			if(ismin) return ret;
			else return -ret;
		}else{
			while(deq.size()>1){
				T a1=deq.back().first, b1=deq.back().second;
				T a2=deq[deq.size()-2].first, b2=deq[deq.size()-2].second;
				if(a1*x+b1<a2*x+b2) break;
				deq.pop_back();
			}
			T ret=deq.back().first*x+deq.back().second;
			if(ismin) return ret;
			else return -ret;
		}
	}
	void clear(){
		deq.clear();
	}
};
typedef __int128_t lll;
int n;
ll a[200020];
ll s[200020];
ll ans[200020];
void solve(int l, int r){
	if(l>r) return;
	if(l==r){
		ans[l]=min(ans[l], 1+a[l]);
		return;
	}
	int m=(l+r)/2;
	ConvexHullTrickMonotone<lll> cht;
	const ll INF=1e18;
	ll mn=INF;
	for(int i=m; i<=r; i++){
		cht.add(-2*(i+1), (ll)(i+1)*(i+1)+s[i+1]);
	}
	for(int i=l; i<=m; i++){
		mn=min(mn, (ll)cht.get(i, true)+(ll)i*i-s[i]);
		ans[i]=min(ans[i], mn);
	}

	cht.clear();
	mn=INF;
	for(int i=l; i<=m; i++){
		cht.add(-2*i, (ll)i*i-s[i]);
	}
	for(int i=r; i>=m; i--){
		mn=min(mn, (ll)cht.get(i+1, false)+(ll)(i+1)*(i+1)+s[i+1]);
		ans[i]=min(ans[i], mn);
	}
	solve(l, m-1);
	solve(m+1, r);
}
int main()
{
	cin>>n;
	s[0]=0;
    for(int i=0; i<n; i++){
        cin>>a[i];
        s[i+1]=s[i]+a[i];
    }
	const ll INF=1e18;
	fill(ans, ans+n, INF);
	solve(0, n-1);
	for(int i=0; i<n; i++){
		printf("%lld\n", ans[i]);
	}
    return 0;
}
0