結果

問題 No.808 Kaiten Sushi?
ユーザー maspymaspy
提出日時 2020-04-10 10:46:21
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 589 ms / 2,000 ms
コード長 2,253 bytes
コンパイル時間 2,139 ms
コンパイル使用メモリ 86,292 KB
実行使用メモリ 101,128 KB
最終ジャッジ日時 2023-10-12 20:48:03
合計ジャッジ時間 21,529 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
70,940 KB
testcase_01 AC 72 ms
70,824 KB
testcase_02 AC 71 ms
71,016 KB
testcase_03 AC 72 ms
71,048 KB
testcase_04 AC 72 ms
70,828 KB
testcase_05 AC 73 ms
70,688 KB
testcase_06 AC 72 ms
70,820 KB
testcase_07 AC 126 ms
77,916 KB
testcase_08 AC 120 ms
77,968 KB
testcase_09 AC 147 ms
78,448 KB
testcase_10 AC 135 ms
78,224 KB
testcase_11 AC 125 ms
77,836 KB
testcase_12 AC 135 ms
77,844 KB
testcase_13 AC 125 ms
77,992 KB
testcase_14 AC 98 ms
76,584 KB
testcase_15 AC 146 ms
78,512 KB
testcase_16 AC 133 ms
77,820 KB
testcase_17 AC 102 ms
76,788 KB
testcase_18 AC 94 ms
75,864 KB
testcase_19 AC 87 ms
75,696 KB
testcase_20 AC 96 ms
75,552 KB
testcase_21 AC 87 ms
75,644 KB
testcase_22 AC 92 ms
75,740 KB
testcase_23 AC 263 ms
88,124 KB
testcase_24 AC 241 ms
85,116 KB
testcase_25 AC 260 ms
87,896 KB
testcase_26 AC 253 ms
86,956 KB
testcase_27 AC 519 ms
98,748 KB
testcase_28 AC 279 ms
84,228 KB
testcase_29 AC 526 ms
99,460 KB
testcase_30 AC 389 ms
94,472 KB
testcase_31 AC 534 ms
101,112 KB
testcase_32 AC 556 ms
100,988 KB
testcase_33 AC 368 ms
92,324 KB
testcase_34 AC 411 ms
97,500 KB
testcase_35 AC 480 ms
98,404 KB
testcase_36 AC 370 ms
92,716 KB
testcase_37 AC 72 ms
71,132 KB
testcase_38 AC 70 ms
71,220 KB
testcase_39 AC 536 ms
100,388 KB
testcase_40 AC 229 ms
84,056 KB
testcase_41 AC 281 ms
84,876 KB
testcase_42 AC 508 ms
99,212 KB
testcase_43 AC 306 ms
88,636 KB
testcase_44 AC 390 ms
97,352 KB
testcase_45 AC 305 ms
87,608 KB
testcase_46 AC 258 ms
84,440 KB
testcase_47 AC 574 ms
100,972 KB
testcase_48 AC 589 ms
100,952 KB
testcase_49 AC 565 ms
101,128 KB
testcase_50 AC 580 ms
100,948 KB
testcase_51 AC 559 ms
101,060 KB
testcase_52 AC 325 ms
98,232 KB
testcase_53 AC 407 ms
100,476 KB
testcase_54 AC 72 ms
71,244 KB
testcase_55 AC 71 ms
71,044 KB
testcase_56 AC 73 ms
71,084 KB
testcase_57 AC 223 ms
85,968 KB
testcase_58 AC 269 ms
94,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect


class BinaryIndexedTree():
    def __init__(self, seq):
        self.size = len(seq)
        self.depth = self.size.bit_length()
        self.build(seq)

    def build(self, seq):
        data = seq
        size = self.size
        for i, x in enumerate(data):
            j = i + (i & (-i))
            if j < size:
                data[j] += data[i]
        self.data = data

    def __repr__(self):
        return self.data.__repr__()

    def get_sum(self, i):
        data = self.data
        s = 0
        while i:
            s += data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        data = self.data
        size = self.size
        while i < size:
            data[i] += x
            i += i & -i

    def find_kth_element(self, k):
        data = self.data
        size = self.size
        x, sx = 0, 0
        dx = 1 << (self.depth)
        for i in range(self.depth - 1, -1, -1):
            dx = (1 << i)
            if x + dx >= size:
                continue
            y = x + dx
            sy = sx + data[y]
            if sy < k:
                x, sx = y, sy
        return x + 1


N, L = map(int, readline().split())
X = tuple(map(int, readline().split()))
Y = tuple(map(int, readline().split()))

sushi = BinaryIndexedTree([0] + [1] * (N + N))
tea = BinaryIndexedTree([0] + [1] * (N + N))

x = X[0]
sushi.add(1, -1)
sushi.add(N + 1, -1)
cycle = 0

for _ in range(N - 1):
    # 次の茶まで進む
    k = tea.get_sum(bisect(Y, x))  # 左にある茶の個数
    i = tea.find_kth_element(k + 1)
    if i > N:
        i -= N
        cycle += 1
    x = Y[i - 1]
    # 次の寿司まで進む
    k = sushi.get_sum(bisect(X, x))
    i = sushi.find_kth_element(k + 1)
    if i > N:
        i -= N
        cycle += 1
    x = X[i - 1]
    # 寿司を削除
    sushi.add(i, -1)
    sushi.add(i + N, -1)
    # 手前のお茶を削除
    k = tea.get_sum(bisect(Y, x) + N)
    i = tea.find_kth_element(k)
    if i > N:
        i -= N
    tea.add(i, -1)
    tea.add(i + N, -1)

i = tea.find_kth_element(1)
y = Y[i - 1]
if x > y:
    y += L
print(cycle * L + y)
0