結果

問題 No.2458 Line Up Charged Balls
ユーザー kotatsugamekotatsugame
提出日時 2023-09-01 22:17:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,889 bytes
コンパイル時間 682 ms
コンパイル使用メモリ 67,824 KB
実行使用メモリ 7,876 KB
最終ジャッジ日時 2023-09-01 22:17:13
合計ジャッジ時間 3,447 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,612 KB
testcase_01 AC 2 ms
5,524 KB
testcase_02 AC 2 ms
5,480 KB
testcase_03 AC 2 ms
5,444 KB
testcase_04 AC 2 ms
5,460 KB
testcase_05 AC 37 ms
7,868 KB
testcase_06 AC 36 ms
7,620 KB
testcase_07 AC 2 ms
5,408 KB
testcase_08 AC 2 ms
5,436 KB
testcase_09 AC 41 ms
7,576 KB
testcase_10 AC 13 ms
6,104 KB
testcase_11 AC 3 ms
5,624 KB
testcase_12 AC 2 ms
5,476 KB
testcase_13 AC 26 ms
6,688 KB
testcase_14 AC 23 ms
6,544 KB
testcase_15 AC 14 ms
6,344 KB
testcase_16 AC 65 ms
7,680 KB
testcase_17 AC 71 ms
7,616 KB
testcase_18 AC 66 ms
7,636 KB
testcase_19 AC 59 ms
7,612 KB
testcase_20 AC 57 ms
7,736 KB
testcase_21 AC 39 ms
7,612 KB
testcase_22 AC 46 ms
7,624 KB
testcase_23 AC 51 ms
7,632 KB
testcase_24 AC 54 ms
7,640 KB
testcase_25 AC 52 ms
7,620 KB
testcase_26 AC 45 ms
7,868 KB
testcase_27 AC 49 ms
7,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cassert>
using namespace std;
/**
 * @brief Dynamic-Li-Chao-Tree
 * @docs docs/dynamic-li-chao-tree.md
*/
template< typename T, T x_low, T x_high, T id >
struct DynamicLiChaoTree {

  struct Line {
    T a, b;

    Line(T a, T b) : a(a), b(b) {}

    inline T get(T x) const { return a * x + b; }
  };

  struct Node {
    Line x;
    Node *l, *r;

    Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {}
  };

  Node *root;

  DynamicLiChaoTree() : root{nullptr} {}

  Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) {
    if(!t) return new Node(x);

    T t_l = t->x.get(l), t_r = t->x.get(r);

    if(t_l <= x_l && t_r <= x_r) {
      return t;
    } else if(t_l >= x_l && t_r >= x_r) {
      t->x = x;
      return t;
    } else {
      T m = (l + r) / 2;
      if(m == r) --m;
      T t_m = t->x.get(m), x_m = x.get(m);
      if(t_m > x_m) {
        swap(t->x, x);
        if(x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m);
        else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
      } else {
        if(t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m);
        else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
      }
      return t;
    }
  }

  void add_line(const T &a, const T &b) {
    Line x(a, b);
    root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) {
    if(r < a || b < l) return t;
    if(a <= l && r <= b) {
      Line y{x};
      return add_line(t, y, l, r, x_l, x_r);
    }
    if(t) {
      T t_l = t->x.get(l), t_r = t->x.get(r);
      if(t_l <= x_l && t_r <= x_r) return t;
    } else {
      t = new Node(Line(0, id));
    }
    T m = (l + r) / 2;
    if(m == r) --m;
    T x_m = x.get(m);
    t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
    t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);
    return t;
  }

  void add_segment(const T &l, const T &r, const T &a, const T &b) {
    Line x(a, b);
    root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  T query(const Node *t, const T &l, const T &r, const T &x) const {
    if(!t) return id;
    if(l == r) return t->x.get(x);
    T m = (l + r) / 2;
    if(m == r) --m;
    if(x <= m) return min(t->x.get(x), query(t->l, l, m, x));
    else return min(t->x.get(x), query(t->r, m + 1, r, x));
  }

  T query(const T &x) const {
    return query(root, x_low, x_high, x);
  }
};
int N,Q[3<<17];
long dp[3<<17];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N;
	for(int i=0;i<N;i++)cin>>Q[i];
	DynamicLiChaoTree<long,(long)-1e5,(long)1e5,(long)9e18>seg;
	long ans=0;
	for(int i=0;i<N;i++)
	{
		dp[i]=max(dp[i],-seg.query(Q[i]));
		ans=max(ans,dp[i]);
		seg.add_line(-Q[i],-dp[i]);
	}
	cout<<ans<<endl;
}
0