結果

問題 No.2332 Make a Sequence
ユーザー vwxyzvwxyz
提出日時 2023-08-10 18:54:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 874 ms / 2,000 ms
コード長 3,349 bytes
コンパイル時間 878 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 175,368 KB
最終ジャッジ日時 2024-04-28 12:17:39
合計ジャッジ時間 33,878 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,120 KB
testcase_01 AC 37 ms
52,864 KB
testcase_02 AC 37 ms
52,736 KB
testcase_03 AC 36 ms
52,864 KB
testcase_04 AC 35 ms
52,736 KB
testcase_05 AC 38 ms
52,992 KB
testcase_06 AC 36 ms
53,120 KB
testcase_07 AC 398 ms
162,584 KB
testcase_08 AC 128 ms
102,400 KB
testcase_09 AC 276 ms
124,268 KB
testcase_10 AC 345 ms
144,368 KB
testcase_11 AC 260 ms
127,272 KB
testcase_12 AC 411 ms
158,228 KB
testcase_13 AC 418 ms
158,092 KB
testcase_14 AC 421 ms
158,080 KB
testcase_15 AC 423 ms
158,472 KB
testcase_16 AC 427 ms
158,336 KB
testcase_17 AC 416 ms
161,664 KB
testcase_18 AC 417 ms
158,588 KB
testcase_19 AC 408 ms
158,340 KB
testcase_20 AC 401 ms
158,220 KB
testcase_21 AC 417 ms
158,468 KB
testcase_22 AC 425 ms
151,820 KB
testcase_23 AC 427 ms
151,484 KB
testcase_24 AC 418 ms
151,420 KB
testcase_25 AC 409 ms
151,564 KB
testcase_26 AC 420 ms
151,756 KB
testcase_27 AC 442 ms
151,640 KB
testcase_28 AC 439 ms
151,688 KB
testcase_29 AC 444 ms
151,692 KB
testcase_30 AC 435 ms
151,556 KB
testcase_31 AC 449 ms
151,652 KB
testcase_32 AC 442 ms
152,332 KB
testcase_33 AC 448 ms
151,692 KB
testcase_34 AC 443 ms
150,928 KB
testcase_35 AC 447 ms
152,964 KB
testcase_36 AC 435 ms
150,928 KB
testcase_37 AC 484 ms
162,440 KB
testcase_38 AC 480 ms
160,016 KB
testcase_39 AC 491 ms
162,776 KB
testcase_40 AC 466 ms
159,768 KB
testcase_41 AC 472 ms
162,872 KB
testcase_42 AC 644 ms
162,572 KB
testcase_43 AC 615 ms
162,948 KB
testcase_44 AC 572 ms
159,500 KB
testcase_45 AC 626 ms
160,492 KB
testcase_46 AC 594 ms
161,796 KB
testcase_47 AC 858 ms
167,040 KB
testcase_48 AC 867 ms
166,916 KB
testcase_49 AC 874 ms
167,040 KB
testcase_50 AC 863 ms
166,788 KB
testcase_51 AC 853 ms
166,928 KB
testcase_52 AC 480 ms
159,672 KB
testcase_53 AC 459 ms
157,416 KB
testcase_54 AC 446 ms
155,136 KB
testcase_55 AC 463 ms
159,732 KB
testcase_56 AC 462 ms
159,612 KB
testcase_57 AC 457 ms
150,152 KB
testcase_58 AC 437 ms
150,024 KB
testcase_59 AC 467 ms
153,052 KB
testcase_60 AC 465 ms
158,196 KB
testcase_61 AC 448 ms
155,576 KB
testcase_62 AC 803 ms
175,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

class Li_Chao_Tree:
    def __init__(self,N,X=None,inf=float("inf")):
        self.N=N
        if X==None:
            self.X=[x for x in range(self.N)]
        else:
            self.X=X
        self.idx={x:i for i,x in enumerate(self.X)}
        self.inf=inf
        self.li_chao_tree=[(0,self.inf)]*(2*self.N)
        self.left,self.right=[None]*2*N,[None]*2*N
        for i in range(self.N,2*self.N):
            self.left[i]=self.X[i-self.N]
            self.right[i]=self.X[i-self.N]
        for i in range(self.N-1,0,-1):
            self.left[i]=self.left[i<<1]
            self.right[i]=self.right[i<<1|1]

    def add_line_one_segment(self,a,b,i):
        queue=[i]
        while queue:
            i=queue.pop()
            aa,bb=self.li_chao_tree[i]
            l=a*self.X[self.left[i]]+b
            r=a*self.X[self.right[i]]+b
            ll=aa*self.X[self.left[i]]+bb
            rr=aa*self.X[self.right[i]]+bb
            if ll<=l and rr<=r:
                continue
            if l<=ll and r<=rr:
                self.li_chao_tree[i]=(a,b)
                continue
            queue.append(i<<1)
            queue.append(i<<1|1)

    def add_line(self,a,b,L=None,R=None):
        if L==None:
            L=self.N
        else:
            L+=self.N
        if R==None:
            R=2*self.N
        else:
            R+=self.N
        while L<R:
            if L%2:
                self.add_line_one_segment(a,b,L)
                L+=1
            if R%2:
                R-=1
                self.add_line_one_segment(a,b,R)
            L>>=1
            R>>=1

    def __call__(self,x):
        i=self.idx[x]+self.N
        retu=self.inf
        while i:
            a,b=self.li_chao_tree[i]
            retu=min(retu,a*x+b)
            i>>=1
        return retu
    
    def __getitem__(self,i):
        x=self.X[i]
        i+=self.N
        retu=self.inf
        while i:
            a,b=self.li_chao_tree[i]
            retu=min(retu,a*x+b)
            i>>=1
        return retu

    def __str__(self):
        li_chao_tree=[(0,inf)]*self.N
        for i,x in enumerate(self.X):
            ii=i+self.N
            while ii:
                aa,bb=self.li_chao_tree[ii]
                a,b=li_chao_tree[i]
                if aa*x+bb<a*x+b:
                    li_chao_tree[i]=aa,bb
                ii>>=1
        return "["+", ".join(map(str,li_chao_tree))+"]"

def Z_algorithm(lst):
    le=len(lst)
    LCP=[None]*le
    LCP[0]=le
    L,R=0,0
    for i in range(1,le):
        if i<R:
            x=LCP[i-L]
            if x<R-i:
                LCP[i]=x
            elif R-i<x:
                LCP[i]=R-i
            else:
                while R<le and lst[R-i]==lst[R]:
                    R+=1
                LCP[i]=R-i
                L=i
        else:
            R=max(R,i)
            while R<le and lst[R-i]==lst[R]:
                R+=1
            LCP[i]=R-i
            L=i
    return LCP

N,M=map(int,readline().split())
A=list(map(int,readline().split()))
B=list(map(int,readline().split()))
C=list(map(int,readline().split()))
Z=Z_algorithm(A+B)
inf=float("inf")
LCT=Li_Chao_Tree(M+1,inf=inf)
dp=[inf]*(M+1)
dp[0]=0
for m in range(M):
    k=Z[N+m]
    LCT.add_line(C[m],dp[m]-m*C[m],m,m+k+1)
    dp[m+1]=LCT[m+1]
if dp[M]==inf:
    ans=-1
else:
    ans=dp[M]
print(ans)
0