結果

問題 No.2332 Make a Sequence
コンテスト
ユーザー LyricalMaestro
提出日時 2026-06-08 01:11:34
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 2,316 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 946 ms
コンパイル使用メモリ 85,760 KB
実行使用メモリ 191,260 KB
最終ジャッジ日時 2026-06-08 01:12:10
合計ジャッジ時間 25,831 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11 WA * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/2332

import heapq

MAX_INT = 10 ** 18

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    # 座標圧縮
    value_set = set()
    for a in A:
        value_set.add(a)
    for b in B:
        value_set.add(b)
    value_list = list(value_set)
    value_list.sort()
    value_map = {}
    for i, v in enumerate(value_list):
        value_map[v] = i + 1
    A = [value_map[a] for a in A]
    B = [value_map[b] for b in B]
    value_max = len(value_list)

    H = 10000000000000069
    B_ = value_max + 1

    # Aのローリングハッシュ
    hash_a_list = [0] * N
    hash_a = 0
    for i in range(N):
        hash_a *= B_
        hash_a %= H
        hash_a += A[i]
        hash_a %= H
        hash_a_list[i] = hash_a

    # Bのローリングハッシュ
    hash_b_list = [0] * M
    hash_b = 0
    for i in range(M):
        hash_b *= B_
        hash_b %= H
        hash_b += B[i]
        hash_b %= H
        hash_b_list[i] = hash_b

    pow_b_ = [0] * (max(N, M) + 1)
    b_ = 1
    for i in range(max(N, M) + 1):
        pow_b_[i] = b_
        b_ *= B_
        b_ %= H
    
    def solve(mid, i):
        hash_a = hash_a_list[mid]
        hash_b = hash_b_list[i + mid]
        if i > 0:
            hash_b -= (hash_b_list[i - 1] * pow_b_[mid + 1]) % H
        return hash_a == hash_b


    array = [MAX_INT] * (M + 1)
    array[0] = 0
    queue = []

    for i in range(M):
        while len(queue) > 0 and queue[0][1] < i + 1:
            heapq.heappop(queue)

        if array[i] < MAX_INT and A[0] == B[i]:
            low = 0
            high = min(M - 1 - i, N - 1)
            while high - low > 1:
                mid = (high + low ) // 2
                if solve(mid, i):
                    low = mid
                else:
                    high = mid
            if solve(high, i):
                v = high
            else:
                v = low

            heapq.heappush(queue, (C[i], i + 1 + v))

        if len(queue) > 0:
            array[i + 1] = queue[0][0]

    if array[-1] >= MAX_INT:
        print(-1)
    else:
        ans = sum(array)
        print(ans)








                


if __name__ == "__main__":
    main()
0