結果

問題 No.913 木の燃やし方
ユーザー chocoruskchocorusk
提出日時 2019-10-19 06:45:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 3,000 ms
コード長 2,628 bytes
コンパイル時間 1,386 ms
コンパイル使用メモリ 118,980 KB
実行使用メモリ 11,860 KB
最終ジャッジ日時 2023-09-08 15:28:53
合計ジャッジ時間 14,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,644 KB
testcase_01 AC 2 ms
5,536 KB
testcase_02 AC 2 ms
5,520 KB
testcase_03 AC 5 ms
5,600 KB
testcase_04 AC 4 ms
5,616 KB
testcase_05 AC 4 ms
5,624 KB
testcase_06 AC 4 ms
5,628 KB
testcase_07 AC 2 ms
5,560 KB
testcase_08 AC 217 ms
10,780 KB
testcase_09 AC 209 ms
10,376 KB
testcase_10 AC 208 ms
10,728 KB
testcase_11 AC 214 ms
10,616 KB
testcase_12 AC 226 ms
10,944 KB
testcase_13 AC 217 ms
10,792 KB
testcase_14 AC 210 ms
10,480 KB
testcase_15 AC 209 ms
10,464 KB
testcase_16 AC 203 ms
10,756 KB
testcase_17 AC 196 ms
10,428 KB
testcase_18 AC 196 ms
10,428 KB
testcase_19 AC 190 ms
11,168 KB
testcase_20 AC 212 ms
10,832 KB
testcase_21 AC 193 ms
10,824 KB
testcase_22 AC 184 ms
10,828 KB
testcase_23 AC 178 ms
11,008 KB
testcase_24 AC 164 ms
11,048 KB
testcase_25 AC 134 ms
11,148 KB
testcase_26 AC 228 ms
10,992 KB
testcase_27 AC 189 ms
11,740 KB
testcase_28 AC 185 ms
10,852 KB
testcase_29 AC 183 ms
10,916 KB
testcase_30 AC 189 ms
10,884 KB
testcase_31 AC 171 ms
11,724 KB
testcase_32 AC 176 ms
11,860 KB
testcase_33 AC 182 ms
11,676 KB
testcase_34 AC 198 ms
10,884 KB
testcase_35 AC 193 ms
10,852 KB
testcase_36 AC 182 ms
10,972 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;
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;
	deque<int> cht;
	for(int i=r; i>=m; i--){
		ll a0=-2*(i+1), b0=(ll)(i+1)*(i+1)+s[i+1];
		while(cht.size()>1){
            int i1=cht[cht.size()-1], i2=cht[cht.size()-2];
            ll a1=-2*i1, b1=(ll)i1*i1+s[i1];
            ll a2=-2*i2, b2=(ll)i2*i2+s[i2];
            if((lll)(b1-b2)*(a1-a0)>(lll)(b0-b1)*(a2-a1)) break;
            cht.pop_back();
        }
        cht.push_back(i+1);
	}
	vector<ll> vm1(m-l+1), vm2(r-m+1);
	for(int i=m; i>=l; i--){
		while(cht.size()>1){
            int i1=cht[0], i2=cht[1];
            ll a1=-2*i1, b1=(ll)i1*i1+s[i1];
            ll a2=-2*i2, b2=(ll)i2*i2+s[i2];
            if(a1*(ll)i+b1<a2*(ll)i+b2) break;
            cht.pop_front();
        }
		int r=cht[0];
		vm1[i-l]=(ll)(r-i)*(r-i)+s[r]-s[i];
	}
	ll mn=1e18;
	for(int i=l; i<=m; i++){
		mn=min(mn, vm1[i-l]);
		ans[i]=min(ans[i], mn);
	}

	cht.clear();
	for(int i=l; i<=m; i++){
		ll a0=-2*i, b0=(ll)i*i-s[i];
		while(cht.size()>1){
            int i1=cht[cht.size()-1], i2=cht[cht.size()-2];
            ll a1=-2*i1, b1=(ll)i1*i1-s[i1];
            ll a2=-2*i2, b2=(ll)i2*i2-s[i2];
            if((lll)(b1-b2)*(a1-a0)<(lll)(b0-b1)*(a2-a1)) break;
            cht.pop_back();
        }
        cht.push_back(i);
	}
	for(int i=m; i<=r; i++){
		while(cht.size()>1){
            int i1=cht[0], i2=cht[1];
            ll a1=-2*i1, b1=(ll)i1*i1-s[i1];
            ll a2=-2*i2, b2=(ll)i2*i2-s[i2];
            if(a1*(ll)(i+1)+b1<a2*(ll)(i+1)+b2) break;
            cht.pop_front();
        }
		int l=cht[0];
		vm2[i-m]=(ll)(i+1-l)*(i+1-l)+s[i+1]-s[l];
	}
	mn=1e18;
	for(int i=r; i>=m; i--){
		mn=min(mn, vm2[i-m]);
		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