結果

問題 No.649 ここでちょっとQK!
ユーザー kept1994kept1994
提出日時 2022-05-02 02:35:12
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 4,544 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 188,160 KB
最終ジャッジ日時 2023-09-13 20:16:34
合計ジャッジ時間 12,495 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,708 KB
testcase_01 AC 72 ms
71,288 KB
testcase_02 AC 74 ms
71,704 KB
testcase_03 RE -
testcase_04 AC 380 ms
188,132 KB
testcase_05 AC 409 ms
188,160 KB
testcase_06 AC 300 ms
106,288 KB
testcase_07 AC 71 ms
71,388 KB
testcase_08 AC 72 ms
71,272 KB
testcase_09 AC 73 ms
71,524 KB
testcase_10 AC 73 ms
71,396 KB
testcase_11 AC 76 ms
71,532 KB
testcase_12 AC 320 ms
121,760 KB
testcase_13 AC 316 ms
121,632 KB
testcase_14 AC 312 ms
121,620 KB
testcase_15 AC 331 ms
121,376 KB
testcase_16 AC 316 ms
121,764 KB
testcase_17 AC 348 ms
127,016 KB
testcase_18 AC 370 ms
133,580 KB
testcase_19 AC 394 ms
139,912 KB
testcase_20 AC 406 ms
142,652 KB
testcase_21 AC 428 ms
151,804 KB
testcase_22 AC 460 ms
149,944 KB
testcase_23 AC 473 ms
159,056 KB
testcase_24 AC 502 ms
161,264 KB
testcase_25 AC 533 ms
174,620 KB
testcase_26 AC 566 ms
182,264 KB
testcase_27 AC 98 ms
76,608 KB
testcase_28 AC 99 ms
76,892 KB
testcase_29 AC 95 ms
76,892 KB
testcase_30 AC 288 ms
111,856 KB
testcase_31 AC 298 ms
111,168 KB
testcase_32 AC 72 ms
71,652 KB
testcase_33 AC 71 ms
71,340 KB
testcase_34 AC 72 ms
71,740 KB
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys

MOD = 998244353

class SegTree:
    def __init__(self, monoid, bottomList, func):
        self.monoid = monoid
        self.func = func
        self.bottomLen = len(bottomList)
        self.offset = self.bottomLen        # セグ木の最下層の最初のインデックスに合わせるためのオフセット
        self.segLen = self.bottomLen * 2
        self.tree = [monoid] * self.segLen
        self.build(bottomList)

    """
    初期化
    O(self.segLen)
    """
    def build(self, seq):
        # 最下段の初期化
        for i, x in enumerate(seq, self.offset):
            self.tree[i] = x
        # ビルド
        for i in range(self.offset - 1, 0, -1):
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1])

    """
    一点加算 他演算
    O(log(self.bottomLen))
    """
    def pointAdd(self, i: int, val: int):
        i += self.offset
        self.tree[i] += val
        # self.tree[i] = self.func(self.tree[i], val) <- こっちの方が都度の修正は発生しない。再帰が遅くないか次第。
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子
    
    """
    一点更新
    O(log(self.bottomLen))
    """
    def pointUpdate(self, i: int, val: int):
        i += self.offset
        self.tree[i] = val
        while i > 1:
            i >>= 1 # 2で割って頂点に達するまで下層から遡上
            self.tree[i] = self.func(self.tree[i << 1], self.tree[i << 1 | 1]) # 必ず末尾0と1がペアになるのでor演算子
    
    """
    区間更新
    O(log(self.bottomLen))
    """
    def rangeAdd(self, l: int, r: int, val: int):
        l += self.offset
        r += self.offset
        while l < r:
            if l & 1:
                self.tree[l] = self.func(self.tree[l], val) 
                l += 1
            if r & 1:
                r -= 1
                self.tree[r] = self.func(self.tree[r], val) 
            l >>= 1
            r >>= 1
        return

    """ 区間取得
    O(log(self.bottomLen))
    """
    def getRange(self, l: int, r: int):
        l += self.offset
        r += self.offset
        vL = self.monoid
        vR = self.monoid
        while l < r:
            if l & 1:
                vL = self.func(vL, self.tree[l])
                l += 1
            if r & 1:
                r -= 1
                vR = self.func(self.tree[r], vR)
            l >>= 1
            r >>= 1
        return self.func(vL, vR)

    """ 一点取得
    O(log(self.bottomLen))
    """
    def getPoint(self, i: int):
        i += self.offset
        return self.tree[i]

    """
    二分探索
    O(log(self.bottomLen))
    ※ セグ木上の二分探索を使う場合は2べきにすること。
    """
    def queryKthItem(self, K: int):
        # print("セグ木上の二分探索を使う場合は2べきにすること。")
        index = 1
        restK = K
        while index < self.offset:
            if restK <= self.tree[index << 1]:
                index <<= 1
            else:
                restK -= self.tree[index << 1] # 左に進む場合は右側の分を差し引く。
                index <<= 1
                index += 1
        return index - self.offset

def main():
    Q, K = map(int, input().split())
    queries = []
    c = set()
    for _ in range(Q):
        query = list(map(int, input().split()))
        queries.append(query)
        if query[0] == 1:
            c.add(query[1])
    compressed = {}
    compressed_to_raw = []
    for index, val in enumerate(sorted(list(c))):
        compressed[val] = index
        compressed_to_raw.append(val)
            
    def add(x: int, y: int):
        return x + y
    import math
    decimalPart, integerPart = math.modf(math.log2(len(compressed.keys())))
    seglen = 2 ** (int(integerPart) + 1)
    seg = SegTree(0, [0] * seglen, add)
    segsum = 0

    for query in queries:
        if query[0] == 1:
            seg.pointAdd(compressed[query[1]], 1)
            segsum += 1
        else:
            # print(seg.tree)
            if segsum < K:
                print(-1)
                continue
            num = seg.queryKthItem(K)
            seg.pointUpdate(num, seg.getPoint(num) - 1)
            segsum -= 1
            print(compressed_to_raw[num])
    return

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