結果

問題 No.649 ここでちょっとQK!
ユーザー titan23titan23
提出日時 2022-06-20 18:10:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 568 ms / 3,000 ms
コード長 1,967 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 81,964 KB
実行使用メモリ 253,264 KB
最終ジャッジ日時 2024-04-21 07:23:32
合計ジャッジ時間 10,416 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,608 KB
testcase_01 AC 38 ms
52,096 KB
testcase_02 AC 37 ms
52,864 KB
testcase_03 AC 172 ms
111,052 KB
testcase_04 AC 407 ms
252,588 KB
testcase_05 AC 362 ms
253,264 KB
testcase_06 AC 258 ms
111,904 KB
testcase_07 AC 38 ms
52,608 KB
testcase_08 AC 38 ms
52,480 KB
testcase_09 AC 37 ms
52,608 KB
testcase_10 AC 38 ms
52,608 KB
testcase_11 AC 38 ms
52,352 KB
testcase_12 AC 302 ms
152,064 KB
testcase_13 AC 328 ms
152,404 KB
testcase_14 AC 294 ms
152,576 KB
testcase_15 AC 306 ms
152,320 KB
testcase_16 AC 300 ms
153,856 KB
testcase_17 AC 323 ms
141,016 KB
testcase_18 AC 350 ms
171,264 KB
testcase_19 AC 389 ms
155,728 KB
testcase_20 AC 402 ms
184,576 KB
testcase_21 AC 417 ms
165,900 KB
testcase_22 AC 447 ms
198,844 KB
testcase_23 AC 473 ms
212,204 KB
testcase_24 AC 491 ms
214,312 KB
testcase_25 AC 524 ms
181,352 KB
testcase_26 AC 568 ms
240,008 KB
testcase_27 AC 53 ms
63,644 KB
testcase_28 AC 53 ms
63,872 KB
testcase_29 AC 50 ms
62,848 KB
testcase_30 AC 262 ms
134,912 KB
testcase_31 AC 254 ms
134,400 KB
testcase_32 AC 38 ms
52,096 KB
testcase_33 AC 39 ms
52,864 KB
testcase_34 AC 38 ms
52,864 KB
testcase_35 AC 38 ms
52,608 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)

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

print('\n'.join(map(str, ans)))
0