結果

問題 No.913 木の燃やし方
ユーザー snukesnuke
提出日時 2019-10-18 21:38:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 447 ms / 3,000 ms
コード長 4,475 bytes
コンパイル時間 4,240 ms
コンパイル使用メモリ 159,552 KB
実行使用メモリ 12,744 KB
最終ジャッジ日時 2023-09-07 21:38:33
合計ジャッジ時間 20,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 6 ms
4,384 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 435 ms
12,520 KB
testcase_09 AC 419 ms
12,104 KB
testcase_10 AC 415 ms
12,024 KB
testcase_11 AC 423 ms
12,060 KB
testcase_12 AC 447 ms
12,404 KB
testcase_13 AC 429 ms
12,428 KB
testcase_14 AC 411 ms
12,168 KB
testcase_15 AC 411 ms
12,032 KB
testcase_16 AC 417 ms
12,368 KB
testcase_17 AC 401 ms
12,168 KB
testcase_18 AC 398 ms
12,076 KB
testcase_19 AC 408 ms
12,400 KB
testcase_20 AC 427 ms
12,508 KB
testcase_21 AC 417 ms
12,424 KB
testcase_22 AC 407 ms
12,584 KB
testcase_23 AC 402 ms
12,324 KB
testcase_24 AC 403 ms
12,744 KB
testcase_25 AC 353 ms
12,380 KB
testcase_26 AC 441 ms
12,532 KB
testcase_27 AC 411 ms
12,516 KB
testcase_28 AC 404 ms
12,444 KB
testcase_29 AC 404 ms
12,408 KB
testcase_30 AC 410 ms
12,404 KB
testcase_31 AC 388 ms
12,352 KB
testcase_32 AC 394 ms
12,388 KB
testcase_33 AC 399 ms
12,460 KB
testcase_34 AC 416 ms
12,400 KB
testcase_35 AC 410 ms
12,544 KB
testcase_36 AC 399 ms
12,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("YES");}else{puts("NO");}
const int MX = 200005;


// Segment tree (range add)
struct F { // min func
  typedef ll TT;
  TT d;
  F(TT d=LINF):d(d) {}
  F& operator+=(const F& f) {
    mins(d, f.d);
    return *this;
  }
  F operator+(const F& f) const { return F(*this) += f;}
};
struct seg {
  vector<F> d; int n;
  seg(){}
  seg(int mx) {
    n = 1; while (n < mx) n <<= 1;
    d = vector<F>(n<<1);
  }
  void upd(int i) { d[i] = d[i<<1]+d[i<<1|1];}
  F& operator[](int i) { return d[n+i];}
  void init() { for(int i=n-1;i;--i) upd(i);}
  F get(int i) {
    F x;
    for (i+=n;i;i>>=1) x += d[i];
    return x;
  }
  void add(int l, int r, F f) {
    // cerr<<l<<" "<<r<<" "<<f.d<<endl;
    l += n; r += n;
    while (l < r) {
      if (l&1) d[l] += f, ++l;
      if (r&1) --r, d[r] += f;
      l >>= 1; r >>= 1;
    }
  }
};
//
// Convex Hull Trick
typedef ll LT;
typedef pair<LT,LT> LP;
struct CHT {
  deque<LP> d;
  LT get(LT x) {
    while (sz(d) >= 2) {
      LT a = d[0].fi*x + d[0].se;
      LT b = d[1].fi*x + d[1].se;
      if (a <= b) break; // (min)
      d.pop_front();
    }
    return d[0].fi*x + d[0].se;
  }
  void add(LP x) { // desc of x.fi (min)
    while (sz(d) >= 2) {
      LP y = d[sz(d) - 1];
      LP z = d[sz(d) - 2];
      if ((x.se-y.se)*(z.fi-y.fi) > (y.fi-x.fi)*(y.se-z.se)) break; // both
      d.pop_back();
    }
    d.pb(x);
  }
};
//

int n;
vl a;
seg t;

void dfs(int l, int r) {
  if (r-l == 0) return;
  if (r-l == 1) {
    t.add(l,r,F(a[l]+1));
    return;
  }
  int c = (l+r)/2;
  {
    ll s = 0;
    v(LP) ls;
    for (int i = c; i >= l; --i) {
      if (i != c) s += a[i];
      ls.eb(-2*(i-1),s+ll(i-1)*(i-1));
    }
    CHT d;
    drep(i,sz(ls)) d.add(ls[i]);
    s = 0;
    for (int i = c; i < r; ++i) {
      s += a[i];
      ll now = d.get(i) + (ll)i*i + s;
      t.add(c,i+1,F(now));
    }
  }
  {
    ll s = 0;
    v(LP) ls;
    for (int i = c-1; i < r; ++i) {
      if (i != c-1) s += a[i];
      ls.eb(-2*(n-i),s+ll(n-i)*(n-i));
    }
    CHT d;
    drep(i,sz(ls)) d.add(ls[i]);
    s = 0;
    for (int i = c-1; i >= l; --i) {
      s += a[i];
      int ni = n-i+1;
      ll now = d.get(ni) + (ll)ni*ni + s;
      t.add(i,c,F(now));
    }
  }
  dfs(l,c);
  dfs(c,r);
}

int main() {
  scanf("%d",&n);
  a = vl(n);
  cin>>a;
  t = seg(n);
  dfs(0,n);
  rep(i,n) {
    ll ans = t.get(i).d;
    printf("%lld\n",ans);
  }
  return 0;
}




















0