結果

問題 No.649 ここでちょっとQK!
ユーザー yuly3yuly3
提出日時 2020-04-22 11:16:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 536 ms / 3,000 ms
コード長 2,686 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 172,844 KB
最終ジャッジ日時 2024-04-19 10:29:18
合計ジャッジ時間 10,381 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
54,584 KB
testcase_01 AC 36 ms
54,420 KB
testcase_02 AC 36 ms
55,472 KB
testcase_03 AC 144 ms
115,396 KB
testcase_04 AC 269 ms
165,940 KB
testcase_05 AC 270 ms
165,572 KB
testcase_06 AC 220 ms
116,860 KB
testcase_07 AC 36 ms
54,840 KB
testcase_08 AC 35 ms
55,136 KB
testcase_09 AC 35 ms
54,664 KB
testcase_10 AC 38 ms
55,052 KB
testcase_11 AC 37 ms
54,740 KB
testcase_12 AC 292 ms
125,084 KB
testcase_13 AC 302 ms
125,328 KB
testcase_14 AC 294 ms
125,292 KB
testcase_15 AC 306 ms
125,256 KB
testcase_16 AC 253 ms
125,060 KB
testcase_17 AC 297 ms
129,664 KB
testcase_18 AC 315 ms
136,288 KB
testcase_19 AC 355 ms
141,904 KB
testcase_20 AC 369 ms
144,104 KB
testcase_21 AC 400 ms
152,664 KB
testcase_22 AC 428 ms
145,620 KB
testcase_23 AC 437 ms
152,084 KB
testcase_24 AC 455 ms
144,812 KB
testcase_25 AC 490 ms
172,844 KB
testcase_26 AC 536 ms
147,012 KB
testcase_27 AC 54 ms
70,208 KB
testcase_28 AC 55 ms
69,152 KB
testcase_29 AC 50 ms
67,788 KB
testcase_30 AC 256 ms
111,528 KB
testcase_31 AC 240 ms
111,300 KB
testcase_32 AC 35 ms
54,936 KB
testcase_33 AC 34 ms
55,680 KB
testcase_34 AC 35 ms
55,236 KB
testcase_35 AC 35 ms
55,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict
from operator import add

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


class SegmentTree:
    def __init__(self, init_value: list, segfunc, ide_ele):
        n = len(init_value)
        self.N0 = 1 << (n - 1).bit_length()
        self.ide_ele = ide_ele
        self.data = [ide_ele] * (2 * self.N0)
        self.segfunc = segfunc
        
        for i, x in enumerate(init_value):
            self.data[i + self.N0 - 1] = x
        for i in range(self.N0 - 2, -1, -1):
            self.data[i] = self.segfunc(self.data[2 * i + 1], self.data[2 * i + 2])
    
    def update(self, k, x):
        k += self.N0 - 1
        ################################################################
        self.data[k] = x
        ################################################################
        while k:
            k = (k - 1) // 2
            self.data[k] = self.segfunc(self.data[k * 2 + 1], self.data[k * 2 + 2])
    
    def query(self, left, right):
        L = left + self.N0
        R = right + self.N0
        res = self.ide_ele
        ################################################################
        
        while L < R:
            if L & 1:
                res = self.segfunc(res, self.data[L - 1])
                L += 1
            if R & 1:
                R -= 1
                res = self.segfunc(res, self.data[R - 1])
            L >>= 1
            R >>= 1
        
        ################################################################
        return res


def solve():
    Q, K = map(int, rl().split())
    query = [list(map(int, rl().split())) for _ in range(Q)]
    
    compress = []
    for i in range(Q):
        if query[i][0] == 1:
            compress.append(query[i][1])
    compress.sort()
    v_to_idx = {vi: idx for idx, vi in enumerate(compress)}
    counter = defaultdict(int)
    for vi in compress:
        counter[vi] += 1
    N = len(compress)
    
    seg_tree = SegmentTree([0] * N, add, 0)
    ans = []
    for qi in query:
        if qi[0] == 1:
            val = qi[1]
            seg_tree.update(v_to_idx[val] - counter[val] + 1, 1)
            counter[val] -= 1
        else:
            if seg_tree.query(0, N) < K:
                ans.append(-1)
            else:
                ok, ng = N - 1, -1
                while 1 < ok - ng:
                    mid = (ok + ng) // 2
                    if K <= seg_tree.query(0, mid + 1):
                        ok = mid
                    else:
                        ng = mid
                ans.append(compress[ok])
                seg_tree.update(ok, 0)
    print(*ans, sep='\n')


if __name__ == '__main__':
    solve()
0