結果
問題 | No.649 ここでちょっとQK! |
ユーザー |
|
提出日時 | 2022-07-24 22:24:00 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,560 bytes |
コンパイル時間 | 202 ms |
コンパイル使用メモリ | 82,588 KB |
実行使用メモリ | 181,716 KB |
最終ジャッジ日時 | 2024-07-06 17:41:51 |
合計ジャッジ時間 | 12,084 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 28 WA * 4 |
ソースコード
class SegmentTree(object):def __init__(self, v, op, e):self._n = len(v)self.op = opself.e = eself.log = SegmentTree._ceil_pow2(self._n)self.size = 1 << self.logself.d = [self.e] * 2 * self.sizefor i in range(self._n):self.d[self.size + i] = v[i]for i in range(self.size - 1, 0, -1):self._update(i)@staticmethoddef _ceil_pow2(n):for x in range(64):if 1 << x >= n:breakreturn xdef _update(self, k):self.d[k] = self.op(self.d[2 * k], self.d[2 * k + 1])def __getitem__(self, p):assert 0 <= p < self._nreturn self.d[p + self.size]def __setitem__(self, p, x):assert 0 <= p < self._np += self.sizeself.d[p] = xfor i in range(1, self.log + 1):self._update(p >> i)def __str__(self):return str(self.d[self.size:])def query(self, l, r):if l is ...:l = 0if r is ...:r = self._nassert 0 <= l <= r <= self._nsml = self.esmr = self.el += self.sizer += self.sizewhile l < r:if l & 1:sml = self.op(sml, self.d[l])l += 1if r & 1:r -= 1smr = self.op(self.d[r], smr)l >>= 1r >>= 1return self.op(sml, smr)def max_right(self, l, f):if l is ...:l = 0assert 0 <= l <= self._nassert f(self.e)if l == self._n:return self._nl += self.sizesm = self.ewhile True:while l & 1 == 0:l >>= 1if not f(self.op(sm, self.d[l])):while l < self.size:l <<= 1if f(self.op(sm, self.d[l])):sm = self.op(sm, self.d[l])l += 1return l - self.sizesm = self.op(sm, self.d[l])l += 1if (l & -l) == l:breakreturn self._ndef min_left(self, r, f):if r is ...:r = self._nassert 0 <= r <= self._nassert f(self.e)if r == 0:return 0r += self.sizesm = self.ewhile True:r -= 1while r > 1 and r & 1:r >>= 1if not f(self.op(self.d[r], sm)):while r < self.size:r <<= 1r += 1if f(self.op(self.d[r], sm)):sm = self.op(self.d[r], sm)r -= 1return r + 1 - self.sizesm = self.op(self.d[r], sm)if (r & -r) == r:breakreturn 0class _CountOrItem(object):def __init__(self, _tree_map):self._tree = _tree_map._treeself._g = _tree_map._gdef _bind(self, l, r):self._l = lself._r = rdef _predicate(self, s):return s < self._tree.query(self._l, self._r)def count(self):return self._tree.query(self._l, self._r)def item(self):try:if self._l is ...:i = self._tree.max_right(..., self._predicate)else:i = self._tree.min_left(..., self._predicate) - 1except AssertionError:return Nonereturn self._g[i] if 0 <= i < len(self._g) else Nonefrom operator import addclass LimitedTreeMap(object):def __init__(self, domain):self._g = sorted(set(domain))self._f = {v: k for k, v in enumerate(self._g)}self._tree = SegmentTree([0] * len(self._g), add, 0)self._count_or_item = _CountOrItem(self)def __setitem__(self, p, x):p = self._f[p]self._tree[p] = xdef __getitem__(self, p):p = self._f[p]return self._tree[p]def __str__(self):return str({v: self._tree[k] for v, k in self._f.items() if self._tree[k]})def __repr__(self):return str({v: self._tree[k] for v, k in self._f.items() if self._tree[k]})def __lt__(self, p):p = self._f[p]self._count_or_item._bind(..., p)return self._count_or_itemdef __le__(self, p):p = self._f[p]self._count_or_item._bind(..., p + 1)return self._count_or_itemdef __gt__(self, p):p = self._f[p]self._count_or_item._bind(p + 1, ...)return self._count_or_itemdef __ge__(self, p):p = self._f[p]self._count_or_item._bind(p, ...)return self._count_or_itemdef k_smallest(self, k):i = self._tree.max_right(..., lambda s: s < k + 1)return self._g[i] if 0 <= i < len(self._g) else Nonedef k_largest(self, k):i = self._tree.min_left(..., lambda s: s < k + 1) - 1return self._g[i] if 0 <= i < len(self._g) else Noneq, k = map(int, input().split())domain = []queries = []for _ in range(q):t, *args = map(int, input().split())if t == 1:domain.append(*args)queries.append((t, *args))tree_map = LimitedTreeMap(domain)for t, *args in queries:if t == 1:x, = argstree_map[x] += 1else:x = tree_map.k_smallest(k - 1) or -1if x >= 0:tree_map[x] -= 1print(x)