結果

問題 No.2332 Make a Sequence
ユーザー kotatsugamekotatsugame
提出日時 2023-05-28 14:51:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 3,202 bytes
コンパイル時間 1,556 ms
コンパイル使用メモリ 95,972 KB
実行使用メモリ 27,552 KB
最終ジャッジ日時 2023-08-27 10:45:19
合計ジャッジ時間 23,441 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 270 ms
23,436 KB
testcase_08 AC 72 ms
5,720 KB
testcase_09 AC 213 ms
18,012 KB
testcase_10 AC 230 ms
22,544 KB
testcase_11 AC 156 ms
15,748 KB
testcase_12 AC 329 ms
27,248 KB
testcase_13 AC 329 ms
27,544 KB
testcase_14 AC 328 ms
27,256 KB
testcase_15 AC 327 ms
27,552 KB
testcase_16 AC 327 ms
27,388 KB
testcase_17 AC 328 ms
27,192 KB
testcase_18 AC 328 ms
27,332 KB
testcase_19 AC 329 ms
27,312 KB
testcase_20 AC 329 ms
27,260 KB
testcase_21 AC 332 ms
27,196 KB
testcase_22 AC 328 ms
27,328 KB
testcase_23 AC 328 ms
27,396 KB
testcase_24 AC 328 ms
27,192 KB
testcase_25 AC 328 ms
27,200 KB
testcase_26 AC 330 ms
27,256 KB
testcase_27 AC 326 ms
26,904 KB
testcase_28 AC 327 ms
26,668 KB
testcase_29 AC 327 ms
26,868 KB
testcase_30 AC 328 ms
26,996 KB
testcase_31 AC 328 ms
26,724 KB
testcase_32 AC 325 ms
25,488 KB
testcase_33 AC 327 ms
25,352 KB
testcase_34 AC 325 ms
25,484 KB
testcase_35 AC 326 ms
25,720 KB
testcase_36 AC 326 ms
25,352 KB
testcase_37 AC 331 ms
26,520 KB
testcase_38 AC 329 ms
26,400 KB
testcase_39 AC 329 ms
26,472 KB
testcase_40 AC 330 ms
26,404 KB
testcase_41 AC 326 ms
26,668 KB
testcase_42 AC 332 ms
25,144 KB
testcase_43 AC 332 ms
25,352 KB
testcase_44 AC 332 ms
25,356 KB
testcase_45 AC 326 ms
25,824 KB
testcase_46 AC 333 ms
25,556 KB
testcase_47 AC 329 ms
24,224 KB
testcase_48 AC 330 ms
24,236 KB
testcase_49 AC 331 ms
24,164 KB
testcase_50 AC 330 ms
24,400 KB
testcase_51 AC 329 ms
24,232 KB
testcase_52 AC 327 ms
26,724 KB
testcase_53 AC 330 ms
26,860 KB
testcase_54 AC 328 ms
26,712 KB
testcase_55 AC 329 ms
27,032 KB
testcase_56 AC 328 ms
26,884 KB
testcase_57 AC 234 ms
10,036 KB
testcase_58 AC 227 ms
8,644 KB
testcase_59 AC 225 ms
9,116 KB
testcase_60 AC 230 ms
9,236 KB
testcase_61 AC 226 ms
8,656 KB
testcase_62 AC 354 ms
25,548 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:132:9: 警告: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
  132 |         if(ans==(long)1e18)ans=-1;
      |         ^~
main.cpp:121:14: 備考: ‘ans’ はここで定義されています
  121 |         long ans;
      |              ^~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<atcoder/string>
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,M;
int A[2<<17],B[2<<17],C[2<<17];
int main()
{
	cin>>N>>M;
	vector<int>S;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		S.push_back(A[i]);
	}
	for(int i=0;i<M;i++)
	{
		cin>>B[i];
		S.push_back(B[i]);
	}
	for(int i=0;i<M;i++)cin>>C[i];
	vector<int>T=atcoder::z_algorithm(S);
	DynamicLiChaoTree<long,0L,(long)2e5,(long)1e18>Q;
	Q.add_segment(0,1,0,0);
	long ans;
	for(int i=0;i<=M;i++)
	{
		ans=Q.query(i);
		if(i<M&&ans<(long)1e18)
		{
			int len=min(T[N+i],N);
			//cout<<i<<"->"<<i+len<<" : "<<ans+C[i]<<endl;
			Q.add_segment(i,i+len+1,C[i],ans-(long)i*C[i]);
		}
	}
	if(ans==(long)1e18)ans=-1;
	cout<<ans<<endl;
}
0