結果

問題 No.649 ここでちょっとQK!
ユーザー c-yanc-yan
提出日時 2021-02-15 00:15:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 820 ms / 3,000 ms
コード長 2,382 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 167,892 KB
最終ジャッジ日時 2024-07-22 12:10:02
合計ジャッジ時間 13,597 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,864 KB
testcase_01 AC 42 ms
52,608 KB
testcase_02 AC 41 ms
52,992 KB
testcase_03 AC 133 ms
100,480 KB
testcase_04 AC 296 ms
167,760 KB
testcase_05 AC 296 ms
167,892 KB
testcase_06 AC 187 ms
95,512 KB
testcase_07 AC 41 ms
52,736 KB
testcase_08 AC 40 ms
52,608 KB
testcase_09 AC 40 ms
53,120 KB
testcase_10 AC 45 ms
52,992 KB
testcase_11 AC 40 ms
52,736 KB
testcase_12 AC 473 ms
117,760 KB
testcase_13 AC 481 ms
117,632 KB
testcase_14 AC 479 ms
117,760 KB
testcase_15 AC 475 ms
117,504 KB
testcase_16 AC 300 ms
117,632 KB
testcase_17 AC 402 ms
122,752 KB
testcase_18 AC 441 ms
107,776 KB
testcase_19 AC 511 ms
127,592 KB
testcase_20 AC 520 ms
130,772 KB
testcase_21 AC 623 ms
140,136 KB
testcase_22 AC 670 ms
142,848 KB
testcase_23 AC 709 ms
121,344 KB
testcase_24 AC 719 ms
144,568 KB
testcase_25 AC 732 ms
153,784 KB
testcase_26 AC 820 ms
159,792 KB
testcase_27 AC 71 ms
69,632 KB
testcase_28 AC 76 ms
71,040 KB
testcase_29 AC 61 ms
64,512 KB
testcase_30 AC 384 ms
104,736 KB
testcase_31 AC 323 ms
104,248 KB
testcase_32 AC 42 ms
52,736 KB
testcase_33 AC 42 ms
52,864 KB
testcase_34 AC 40 ms
52,736 KB
testcase_35 AC 40 ms
52,352 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