結果

問題 No.913 木の燃やし方
ユーザー ei1333333ei1333333
提出日時 2019-10-18 01:46:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 450 ms / 3,000 ms
コード長 5,225 bytes
コンパイル時間 2,462 ms
コンパイル使用メモリ 209,920 KB
実行使用メモリ 9,388 KB
最終ジャッジ日時 2023-09-07 20:21:46
合計ジャッジ時間 18,881 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 7 ms
4,380 KB
testcase_04 AC 7 ms
4,384 KB
testcase_05 AC 7 ms
4,384 KB
testcase_06 AC 5 ms
4,384 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 439 ms
7,568 KB
testcase_09 AC 421 ms
7,604 KB
testcase_10 AC 426 ms
7,636 KB
testcase_11 AC 425 ms
7,416 KB
testcase_12 AC 441 ms
7,680 KB
testcase_13 AC 404 ms
7,624 KB
testcase_14 AC 391 ms
7,456 KB
testcase_15 AC 406 ms
7,568 KB
testcase_16 AC 420 ms
7,568 KB
testcase_17 AC 407 ms
7,588 KB
testcase_18 AC 408 ms
7,272 KB
testcase_19 AC 378 ms
9,268 KB
testcase_20 AC 440 ms
7,680 KB
testcase_21 AC 450 ms
7,728 KB
testcase_22 AC 448 ms
7,680 KB
testcase_23 AC 438 ms
8,016 KB
testcase_24 AC 414 ms
8,436 KB
testcase_25 AC 365 ms
9,216 KB
testcase_26 AC 448 ms
7,720 KB
testcase_27 AC 402 ms
9,388 KB
testcase_28 AC 427 ms
7,684 KB
testcase_29 AC 424 ms
7,796 KB
testcase_30 AC 427 ms
7,896 KB
testcase_31 AC 372 ms
9,268 KB
testcase_32 AC 385 ms
9,196 KB
testcase_33 AC 378 ms
9,204 KB
testcase_34 AC 440 ms
7,844 KB
testcase_35 AC 424 ms
7,856 KB
testcase_36 AC 440 ms
7,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

const int mod = 998244353;

using int64 = long long;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

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 main() {
  int N;
  cin >> N;
  vector< int64 > A(N);
  cin >> A;
  vector< int64 > S(N + 1);
  for(int i = 1; i <= N; i++) S[i] = S[i - 1] + A[i - 1];

  vector< int64 > ans(N, infll);
  auto rec = MFP([&](auto rec, int l, int r) -> void {
    if(l >= r) return;
    int md = (l + r) / 2;
    rec(l, md);
    rec(md + 1, r);


    if(l < md) {
      ConvexHullTrickAddMonotone< int64, true > cht;
      for(int64 i = md + 1; i <= r; i++) {
        cht.add(-2 * i, i * i + S[i]);
      }
      int64 ret = infll;
      for(int64 i = l; i < md; i++) {
        chmin(ret, cht.query_monotone_inc(i) + i * i - S[i]);
        chmin(ans[i], ret);
      }
    }

    if(md < r) {
      ConvexHullTrickAddMonotone< int64, true > cht;
      for(int64 i = l; i <= md; i++) {
        cht.add(-2 * i, i * i - S[i]);
      }
      int64 ret = infll;
      for(int64 i = r; i > md; i--) {
        chmin(ret, cht.query_monotone_dec(i) + i * i + S[i]);
        chmin(ans[i - 1], ret);
      }
    }
  });
  rec(0, N);
  for(int i = 0; i < N; i++) cout << ans[i] << endl;
}
0