結果

問題 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,925 ms / 3,000 ms
コード長 2,382 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 61,004 KB
最終ジャッジ日時 2024-07-22 12:08:28
合計ジャッジ時間 37,058 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,752 KB
testcase_01 AC 32 ms
10,752 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 240 ms
28,288 KB
testcase_04 AC 1,365 ms
60,876 KB
testcase_05 AC 1,371 ms
61,004 KB
testcase_06 AC 427 ms
35,840 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 32 ms
10,880 KB
testcase_09 AC 31 ms
10,880 KB
testcase_10 AC 32 ms
10,752 KB
testcase_11 AC 32 ms
10,880 KB
testcase_12 AC 1,471 ms
34,764 KB
testcase_13 AC 1,473 ms
34,708 KB
testcase_14 AC 1,486 ms
34,876 KB
testcase_15 AC 1,450 ms
34,896 KB
testcase_16 AC 856 ms
35,028 KB
testcase_17 AC 1,232 ms
36,424 KB
testcase_18 AC 1,383 ms
37,704 KB
testcase_19 AC 1,580 ms
38,952 KB
testcase_20 AC 1,745 ms
40,632 KB
testcase_21 AC 2,003 ms
41,860 KB
testcase_22 AC 2,127 ms
43,256 KB
testcase_23 AC 2,363 ms
44,716 KB
testcase_24 AC 2,544 ms
46,124 KB
testcase_25 AC 2,688 ms
47,528 KB
testcase_26 AC 2,925 ms
59,296 KB
testcase_27 AC 39 ms
10,880 KB
testcase_28 AC 39 ms
10,880 KB
testcase_29 AC 36 ms
10,752 KB
testcase_30 AC 1,289 ms
28,392 KB
testcase_31 AC 952 ms
28,512 KB
testcase_32 AC 33 ms
10,752 KB
testcase_33 AC 33 ms
10,752 KB
testcase_34 AC 33 ms
10,880 KB
testcase_35 AC 32 ms
10,752 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