結果

問題 No.2332 Make a Sequence
ユーザー 👑 NachiaNachia
提出日時 2023-05-28 14:00:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 2,690 bytes
コンパイル時間 2,444 ms
コンパイル使用メモリ 105,656 KB
実行使用メモリ 15,620 KB
最終ジャッジ日時 2023-08-27 08:48:56
合計ジャッジ時間 12,065 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 65 ms
14,660 KB
testcase_08 AC 24 ms
5,120 KB
testcase_09 AC 53 ms
9,716 KB
testcase_10 AC 51 ms
14,216 KB
testcase_11 AC 35 ms
8,972 KB
testcase_12 AC 78 ms
15,416 KB
testcase_13 AC 78 ms
15,340 KB
testcase_14 AC 78 ms
15,420 KB
testcase_15 AC 78 ms
15,292 KB
testcase_16 AC 78 ms
15,308 KB
testcase_17 AC 78 ms
15,432 KB
testcase_18 AC 78 ms
15,296 KB
testcase_19 AC 78 ms
15,300 KB
testcase_20 AC 78 ms
15,360 KB
testcase_21 AC 78 ms
15,420 KB
testcase_22 AC 78 ms
15,356 KB
testcase_23 AC 78 ms
15,288 KB
testcase_24 AC 78 ms
15,360 KB
testcase_25 AC 78 ms
15,420 KB
testcase_26 AC 79 ms
15,312 KB
testcase_27 AC 79 ms
15,364 KB
testcase_28 AC 80 ms
15,296 KB
testcase_29 AC 80 ms
15,308 KB
testcase_30 AC 81 ms
15,420 KB
testcase_31 AC 80 ms
15,360 KB
testcase_32 AC 88 ms
15,620 KB
testcase_33 AC 89 ms
15,424 KB
testcase_34 AC 89 ms
15,432 KB
testcase_35 AC 89 ms
15,420 KB
testcase_36 AC 88 ms
15,420 KB
testcase_37 AC 90 ms
15,304 KB
testcase_38 AC 89 ms
15,480 KB
testcase_39 AC 93 ms
15,304 KB
testcase_40 AC 88 ms
15,416 KB
testcase_41 AC 87 ms
15,256 KB
testcase_42 AC 132 ms
15,232 KB
testcase_43 AC 129 ms
15,392 KB
testcase_44 AC 124 ms
15,460 KB
testcase_45 AC 133 ms
15,244 KB
testcase_46 AC 130 ms
15,248 KB
testcase_47 AC 229 ms
15,360 KB
testcase_48 AC 230 ms
15,300 KB
testcase_49 AC 231 ms
15,296 KB
testcase_50 AC 230 ms
15,308 KB
testcase_51 AC 230 ms
15,460 KB
testcase_52 AC 82 ms
15,424 KB
testcase_53 AC 81 ms
15,296 KB
testcase_54 AC 79 ms
15,284 KB
testcase_55 AC 82 ms
15,364 KB
testcase_56 AC 82 ms
15,340 KB
testcase_57 AC 80 ms
15,344 KB
testcase_58 AC 80 ms
15,300 KB
testcase_59 AC 80 ms
15,344 KB
testcase_60 AC 81 ms
15,300 KB
testcase_61 AC 80 ms
15,232 KB
testcase_62 AC 175 ms
15,296 KB
権限があれば一括ダウンロードができます

ソースコード

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