結果

問題 No.649 ここでちょっとQK!
ユーザー kept1994kept1994
提出日時 2022-05-02 02:45:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 616 ms / 3,000 ms
コード長 4,708 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 188,116 KB
最終ジャッジ日時 2023-09-13 20:29:03
合計ジャッジ時間 12,193 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,524 KB
testcase_01 AC 72 ms
71,276 KB
testcase_02 AC 72 ms
71,548 KB
testcase_03 AC 265 ms
105,660 KB
testcase_04 AC 395 ms
188,096 KB
testcase_05 AC 395 ms
188,116 KB
testcase_06 AC 323 ms
106,152 KB
testcase_07 AC 72 ms
71,188 KB
testcase_08 AC 74 ms
71,372 KB
testcase_09 AC 73 ms
71,428 KB
testcase_10 AC 73 ms
71,248 KB
testcase_11 AC 71 ms
71,240 KB
testcase_12 AC 346 ms
121,332 KB
testcase_13 AC 356 ms
121,548 KB
testcase_14 AC 337 ms
121,540 KB
testcase_15 AC 345 ms
121,120 KB
testcase_16 AC 332 ms
121,316 KB
testcase_17 AC 370 ms
127,028 KB
testcase_18 AC 392 ms
133,648 KB
testcase_19 AC 415 ms
139,848 KB
testcase_20 AC 440 ms
142,724 KB
testcase_21 AC 468 ms
151,788 KB
testcase_22 AC 491 ms
150,120 KB
testcase_23 AC 515 ms
159,084 KB
testcase_24 AC 549 ms
160,740 KB
testcase_25 AC 578 ms
174,516 KB
testcase_26 AC 616 ms
180,012 KB
testcase_27 AC 97 ms
76,888 KB
testcase_28 AC 98 ms
76,812 KB
testcase_29 AC 95 ms
76,812 KB
testcase_30 AC 303 ms
111,712 KB
testcase_31 AC 294 ms
111,268 KB
testcase_32 AC 73 ms
71,732 KB
testcase_33 AC 73 ms
71,328 KB
testcase_34 AC 71 ms
71,680 KB
testcase_35 AC 71 ms
71,096 KB
権限があれば一括ダウンロードができます

ソースコード

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
    def getSegLenOfThePowerOf2(ln: int):
        if ln <= 0:
            return 1
        else:    
            import math
            decimalPart, integerPart = math.modf(math.log2(ln))
            return 2 ** (int(integerPart) + 1)


    seglen = getSegLenOfThePowerOf2(len(compressed.keys()))
    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