結果

問題 No.649 ここでちょっとQK!
ユーザー c-yanc-yan
提出日時 2021-02-15 00:14:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,364 ms / 3,000 ms
コード長 2,382 bytes
コンパイル時間 116 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 61,588 KB
最終ジャッジ日時 2023-09-29 18:08:17
合計ジャッジ時間 31,684 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,444 KB
testcase_01 AC 16 ms
8,424 KB
testcase_02 AC 17 ms
8,396 KB
testcase_03 AC 186 ms
28,968 KB
testcase_04 AC 1,115 ms
61,508 KB
testcase_05 AC 1,117 ms
61,588 KB
testcase_06 AC 351 ms
36,780 KB
testcase_07 AC 16 ms
8,508 KB
testcase_08 AC 15 ms
8,504 KB
testcase_09 AC 16 ms
8,444 KB
testcase_10 AC 15 ms
8,396 KB
testcase_11 AC 15 ms
8,416 KB
testcase_12 AC 1,208 ms
34,044 KB
testcase_13 AC 1,217 ms
33,992 KB
testcase_14 AC 1,226 ms
34,124 KB
testcase_15 AC 1,175 ms
34,148 KB
testcase_16 AC 701 ms
34,000 KB
testcase_17 AC 1,020 ms
35,676 KB
testcase_18 AC 1,148 ms
37,216 KB
testcase_19 AC 1,284 ms
38,800 KB
testcase_20 AC 1,402 ms
40,320 KB
testcase_21 AC 1,632 ms
41,888 KB
testcase_22 AC 1,697 ms
43,412 KB
testcase_23 AC 1,928 ms
44,976 KB
testcase_24 AC 2,101 ms
46,516 KB
testcase_25 AC 2,166 ms
48,104 KB
testcase_26 AC 2,364 ms
59,972 KB
testcase_27 AC 24 ms
8,628 KB
testcase_28 AC 21 ms
8,568 KB
testcase_29 AC 20 ms
8,612 KB
testcase_30 AC 1,055 ms
26,252 KB
testcase_31 AC 785 ms
26,360 KB
testcase_32 AC 17 ms
8,436 KB
testcase_33 AC 16 ms
8,432 KB
testcase_34 AC 16 ms
8,432 KB
testcase_35 AC 16 ms
8,492 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