結果

問題 No.618 labo-index
ユーザー maspymaspy
提出日時 2020-04-08 01:31:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 856 ms / 6,000 ms
コード長 2,240 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 123,804 KB
最終ジャッジ日時 2023-09-22 21:09:29
合計ジャッジ時間 15,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,308 KB
testcase_01 AC 72 ms
71,288 KB
testcase_02 AC 72 ms
71,448 KB
testcase_03 AC 72 ms
71,416 KB
testcase_04 AC 104 ms
77,404 KB
testcase_05 AC 124 ms
77,560 KB
testcase_06 AC 77 ms
75,468 KB
testcase_07 AC 83 ms
75,736 KB
testcase_08 AC 90 ms
76,536 KB
testcase_09 AC 97 ms
76,560 KB
testcase_10 AC 110 ms
77,520 KB
testcase_11 AC 94 ms
76,428 KB
testcase_12 AC 94 ms
76,660 KB
testcase_13 AC 103 ms
77,392 KB
testcase_14 AC 95 ms
76,760 KB
testcase_15 AC 103 ms
77,376 KB
testcase_16 AC 115 ms
77,936 KB
testcase_17 AC 92 ms
76,448 KB
testcase_18 AC 136 ms
78,984 KB
testcase_19 AC 506 ms
102,568 KB
testcase_20 AC 593 ms
102,624 KB
testcase_21 AC 523 ms
102,600 KB
testcase_22 AC 543 ms
102,792 KB
testcase_23 AC 556 ms
102,612 KB
testcase_24 AC 525 ms
102,712 KB
testcase_25 AC 577 ms
102,692 KB
testcase_26 AC 568 ms
102,612 KB
testcase_27 AC 547 ms
102,488 KB
testcase_28 AC 514 ms
102,804 KB
testcase_29 AC 555 ms
102,700 KB
testcase_30 AC 526 ms
102,808 KB
testcase_31 AC 549 ms
102,512 KB
testcase_32 AC 576 ms
102,608 KB
testcase_33 AC 564 ms
102,608 KB
testcase_34 AC 856 ms
115,144 KB
testcase_35 AC 840 ms
123,804 KB
testcase_36 AC 366 ms
106,096 KB
testcase_37 AC 658 ms
104,968 KB
testcase_38 AC 71 ms
71,120 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):
    left = 0
    right = full + 1
    while left + 1 < right:
        k = (left + right) // 2
        if base + nums[bit.find_kth_element(full + 1 - k)] >= k:
            left = k
        else:
            right = k
    return left


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