結果

問題 No.649 ここでちょっとQK!
ユーザー Kenya ITOHKenya ITOH
提出日時 2023-05-06 13:00:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 412 ms / 3,000 ms
コード長 1,398 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 167,852 KB
最終ジャッジ日時 2024-05-03 01:29:36
合計ジャッジ時間 9,494 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
51,968 KB
testcase_01 AC 40 ms
52,608 KB
testcase_02 AC 40 ms
52,096 KB
testcase_03 AC 203 ms
97,488 KB
testcase_04 AC 281 ms
167,852 KB
testcase_05 AC 279 ms
167,760 KB
testcase_06 AC 235 ms
97,200 KB
testcase_07 AC 32 ms
54,092 KB
testcase_08 AC 31 ms
52,492 KB
testcase_09 AC 31 ms
52,652 KB
testcase_10 AC 31 ms
52,828 KB
testcase_11 AC 31 ms
52,812 KB
testcase_12 AC 233 ms
117,000 KB
testcase_13 AC 239 ms
116,696 KB
testcase_14 AC 233 ms
116,888 KB
testcase_15 AC 247 ms
117,248 KB
testcase_16 AC 238 ms
116,992 KB
testcase_17 AC 272 ms
108,996 KB
testcase_18 AC 275 ms
126,468 KB
testcase_19 AC 290 ms
131,888 KB
testcase_20 AC 326 ms
134,100 KB
testcase_21 AC 335 ms
139,136 KB
testcase_22 AC 341 ms
140,828 KB
testcase_23 AC 370 ms
143,448 KB
testcase_24 AC 376 ms
139,648 KB
testcase_25 AC 410 ms
156,448 KB
testcase_26 AC 412 ms
136,600 KB
testcase_27 AC 54 ms
67,072 KB
testcase_28 AC 54 ms
66,304 KB
testcase_29 AC 52 ms
65,664 KB
testcase_30 AC 206 ms
107,780 KB
testcase_31 AC 227 ms
107,572 KB
testcase_32 AC 35 ms
52,352 KB
testcase_33 AC 32 ms
52,096 KB
testcase_34 AC 32 ms
52,352 KB
testcase_35 AC 34 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#BITを用いた実装
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
  
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
  
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def lower_bound(self,w):
        '''
        a1+a2+...+ax>=wとなるような最小のxを求める
        https://algo-logic.info/binary-indexed-tree/
        '''
        if w<=0:
            return 0
        else:
            x = 0
            r = 1
            while r < self.size:
                r = r<<1
            lenth = r
            S = 0
            while lenth > 0:
                if lenth + x < self.size and self.tree[x+lenth] < w:
                    w -= self.tree[x+lenth]
                    x += lenth
                lenth = lenth>>1
            return x+1

Q,K = map(int,input().split())
query = [list(map(int,input().split())) for _ in range(Q)]

V = set()
for q in query:
    if q[0] == 1:
        V.add(q[1])

V = sorted(V)
d = {v:i for i,v in enumerate(V)}

bit = Bit(len(V))

for q in query:
    if q[0] == 1:
        bit.add(d[q[1]]+1,1)
    else:
        if bit.sum(len(V)) < K:
            print(-1)
        else:
            x = bit.lower_bound(K)
            print(V[x-1])
            bit.add(x,-1)
0