結果

問題 No.2332 Make a Sequence
ユーザー navel_tosnavel_tos
提出日時 2024-02-25 21:49:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,862 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 204,096 KB
最終ジャッジ日時 2024-02-25 21:49:44
合計ジャッジ時間 30,787 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 38 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 211 ms
122,668 KB
testcase_08 AC 103 ms
103,548 KB
testcase_09 AC 182 ms
125,564 KB
testcase_10 AC 231 ms
134,620 KB
testcase_11 AC 180 ms
98,444 KB
testcase_12 AC 243 ms
135,064 KB
testcase_13 AC 233 ms
135,188 KB
testcase_14 AC 231 ms
135,192 KB
testcase_15 AC 232 ms
135,192 KB
testcase_16 AC 231 ms
135,192 KB
testcase_17 AC 238 ms
135,316 KB
testcase_18 AC 233 ms
135,188 KB
testcase_19 AC 240 ms
135,192 KB
testcase_20 AC 233 ms
135,196 KB
testcase_21 AC 234 ms
135,188 KB
testcase_22 AC 301 ms
135,704 KB
testcase_23 AC 309 ms
134,424 KB
testcase_24 AC 300 ms
135,708 KB
testcase_25 AC 312 ms
135,832 KB
testcase_26 AC 306 ms
134,404 KB
testcase_27 AC 319 ms
134,424 KB
testcase_28 AC 330 ms
136,220 KB
testcase_29 AC 329 ms
136,216 KB
testcase_30 AC 314 ms
135,700 KB
testcase_31 AC 331 ms
134,548 KB
testcase_32 AC 430 ms
135,700 KB
testcase_33 AC 391 ms
135,572 KB
testcase_34 AC 393 ms
136,344 KB
testcase_35 AC 438 ms
136,176 KB
testcase_36 AC 390 ms
135,836 KB
testcase_37 AC 418 ms
136,200 KB
testcase_38 AC 463 ms
136,332 KB
testcase_39 AC 454 ms
136,060 KB
testcase_40 AC 413 ms
136,004 KB
testcase_41 AC 418 ms
135,500 KB
testcase_42 AC 710 ms
178,696 KB
testcase_43 AC 726 ms
179,460 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 1,322 ms
202,976 KB
testcase_48 AC 1,318 ms
204,096 KB
testcase_49 AC 1,285 ms
203,104 KB
testcase_50 AC 1,329 ms
203,912 KB
testcase_51 AC 1,344 ms
203,248 KB
testcase_52 AC 370 ms
135,056 KB
testcase_53 AC 349 ms
135,800 KB
testcase_54 AC 334 ms
135,948 KB
testcase_55 AC 356 ms
135,948 KB
testcase_56 AC 363 ms
135,956 KB
testcase_57 AC 252 ms
136,096 KB
testcase_58 AC 207 ms
135,196 KB
testcase_59 AC 231 ms
134,288 KB
testcase_60 AC 258 ms
136,224 KB
testcase_61 AC 216 ms
134,908 KB
testcase_62 AC 1,121 ms
174,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#MMA Contest 015 - K Make a Sequence
#static Li Chao Treeで通ったのでdynamic Li Chao Treeで通す

#動的Li Chao Tree
class dynamic_Li_Chao_Tree:
    def __init__(self, min_x, max_x):
        #定義域 [min_x, max_x]内でのみ正確に動作する
        #_C[i]: (左子のid, 右子のid)  親のidは保持しない
        #_S[i]: (Lt, mid, Rt)  区間は閉区間[Lt, Rt]で扱う
        Lt, Rt = -1, 0
        while min_x < Lt: Lt = Lt * 2
        while Rt < max_x: Rt = Rt * 2 + 1
        self._C = [(None, None)]
        self._S = [(Lt, 0, Rt)]
        self._L = [None]

    def _f(self, line, x):
        return line[0] * x + line[1]

    def _make_child(self, index, c_Lt, c_Rt, make_left = True):
        #必要なら子を作り、子の番号を返す
        assert c_Lt <= c_Rt
        Lt, Rt = self._C[index]
        if make_left:
            if Lt != None: return Lt
            self._C[index] = (len(self._S), Rt)
        else:
            if Rt != None: return Rt
            self._C[index] = (Lt, len(self._S))
        self._C.append((None, None))
        self._S.append((c_Lt, (c_Lt + c_Rt) // 2, c_Rt))
        self._L.append(None)
        return len(self._S) - 1

    def _add_line(self, line, i):  #node iとその子に線分を追加
        Q = [(i, line)]
        while Q:
            i, line = Q.pop()
            if self._L[i] == None:
                self._L[i] = line
                continue
            Lt, mid, Rt = self._S[i]
            hL = self._f(line, Lt ) < self._f(self._L[i], Lt )
            hM = self._f(line, mid) < self._f(self._L[i], mid)     
            hR = self._f(line, Rt ) < self._f(self._L[i], Rt )
            if hL == hR == True:
                self._L[i] = line
                continue
            if hL == hR == False:
                continue
            if hM == line:
                self._L[i], line = line, self._L[i]
            if hL != hM:  #左子を作り降りる
                Q.append((self._make_child(i, Lt, mid, True), line))
            else:
                Q.append((self._make_child(i, mid + 1, Rt, False), line))

    def add_line(self, line, x_Lt = None, x_Rt = None):  #半開区間[x_Lt, x_Rt)に線分追加
        if x_Lt == x_Rt == None:
            self._add_line(line, 0)
            return
        x_Lt = self._S[0][0] if x_Lt == None else x_Lt
        x_Rt = self._S[0][2] if x_Rt == None else x_Rt - 1
        Q = [(0, line, x_Lt, x_Rt)]
        while Q:
            i, line, x_Lt, x_Rt = Q.pop()
            Lt, mid, Rt = self._S[i]
            if x_Lt > x_Rt or x_Rt < Lt or Rt < x_Lt:
                continue
            x_Lt = max(x_Lt, Lt)
            x_Rt = min(x_Rt, Rt)
            if x_Lt <= Lt <= Rt <= x_Rt:
                self._add_line(line, i)
            elif x_Rt <= mid:
                Q.append((self._make_child(i, Lt, mid, True), line, x_Lt, x_Rt))
            elif mid < x_Lt:
                Q.append((self._make_child(i, mid + 1, Rt, False), line, x_Lt, x_Rt))
            else:
                Q.append((self._make_child(i, Lt, mid, True), line, x_Lt, mid))
                Q.append((self._make_child(i, mid + 1, Rt, False), line, mid + 1, x_Rt))

    def fold(self, x):  #座標xの最小値を計算
        i = 0
        ans = 4 * 10 ** 18
        while i != None:
            Lt, mid, Rt = self._S[i]
            c_Lt, c_Rt = self._C[i]
            if self._L[i] != None:
                ans = min(ans, self._f(self._L[i], x))
            i = c_Lt if x <= mid else c_Rt
        return ans

#Z-algorithm  TとS[i:]のLCP判定は、T+'$'+S のようにSに出ない文字を挟んで実行するとよい
#Reference: https://tjkendev.github.io/procon-library/python/string/z-algorithm.html
def Z_algorithm(S):  #SとS[i:]の最長共通接頭辞を求める
    N=len(S); A=[0]*N; i,j=1,0; A[0]=L=N
    while i<L:
        while i+j<L and S[j]==S[i+j]: j+=1
        if not j: i+=1; continue
        A[i]=j; k=1
        while L-i>k<j-A[k]: A[i+k]=A[k]; k+=1
        i+=k; j-=k
    return A


#入力受取
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

#Bの(Aとの)最長共通接頭辞を計算
same = Z_algorithm(A + [0] + B)[N + 1:]

#DP[i]: 列Sを[0: i)まで(つまり、i文字目の直前まで)一致させるための最小コスト とする
#これはLi Chao Treeの線分追加で計算が可能
#k = same[i]として、[i: i + k)に y = C[i] * x + (DP[i] - C[i] * i) の線分を追加する
LCT = dynamic_Li_Chao_Tree(0, M + 1)
LCT.add_line((0, 0), 0, 1)  #初期化
for i in range(M):
    b = LCT.fold(i)
    k = same[i]
    if b > 10 ** 18 or k == 0:
        continue
    LCT.add_line((C[i], b - C[i] * i), i + 1, min(M, i + k) + 1)
    
#答えを出力
ans = LCT.fold(M)
print(ans if ans < 10 ** 18 else -1)
0