結果

問題 No.808 Kaiten Sushi?
ユーザー mkawa2mkawa2
提出日時 2022-03-03 12:12:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,894 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 82,352 KB
実行使用メモリ 254,832 KB
最終ジャッジ日時 2024-07-17 08:42:58
合計ジャッジ時間 21,820 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 40 ms
53,164 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 117 ms
78,164 KB
testcase_08 AC 114 ms
77,756 KB
testcase_09 WA -
testcase_10 AC 129 ms
78,968 KB
testcase_11 AC 113 ms
77,312 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 83 ms
76,136 KB
testcase_18 AC 63 ms
68,984 KB
testcase_19 AC 56 ms
64,468 KB
testcase_20 AC 64 ms
69,096 KB
testcase_21 AC 55 ms
65,868 KB
testcase_22 AC 62 ms
68,340 KB
testcase_23 AC 336 ms
118,684 KB
testcase_24 AC 292 ms
115,552 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 498 ms
183,992 KB
testcase_53 AC 652 ms
252,976 KB
testcase_54 AC 38 ms
53,576 KB
testcase_55 AC 39 ms
53,156 KB
testcase_56 AC 40 ms
53,584 KB
testcase_57 AC 273 ms
120,740 KB
testcase_58 AC 401 ms
144,028 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