結果

問題 No.618 labo-index
ユーザー maspymaspy
提出日時 2020-04-08 01:25:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,551 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 81,988 KB
実行使用メモリ 124,544 KB
最終ジャッジ日時 2024-07-08 11:38:24
合計ジャッジ時間 8,785 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,224 KB
testcase_01 AC 37 ms
52,224 KB
testcase_02 AC 36 ms
51,968 KB
testcase_03 AC 37 ms
51,968 KB
testcase_04 AC 66 ms
68,352 KB
testcase_05 AC 71 ms
74,240 KB
testcase_06 AC 38 ms
52,608 KB
testcase_07 AC 48 ms
61,184 KB
testcase_08 AC 52 ms
63,104 KB
testcase_09 AC 61 ms
67,200 KB
testcase_10 AC 66 ms
70,400 KB
testcase_11 AC 57 ms
65,408 KB
testcase_12 WA -
testcase_13 AC 62 ms
68,352 KB
testcase_14 AC 57 ms
66,176 KB
testcase_15 WA -
testcase_16 AC 74 ms
75,264 KB
testcase_17 WA -
testcase_18 AC 92 ms
76,928 KB
testcase_19 AC 252 ms
102,656 KB
testcase_20 AC 248 ms
102,528 KB
testcase_21 AC 255 ms
102,528 KB
testcase_22 AC 249 ms
102,528 KB
testcase_23 AC 252 ms
102,880 KB
testcase_24 AC 250 ms
102,656 KB
testcase_25 AC 254 ms
103,040 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 245 ms
102,912 KB
testcase_29 AC 253 ms
102,784 KB
testcase_30 AC 257 ms
102,912 KB
testcase_31 AC 258 ms
102,528 KB
testcase_32 AC 256 ms
102,856 KB
testcase_33 AC 260 ms
102,528 KB
testcase_34 AC 270 ms
124,544 KB
testcase_35 AC 258 ms
123,136 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 36 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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


Q = int(readline())
m = map(int, read().split())
query = tuple(zip(m, m))

nums = []
base = 0
for t, x in query:
    if t == 1:
        nums.append(x - base)
    elif t == 2:
        continue
    else:
        base += x

INF = 10 ** 18
nums.append(-INF)
nums.append(INF)
nums = sorted(set(nums))
num_rank = {x: i for i, x in enumerate(nums)}


def solve(bit, nums, full, base):
    if full == 0:
        return 0
    if nums[1] + base > full:
        return 0
    data = bit.data
    size = bit.size
    x, sx = 0, 0
    for i in range(bit.depth - 1, -1, -1):
        dx = (1 << i)
        if x + dx >= size:
            continue
        y = x + dx
        sy = sx + data[y]
        if y + 1 < len(nums) and full - sy >= nums[y + 1] + base:
            x, sx = y, sy
    cand = nums[x + 1] + base
    n = bit.get_sum(x + 1)
    if nums[x + 1] + 1 + base <= full - n:
        return full - n
    else:
        return cand


bit = BinaryIndexedTree([0] * (len(nums)))
base = 0
member = []
full = 0
for t, x in query:
    if t == 1:
        r = num_rank[x - base]
        member.append(r)
        bit.add(r, 1)
        full += 1
    elif t == 2:
        r = member[x - 1]
        bit.add(r, -1)
        full -= 1
    else:
        base += x
    print(solve(bit, nums, full, base))
0