結果

問題 No.2332 Make a Sequence
ユーザー 👑 NachiaNachia
提出日時 2023-05-28 14:00:36
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 2,690 bytes
コンパイル時間 3,619 ms
コンパイル使用メモリ 106,188 KB
実行使用メモリ 15,460 KB
最終ジャッジ日時 2024-12-26 22:45:49
合計ジャッジ時間 14,900 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#include <atcoder/string>
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

using Modint = atcoder::static_modint<998244353>;

struct LiChaoTree{
  using Val = i64;
  struct Func {
    i64 a, b;
    Val operator()(i64 x) { return a * x + b; }
  };
  static Func infFunc() { return { 0,INF }; }
 
  int N;
  vector<Func> V;
  vector<bool> visited;
  
  LiChaoTree(i64 n){
    N = 1;
    while(N < n) N *= 2;
    V.assign(N*2, infFunc());
    visited.assign(N*2, false);
  }
 
  void addSegment(i64 l, i64 r, Func f){
    if(l >= r) return;
    auto dfs = [&,this](int i,Func f,i64 a,i64 b,auto dfs) -> void {
      visited[i] = true;
      if(i >= (i64)V.size()) return;
      if(r <= a || b <= l) return;
      i64 m = (a+b)/2;
      if(!(l <= a && b <= r)){
        dfs(i*2,f,a,m,dfs);
        dfs(i*2+1,f,m,b,dfs);
        return;
      }
      bool greatf_l = !(V[i](a) < f(a));
      bool greatf_r = !(V[i](b-1) < f(b-1));
      if(!greatf_l && !greatf_r) return;
      if(greatf_l && greatf_r){ V[i] = f; return; }
      if(a + 1 == b) return;
      if(!(V[i](m) < f(m))){
        swap(V[i],f);
        swap(greatf_l,greatf_r);
      }
      if(greatf_l) dfs(i*2,f,a,m,dfs);
      else dfs(i*2+1,f,m,b,dfs);
    };
    dfs(1,f,0,N,dfs);
  }
  
  void addLine(Func f){
    addSegment(0,N,f);
  }
 
  Val minVal(i64 p){
    int i = 1;
    Val res = infFunc()(p);
    int l = 0, r = N;
    while(i < (i64)V.size()){
      if(!visited[i]) break;
      res = min(res,V[i](p));
      i64 m = (l+r)/2;
      if(p < m){ i = i*2; r = m; }
      else{ i = i*2+1; l = m; }
    }
    return res;
  }
};

int main(){
    int N, M; cin >> N >> M;
    vector<int> A(N); rep(i,N) cin >> A[i];
    vector<int> B(M); rep(i,M) cin >> B[i];
    vector<i64> C(M); rep(i,M) cin >> C[i];
    vector<int> L(M); {
        vector<int> AB(N+M);
        rep(i,N) AB[i] = A[i];
        rep(i,M) AB[N+i] = B[i];
        auto Z = atcoder::z_algorithm(AB);
        rep(i,M) L[i] = min(Z[N+i], N);
    }
    LiChaoTree T(M+1);
    T.addSegment(0, 1, {0,0});
    for(int l=0; l<M; l++){
        if(L[l] == 0) continue;
        i64 cost = T.minVal(l);
        T.addSegment(l+1, l+L[l]+1, { C[l], cost-C[l]*l });
    }
    i64 ans = T.minVal(M);
    if(ans == INF) ans = -1;
    cout << ans << endl;
    return 0;
}



struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

0