結果
問題 | No.649 ここでちょっとQK! |
ユーザー | kept1994 |
提出日時 | 2022-05-01 02:48:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 682 ms / 3,000 ms |
コード長 | 4,899 bytes |
コンパイル時間 | 499 ms |
コンパイル使用メモリ | 86,948 KB |
実行使用メモリ | 186,256 KB |
最終ジャッジ日時 | 2023-09-12 17:53:25 |
合計ジャッジ時間 | 14,402 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 70 ms
70,720 KB |
testcase_01 | AC | 69 ms
71,152 KB |
testcase_02 | AC | 69 ms
71,180 KB |
testcase_03 | AC | 252 ms
105,340 KB |
testcase_04 | AC | 412 ms
186,256 KB |
testcase_05 | AC | 370 ms
186,108 KB |
testcase_06 | AC | 280 ms
104,680 KB |
testcase_07 | AC | 70 ms
71,080 KB |
testcase_08 | AC | 70 ms
71,260 KB |
testcase_09 | AC | 71 ms
71,160 KB |
testcase_10 | AC | 71 ms
71,120 KB |
testcase_11 | AC | 71 ms
70,928 KB |
testcase_12 | AC | 390 ms
120,116 KB |
testcase_13 | AC | 395 ms
120,332 KB |
testcase_14 | AC | 388 ms
120,688 KB |
testcase_15 | AC | 410 ms
120,224 KB |
testcase_16 | AC | 342 ms
120,220 KB |
testcase_17 | AC | 404 ms
125,872 KB |
testcase_18 | AC | 424 ms
132,752 KB |
testcase_19 | AC | 455 ms
139,084 KB |
testcase_20 | AC | 473 ms
141,868 KB |
testcase_21 | AC | 534 ms
149,396 KB |
testcase_22 | AC | 530 ms
147,724 KB |
testcase_23 | AC | 575 ms
155,764 KB |
testcase_24 | AC | 601 ms
158,184 KB |
testcase_25 | AC | 648 ms
172,108 KB |
testcase_26 | AC | 682 ms
177,948 KB |
testcase_27 | AC | 113 ms
77,636 KB |
testcase_28 | AC | 109 ms
77,372 KB |
testcase_29 | AC | 101 ms
76,756 KB |
testcase_30 | AC | 364 ms
111,796 KB |
testcase_31 | AC | 319 ms
110,752 KB |
testcase_32 | AC | 71 ms
71,044 KB |
testcase_33 | AC | 70 ms
71,056 KB |
testcase_34 | AC | 71 ms
71,340 KB |
testcase_35 | AC | 71 ms
71,296 KB |
ソースコード
#!/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] 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 seg = SegTree(0, [0] * len(compressed.keys()), add) segsum = 0 # True ------ ok | ng ---- False def is_ok(k: int, threshold: int): return seg.getRange(0, k + 1) < threshold # 条件式 def binSearch(ok: int, ng: int, threshold: int): # print(ok, ng) # はじめの2値の状態 while abs(ok - ng) > 1: # 終了条件(差が1となり境界を見つけた時) mid = (ok + ng) // 2 # print("target > ", mid) result = is_ok(mid, threshold) # print(result) if result: ok = mid # midが条件を満たすならmidまではokなのでokの方を真ん中まで持っていく else: ng = mid # midが条件を満たさないならmidまではngなのでngの方を真ん中まで持っていく # print(ok, ng) # 半分に切り分ける毎の2値の状態 return ng # 関数呼び出し時の引数のngは絶対評価されないのでngに書く値が答えになりうるならその数マイナス1を指定する。 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 = binSearch(-1, len(compressed.keys()), K) seg.pointUpdate(num, seg.getPoint(num) - 1) segsum -= 1 print(compressed_to_raw[num]) return if __name__ == '__main__': main()