結果

問題 No.649 ここでちょっとQK!
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-09-04 14:19:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 849 ms / 3,000 ms
コード長 2,802 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 145,408 KB
最終ジャッジ日時 2024-04-29 14:35:51
合計ジャッジ時間 14,180 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
56,192 KB
testcase_01 AC 52 ms
56,064 KB
testcase_02 AC 54 ms
56,192 KB
testcase_03 AC 176 ms
89,728 KB
testcase_04 AC 337 ms
145,408 KB
testcase_05 AC 337 ms
145,280 KB
testcase_06 AC 300 ms
107,648 KB
testcase_07 AC 55 ms
56,320 KB
testcase_08 AC 56 ms
56,192 KB
testcase_09 AC 53 ms
55,808 KB
testcase_10 AC 53 ms
55,936 KB
testcase_11 AC 54 ms
56,192 KB
testcase_12 AC 530 ms
107,136 KB
testcase_13 AC 529 ms
106,880 KB
testcase_14 AC 512 ms
107,392 KB
testcase_15 AC 539 ms
107,904 KB
testcase_16 AC 330 ms
107,136 KB
testcase_17 AC 436 ms
110,976 KB
testcase_18 AC 475 ms
114,816 KB
testcase_19 AC 571 ms
118,144 KB
testcase_20 AC 587 ms
120,320 KB
testcase_21 AC 632 ms
124,032 KB
testcase_22 AC 669 ms
125,184 KB
testcase_23 AC 725 ms
130,944 KB
testcase_24 AC 759 ms
122,704 KB
testcase_25 AC 805 ms
137,856 KB
testcase_26 AC 849 ms
140,416 KB
testcase_27 AC 88 ms
73,984 KB
testcase_28 AC 96 ms
76,672 KB
testcase_29 AC 78 ms
70,144 KB
testcase_30 AC 338 ms
102,912 KB
testcase_31 AC 309 ms
102,400 KB
testcase_32 AC 53 ms
56,064 KB
testcase_33 AC 53 ms
55,808 KB
testcase_34 AC 52 ms
55,680 KB
testcase_35 AC 54 ms
56,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

Q,K = map(int,input().split())

import bisect

class BIT:

    def __init__(self,len_A):
        self.N = len_A + 10
        self.bit = [0]*(len_A+10)
        
    # sum(A0 ~ Ai)
    # O(log N)
    def query(self,i):
        res = 0
        idx = i+1
        while idx:
            res += self.bit[idx]
            idx -= idx&(-idx)
        return res

    # Ai += x
    # O(log N)
    def update(self,i,x):
        idx = i+1
        while idx < self.N:
            self.bit[idx] += x
            idx += idx&(-idx)
    
    # min_i satisfying {sum(A0 ~ Ai) >= w} (Ai >= 0)
    # O(log N)
    def lower_left(self,w):
        if (w < 0):
            return -1
        x = 0
        k = 1<<(self.N.bit_length()-1)
        while k > 0:
            if x+k < self.N and self.bit[x+k] < w:
                w -= self.bit[x+k]
                x += k
            k //= 2
        return x


class OrderBIT:

    def __init__(self,all_values,sort_flag = False):
        if sort_flag:
            self.A = all_values
        else:
            self.A = sorted(all_values)
        self.B = BIT(len(all_values))
        self.num = 0
        
    def insert_val(self,x,c=1):
        k = bisect.bisect_left(self.A,x)
        self.B.update(k,c)
        self.num += c
    
    def delete_val(self,x,c=1):
        k = bisect.bisect_left(self.A,x)
        self.B.update(k,-c)
        self.num -= c
    
    # find the k-th min_val (k:0-indexed)
    def find_kth_val(self,k):
        if self.num <= k:
            ##### MINIMUM VAL #######
            return -10**9
        return self.A[self.B.lower_left(k+1)]
    
    # count the number of values lower than or equal to x
    def count_lower(self,x):
        if x < self.A[0]:
            return 0
        return self.B.query(bisect.bisect_right(self.A,x)-1)

    # min_val higher than x
    def find_higher(self,x):
        return self.find_kth_val(self.count_lower(x))
        
    # min_val lower than x
    def find_lower(self,x):
        return self.find_kth_val(self.count_lower(x)-1)
query = [tuple(map(int,input().split())) for _ in range(Q)]
v = []
for i in range(Q):
    if len(query[i])==2:
        v.append(query[i][1])
v = list(set(v))
if len(v)!=0:
    M = max(v)+1
T = OrderBIT(v)
N = 0
for q in query:
    
    if q[0]==1:
        N += 1
        T.insert_val(q[1])
        continue
    
    if N < K:
        print(-1)
        continue
    y = M
    x = -1
    while y-x>1:
        mid = (y+x)//2
        if T.count_lower(mid) >=K:
            y = mid
        else:
            x = mid
    T.delete_val(y)
    print(y)
    N -= 1
    
    
    
        
0