結果

問題 No.649 ここでちょっとQK!
ユーザー c-yanc-yan
提出日時 2021-02-15 00:15:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 786 ms / 3,000 ms
コード長 2,382 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 163,036 KB
最終ジャッジ日時 2023-09-29 18:09:42
合計ジャッジ時間 14,196 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,360 KB
testcase_01 AC 73 ms
71,428 KB
testcase_02 AC 75 ms
71,496 KB
testcase_03 AC 148 ms
103,116 KB
testcase_04 AC 293 ms
139,680 KB
testcase_05 AC 292 ms
139,368 KB
testcase_06 AC 198 ms
98,156 KB
testcase_07 AC 75 ms
71,120 KB
testcase_08 AC 73 ms
71,516 KB
testcase_09 AC 74 ms
71,336 KB
testcase_10 AC 74 ms
71,372 KB
testcase_11 AC 73 ms
71,316 KB
testcase_12 AC 464 ms
118,912 KB
testcase_13 AC 431 ms
118,656 KB
testcase_14 AC 423 ms
119,388 KB
testcase_15 AC 438 ms
118,868 KB
testcase_16 AC 314 ms
119,128 KB
testcase_17 AC 416 ms
105,976 KB
testcase_18 AC 454 ms
124,020 KB
testcase_19 AC 520 ms
130,896 KB
testcase_20 AC 534 ms
133,864 KB
testcase_21 AC 632 ms
142,816 KB
testcase_22 AC 660 ms
118,520 KB
testcase_23 AC 722 ms
144,928 KB
testcase_24 AC 716 ms
146,984 KB
testcase_25 AC 709 ms
157,060 KB
testcase_26 AC 786 ms
163,036 KB
testcase_27 AC 99 ms
77,028 KB
testcase_28 AC 100 ms
76,836 KB
testcase_29 AC 89 ms
76,080 KB
testcase_30 AC 384 ms
107,640 KB
testcase_31 AC 310 ms
107,652 KB
testcase_32 AC 73 ms
71,264 KB
testcase_33 AC 74 ms
71,392 KB
testcase_34 AC 73 ms
71,376 KB
testcase_35 AC 73 ms
71,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from operator import add
from sys import stdin


class SegmentTree:
    def __init__(self, size, op, e):
        self._op = op
        self._e = e
        self._size = size
        t = 1
        while t < size:
            t *= 2
        self._offset = t - 1
        self._data = [e] * (t * 2 - 1)

    def __getitem__(self, key):
        return self._data[self._offset + key]

    def __iter__(self):
        for i in range(self._size):
            yield self[i]

    def __setitem__(self, key, value):
        op = self._op
        data = self._data
        i = self._offset + key
        data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            data[i] = op(data[i * 2 + 1], data[i * 2 + 2])

    def build(self, iterable):
        op = self._op
        data = self._data
        data[self._offset:self._offset + self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            data[i] = op(data[i * 2 + 1], data[i * 2 + 2])

    def query(self, start, stop):
        def iter_segments(data, l, r):
            while l < r:
                if l & 1 == 0:
                    yield data[l]
                if r & 1 == 0:
                    yield data[r - 1]
                l = l // 2
                r = (r - 1) // 2
        op = self._op
        it = iter_segments(self._data, start + self._offset,
                           stop + self._offset)
        result = self._e
        for v in it:
            result = op(result, v)
        return result


def shrink(a):
    inv = sorted(set(a))
    n = len(inv)
    fwd = {}
    for i in range(n):
        fwd[inv[i]] = i
    return n, fwd, inv


readline = stdin.readline

Q, K = map(int, readline().split())
query = [readline() for _ in range(Q)]

a = []
for q in query:
    if q[0] != '1':
        continue
    a.append(int(q[2:]))
n, fwd, inv = shrink(a)

st = SegmentTree(n, add, 0)
k = 0
result = []
for q in query:
    if q[0] == '1':
        v = fwd[int(q[2:])]
        st[v] += 1
        k += 1
    elif q[0] == '2':
        if k < K:
            result.append(-1)
            continue
        ok = n
        ng = -1
        while ok - ng > 1:
            m = ng + (ok - ng) // 2
            if st.query(0, m) >= K:
                ok = m
            else:
                ng = m
        result.append(inv[ok - 1])
        st[ok - 1] -= 1
        k -= 1
print(*result, sep='\n')
0