結果

問題 No.2332 Make a Sequence
ユーザー momoyuu
提出日時 2023-05-28 15:16:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 638 ms / 2,000 ms
コード長 4,456 bytes
コンパイル時間 2,991 ms
コンパイル使用メモリ 253,864 KB
実行使用メモリ 36,224 KB
最終ジャッジ日時 2024-12-27 05:49:46
合計ジャッジ時間 35,573 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

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