結果

問題 No.913 木の燃やし方
ユーザー ei1333333ei1333333
提出日時 2019-10-02 20:06:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,199 bytes
コンパイル時間 4,067 ms
コンパイル使用メモリ 213,084 KB
実行使用メモリ 17,444 KB
最終ジャッジ日時 2023-09-07 20:17:33
合計ジャッジ時間 17,845 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 2 ms
4,376 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 309 ms
12,712 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;

const int mod = 1e9 + 7;

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 OperatorMonoid >
struct DuelSegmentTree {
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< OperatorMonoid > lazy;
  const H h;
  const OperatorMonoid OM0;


  DuelSegmentTree(int n, const H h, const OperatorMonoid OM0)
      : h(h), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    lazy.assign(2 * sz, OM0);
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      lazy[k] = OM0;
    }
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
  }

  OperatorMonoid operator[](int k) {
    thrust(k += sz);
    return lazy[k];
  }
};

template< typename T, bool isMin >
struct ConvexHullTrickWithIndex {
  struct P {
    T m, b;
    int idx;

    P(T m, T b, int idx) : m(m), b(b), idx(idx) {};

    bool operator<(const P &a) {
      return m != a.m ? m < a.m : b < a.b;
    }
  };

  deque< P > H;

  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.b == a.b || c.b == b.b)
      return sgn(b.m - a.m) * sgn(c.b - b.b) >= sgn(c.m - b.m) * sgn(b.b - a.b);
    return D(b.m - a.m) * sgn(c.b - b.b) / D(abs(b.b - a.b))
           >= D(c.m - b.m) * sgn(b.b - a.b) / D(abs(c.b - b.b));
  }

  void addLine(T m, T b, int idx) {
    if(!isMin) m *= -1, b *= -1;
    P line(m, b, idx);
    if(empty()) {
      H.emplace_front(line);
      return;
    }

    if(empty() || H.front().m <= m) {
      if(H.front().m == m) {
        if(H.front().b <= b) return;
        H.pop_front();
      }
      while(H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
      H.emplace_front(line);
    } else {
      assert(m <= H.back().m);
      if(H.back().m == m) {
        if(H.back().b <= 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 pair< T, int > getY(const P &a, const T &x) {
    return make_pair(a.m * x + a.b, a.idx);
  }

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

  pair< T, int > queryMonotoneInc(T x) {
    assert(!empty());
    while(H.size() >= 2 && getY(H.front(), x) >= getY(H[1], x)) H.pop_front();
    if(isMin) return getY(H.front(), x);
    return make_pair(-getY(H.front(), x).first, H.front().idx);
  }

  pair< T, int > queryMonotoneDec(T x) {
    assert(!empty());
    while(H.size() >= 2 && getY(H.back(), x) >= getY(H[H.size() - 2], x)) H.pop_back();
    if(isMin) return getY(H.back(), x);
    return make_pair(-getY(H.back(), x).first, H.back().idx);
  }
};

int main() {
  int N;
  cin >> N;
  vector< int > A(N);
  cin >> A;

  auto beet = [&]() {
    auto f = [](int64 a, int64 b) { return min(a, b); };
    DuelSegmentTree< int64 > seg(N, f, infll);
    vector< int64 > S(N + 1);
    for(int i = 1; i <= N; i++) S[i] = S[i - 1] + A[i - 1];
    ConvexHullTrickWithIndex< int64, true > cht;
    for(int64 i = 0; i <= N; i++) {
      if(i > 0) {
        auto ret = cht.queryMonotoneInc(i);
        seg.update(ret.second, i, ret.first + i * i - S[i]);
      }
      cht.addLine(-2 * i, i * i + S[i], i);
    }
    vector< int64 > ret(N);
    for(int i = 0; i < N; i++) ret[i] = seg[i];
    return ret;
  };
  auto v = beet();
  reverse(begin(A), end(A));
  auto s = beet();
  reverse(begin(s), end(s));


  for(int i = 0; i < N; i++) {
    cout << min(v[i], s[i]) << endl;
  }
}

0