結果

問題 No.808 Kaiten Sushi?
ユーザー mkawa2mkawa2
提出日時 2022-03-03 12:12:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,894 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 255,588 KB
最終ジャッジ日時 2023-09-24 08:19:49
合計ジャッジ時間 24,503 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 73 ms
71,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 140 ms
79,768 KB
testcase_08 AC 142 ms
79,792 KB
testcase_09 WA -
testcase_10 AC 156 ms
80,248 KB
testcase_11 AC 141 ms
80,296 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 108 ms
77,820 KB
testcase_18 AC 91 ms
76,276 KB
testcase_19 AC 85 ms
76,172 KB
testcase_20 AC 96 ms
75,976 KB
testcase_21 AC 86 ms
76,288 KB
testcase_22 AC 90 ms
75,888 KB
testcase_23 AC 369 ms
126,320 KB
testcase_24 AC 321 ms
114,028 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 AC 517 ms
194,440 KB
testcase_53 AC 666 ms
255,576 KB
testcase_54 AC 71 ms
70,956 KB
testcase_55 AC 72 ms
71,360 KB
testcase_56 AC 72 ms
71,112 KB
testcase_57 AC 302 ms
115,100 KB
testcase_58 AC 418 ms
146,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

class BitSum:
    def __init__(self, n):
        self.n = n+1
        self.table = [0]*self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

    def pos(self, x):
        idx = 0
        for lv in range((self.n-1).bit_length()-1, -1, -1):
            mid = idx+(1 << lv)
            if mid >= self.n: continue
            if self.table[mid] < x:
                x -= self.table[mid]
                idx += 1 << lv
        return idx

# 要素を先読みする代わりに、[0,10**6]でなくとも使える
class Cset:
    def __init__(self, aa):
        aa = set(aa)
        self.dec = tuple(sorted(aa))
        self.enc = {a: i for i, a in enumerate(self.dec)}
        self.mx = len(self.dec)
        self.size = 0
        self.bit = BitSum(self.mx+1)
        self.nums = [False]*(self.mx+1)

    def __repr__(self):
        return "["+" ".join(str(self[i]) for i in range(self.size))+"]"

    def insert(self, a):
        if a not in self.enc: return False
        a = self.enc[a]
        if a < 0 or a > self.mx or self.nums[a]: return False
        self.size += 1
        self.bit.add(a, 1)
        self.nums[a] = True
        return True

    def remove(self, value):
        if value not in self.enc: return False
        value = self.enc[value]
        if value < 0 or value > self.mx or not self.nums[value]: return False
        self.size -= 1
        self.bit.add(value, -1)
        self.nums[value] = False
        return True

    def erase(self, index):
        i = index
        if i >= self.size or i < -self.size: return False
        if i < 0: i = self.size+i+1
        else: i += 1
        a = self.bit.pos(i)
        self.size -= 1
        self.bit.add(a, -1)
        self.nums[a] = False
        return True

    def __getitem__(self, i):
        if i >= self.size or i < -self.size: return -1
        if i < 0: i = self.size+i+1
        else: i += 1
        return self.dec[self.bit.pos(i)]

    def __contains__(self, item):
        return self.nums[self.enc[item]]

    def __len__(self):
        return self.size

    def bisect_left(self, a):
        if a not in self.enc: return -1
        a = self.enc[a]
        if a < 0 or a > self.mx: return -1
        if a == 0: return 0
        return self.bit.sum(a-1)

    def bisect_right(self, a):
        if a not in self.enc: return -1
        a = self.enc[a]
        if a < 0 or a > self.mx: return -1
        return self.bit.sum(a)

n, l = LI()
xx0 = LI()
yy0 = LI()

ans = now = xx0[0]
xx = Cset(xx0[1:]+yy0)
yy = Cset(xx0[1:]+yy0)
for x in xx0[1:]: xx.insert(x)
for y in yy0: yy.insert(y)

for _ in range(n-1):
    j = yy.bisect_left(now)%len(yy)  # 一番近いお茶
    i = xx.bisect_left(yy[j])%len(xx)  # お茶後の寿司
    x = xx[i]
    j = (yy.bisect_left(x)-1)%len(yy)  # 次の寿司直前のお茶
    y = yy[j]
    ans += (y-now)%l+(x-y)%l
    xx.remove(x)
    yy.remove(y)
    now = x
    # pDB(y, x, ans, xx, yy)

ans += (yy[0]-now)%l

print(ans)
0