結果

問題 No.3407 Birds-of-Paradise' Christmas Live
コンテスト
ユーザー Kude
提出日時 2025-12-14 00:27:26
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 4,285 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,129 ms
コンパイル使用メモリ 308,188 KB
実行使用メモリ 9,276 KB
最終ジャッジ日時 2025-12-14 00:27:33
合計ジャッジ時間 5,486 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

// https://ei1333.github.io/library/structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp

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(); }

  static constexpr int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

  static constexpr T floor_div(T n, T d) {
    return n / d - ((n ^ d) < 0 and n % d != 0);
  }

  static constexpr 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);
    if constexpr (is_integral<T>::value) {
      return floor_div(b.S - a.S, a.F - b.F) >= floor_div(c.S - b.S, b.F - c.F);
    } else {
      return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >=
             (c.F - b.F) * sgn(b.S - a.S) / 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);
    }
  }

  static constexpr T get_y(const P& a, const T& x) { return a.F * x + a.S; }

  T query(T x) const {
    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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n;
  VI w(n + 1);
  rep(i, n) cin >> w[i + 1];
  rep(i, n) w[i+1] += w[i];
  cin >> m;
  VI s(m + 1);
  rep(i, m) cin >> s[i + 1];
  rep(i, m) s[i+1] += s[i];
  VI d;
  merge(all(s), all(w), back_inserter(d));
  d.erase(unique(all(d)), d.end());
  int sz = d.size();
  VI a(sz-1);
  rep(z, 2) {
    int p = 0;
    rep(i, n) {
      while (d[p] < w[i]) p++;
      if (i % 2) continue;
      while (d[p] < w[i+1]) a[p++] |= 1 << z;
    }
    swap(w, s);
    swap(n, m);
  }
  ConvexHullTrickAddMonotone<ll, true> cht0, cht1;
  ll now = 0;
  rep(i, sz - 1) {
    ll d2 = (ll)d[i] * d[i];
    if (!(a[i] & 1)) cht0.clear();
    if (!(a[i] & 2)) cht1.clear();
    if (a[i] & 1) cht0.add(2 * d[i], now - d2);
    if (a[i] & 2) cht1.add(2 * d[i], now - d2);
    ll nd2 = (ll)d[i+1] * d[i+1];
    if (!cht0.empty()) chmin(now, cht0.query(d[i+1]) - nd2);
    if (!cht1.empty()) chmin(now, cht1.query(d[i+1]) - nd2);
  }
  cout << -now << '\n';
}
0