結果

問題 No.2332 Make a Sequence
ユーザー momoyuumomoyuu
提出日時 2023-05-28 15:16:58
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 568 ms / 2,000 ms
コード長 4,456 bytes
コンパイル時間 2,908 ms
コンパイル使用メモリ 253,424 KB
実行使用メモリ 36,224 KB
最終ジャッジ日時 2024-06-08 07:25:57
合計ジャッジ時間 32,920 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 350 ms
18,432 KB
testcase_08 AC 86 ms
10,240 KB
testcase_09 AC 272 ms
16,256 KB
testcase_10 AC 291 ms
13,824 KB
testcase_11 AC 200 ms
10,752 KB
testcase_12 AC 432 ms
22,016 KB
testcase_13 AC 425 ms
22,144 KB
testcase_14 AC 421 ms
22,016 KB
testcase_15 AC 433 ms
22,016 KB
testcase_16 AC 418 ms
22,016 KB
testcase_17 AC 426 ms
22,144 KB
testcase_18 AC 418 ms
22,016 KB
testcase_19 AC 431 ms
22,016 KB
testcase_20 AC 424 ms
22,016 KB
testcase_21 AC 428 ms
22,144 KB
testcase_22 AC 436 ms
22,144 KB
testcase_23 AC 433 ms
22,144 KB
testcase_24 AC 443 ms
22,144 KB
testcase_25 AC 441 ms
22,144 KB
testcase_26 AC 434 ms
22,272 KB
testcase_27 AC 442 ms
23,040 KB
testcase_28 AC 447 ms
23,040 KB
testcase_29 AC 440 ms
23,040 KB
testcase_30 AC 433 ms
23,168 KB
testcase_31 AC 427 ms
23,040 KB
testcase_32 AC 460 ms
27,392 KB
testcase_33 AC 455 ms
27,520 KB
testcase_34 AC 463 ms
27,520 KB
testcase_35 AC 449 ms
27,520 KB
testcase_36 AC 442 ms
27,392 KB
testcase_37 AC 460 ms
26,880 KB
testcase_38 AC 455 ms
27,392 KB
testcase_39 AC 452 ms
28,672 KB
testcase_40 AC 448 ms
26,880 KB
testcase_41 AC 440 ms
26,112 KB
testcase_42 AC 492 ms
34,560 KB
testcase_43 AC 500 ms
34,560 KB
testcase_44 AC 501 ms
34,304 KB
testcase_45 AC 489 ms
35,200 KB
testcase_46 AC 489 ms
35,200 KB
testcase_47 AC 557 ms
36,224 KB
testcase_48 AC 553 ms
36,096 KB
testcase_49 AC 567 ms
36,224 KB
testcase_50 AC 564 ms
36,096 KB
testcase_51 AC 568 ms
36,096 KB
testcase_52 AC 445 ms
24,192 KB
testcase_53 AC 444 ms
23,424 KB
testcase_54 AC 441 ms
23,040 KB
testcase_55 AC 443 ms
23,808 KB
testcase_56 AC 454 ms
24,320 KB
testcase_57 AC 438 ms
23,168 KB
testcase_58 AC 435 ms
23,168 KB
testcase_59 AC 429 ms
23,296 KB
testcase_60 AC 435 ms
23,808 KB
testcase_61 AC 442 ms
23,168 KB
testcase_62 AC 512 ms
32,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

/**
 * @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);
  }
};

const ll m2 = 1000000007;
const ll b2 = 1009;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(){};
    rhash(vector<ll> &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    vector<ll> a(n),b(m),c(m);
    for(int i = 0;i<n;i++) cin>>a[i];
    for(int i = 0;i<m;i++) cin>>b[i];
    for(int i = 0;i<m;i++) cin>>c[i];
    vector<rhash> rs(2);
    vector<rhash> rrs(2);
    rs[0] = rhash(a,m2,b2);
    rs[1] = rhash(a,m3,b3);
    rrs[0] = rhash(b,m2,b2);
    rrs[1] = rhash(b,m3,b3);
    vector<ll> dp(m+1,1e16);
    dp[0] = 0;
    DynamicLiChaoTree<ll,0ll,(ll)2<<17,(ll)1e18> li;
    for(int i = 0;i<=m;i++){
        if(i!=0) dp[i] = min(dp[i],li.query(i));
        if(i==m) break;
        int right = min(m-i,n) + 1;
        int left = 0;
        while(right-left>1){
            int mid = (right+left)/2;
            int ri = i + mid;
            bool ok = true;
            for(int j = 0;j<2;j++){
                if(rs[j].get(0,mid)!=rrs[j].get(i,ri)) ok = false;
            }
            if(ok) left = mid;
            else right = mid;
        }
        //cout<<i<<" "<<left<<endl;
        //cout<<dp[i]<<endl;
        if(left==0) continue;
        li.add_segment(i,i+right,c[i],dp[i]-i*c[i]);
    }
    if(dp[m]==1e16) cout<<-1<<endl;
    else cout<<dp[m]<<endl;


}
0