結果

問題 No.649 ここでちょっとQK!
ユーザー titan23titan23
提出日時 2022-06-20 18:12:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 545 ms / 3,000 ms
コード長 1,915 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 252,728 KB
最終ジャッジ日時 2024-04-21 07:25:42
合計ジャッジ時間 10,799 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,608 KB
testcase_01 AC 39 ms
52,096 KB
testcase_02 AC 38 ms
52,224 KB
testcase_03 AC 172 ms
96,896 KB
testcase_04 AC 381 ms
252,468 KB
testcase_05 AC 364 ms
252,728 KB
testcase_06 AC 241 ms
112,008 KB
testcase_07 AC 37 ms
52,960 KB
testcase_08 AC 36 ms
52,708 KB
testcase_09 AC 35 ms
52,960 KB
testcase_10 AC 46 ms
52,816 KB
testcase_11 AC 39 ms
53,412 KB
testcase_12 AC 309 ms
152,172 KB
testcase_13 AC 306 ms
152,240 KB
testcase_14 AC 299 ms
152,280 KB
testcase_15 AC 309 ms
151,992 KB
testcase_16 AC 310 ms
153,916 KB
testcase_17 AC 320 ms
141,132 KB
testcase_18 AC 367 ms
171,636 KB
testcase_19 AC 383 ms
155,612 KB
testcase_20 AC 403 ms
184,448 KB
testcase_21 AC 430 ms
166,156 KB
testcase_22 AC 433 ms
198,792 KB
testcase_23 AC 461 ms
212,016 KB
testcase_24 AC 479 ms
214,040 KB
testcase_25 AC 517 ms
181,432 KB
testcase_26 AC 545 ms
239,808 KB
testcase_27 AC 56 ms
63,616 KB
testcase_28 AC 59 ms
63,744 KB
testcase_29 AC 54 ms
63,232 KB
testcase_30 AC 272 ms
134,784 KB
testcase_31 AC 259 ms
134,144 KB
testcase_32 AC 40 ms
52,464 KB
testcase_33 AC 38 ms
52,736 KB
testcase_34 AC 37 ms
52,096 KB
testcase_35 AC 38 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()

class FenwickTree:

  def __init__(self, n: int):
    "Build a new fenwick tree. / O(N)"
    assert 0 <= n <= 10**8
    self._size = n
    self._tree = [0] * (n+1)
    self._depth = n.bit_length()

  def _sum(self, i):
    "Return sum([0, i)) of a. / O(logN)"
    i += 1  # 1-indexed
    ret = 0
    while i > 0:
      ret += self._tree[i]
      i -= i & -i
    return ret

  def sum(self, l: int, r: int):
    "Return sum([l, r)] of a. / O(logN)"
    assert 0 <= l <= r <= self._size
    return self._sum(r-1) - self._sum(l-1)

  def add(self, p: int, x) -> None:
    "Add x to a[p]. / O(logN)"
    p += 1  # 1-indexed.
    assert 1 <= p <= self._size
    while p <= self._size:
      self._tree[p] += x
      p += p & -p
    return

  def lower_bound(self, w):
    "Return 累積和がx以上になる最小のindexと、その直前までの累積和 / O(logN)"
    '''
    FenwickTreeを集合として管理
    add(a, 1) -> 集合にaを追加
    add(a,-1) -> 集合からaを削除
    sum(a)    -> aが何番目に小さいかを返す
    lower_bound(w) -> w番目に小さい要素を返す
    '''
    acc, pos = 0, 0
    for i in range(self._depth, -1, -1):
      k = pos + (1 << i)
      if k <= self._size and acc + self._tree[k] < w:
        acc += self._tree[k]
        pos += 1 << i
    return pos+1, acc

##################

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

A = []
for q in qu:
  if q[0] == 1:
    v = q[1]
    A.append(v)

dictonum = {x:i for i,x in enumerate(sorted(list(set(A))))}
dictox = {i:x for i,x in enumerate(sorted(list(set(A))))}
n = len(A)
fw = FenwickTree(n)

for q in qu:
  if q[0] == 1:
    v = dictonum[q[1]]
    fw.add(v, 1)
  else:
    if fw.sum(0, n) < K:
      print(-1)
    else:
      w = fw.lower_bound(K)[0]
      print(dictox[w-1])
      fw.add(w-1, -1)
0