cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kobejean/cp-library

:warning: cp_library/ds/heap/min_multiset_cls.py

Depends on

Code

import cp_library.__header__
from collections import Counter, UserList
from typing import Iterable
from math import inf
from cp_library.misc.typing import _T
import cp_library.ds.__header__
import cp_library.ds.heap.__header__
from cp_library.ds.heap.heappop_fn import heappop
from cp_library.ds.heap.heappush_fn import heappush

class MinMultiset(UserList[_T]):
    def __init__(self, iterable: Iterable = None, default = inf): self.data = list(iterable) if iterable else []; self.default = default; self.counter = Counter(self.data)
    def add(self, x: _T): self.counter[x] += 1; heappush(self.data, x)
    def remove(self, x: _T):
        cnt, data = self.counter, self.data; cnt[x] -= 1
        while data and cnt[data[0]] == 0: heappop(data)
    @property
    def min(self): return self.data[0] if self.data else self.default
'''
╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
             https://kobejean.github.io/cp-library               
'''
from collections import Counter, UserList
from typing import Iterable
from math import inf
from typing import TypeVar

_S = TypeVar('S'); _T = TypeVar('T'); _U = TypeVar('U'); _T1 = TypeVar('T1'); _T2 = TypeVar('T2'); _T3 = TypeVar('T3'); _T4 = TypeVar('T4'); _T5 = TypeVar('T5'); _T6 = TypeVar('T6')



def heapsiftup(heap: list, pos: int):
    n, item, c = len(heap)-1, heap[pos], pos<<1|1
    while c < n and heap[c := c+(heap[c+1]<heap[c])] < item: heap[pos], pos, c = heap[c], c, c<<1|1
    if c == n and heap[c] < item: heap[pos], pos = heap[c], c
    heap[pos] = item

def heappop(heap: list):
    item = heap.pop()
    if heap: item, heap[0] = heap[0], item; heapsiftup(heap, 0)
    return item

def heapsiftdown(heap: list, root: int, pos: int):
    item = heap[pos]
    while root < pos and item < heap[p := (pos-1)>>1]: heap[pos], pos = heap[p], p
    heap[pos] = item

def heappush(heap: list, item):
    heap.append(item)
    heapsiftdown(heap, 0, len(heap)-1)

class MinMultiset(UserList[_T]):
    def __init__(self, iterable: Iterable = None, default = inf): self.data = list(iterable) if iterable else []; self.default = default; self.counter = Counter(self.data)
    def add(self, x: _T): self.counter[x] += 1; heappush(self.data, x)
    def remove(self, x: _T):
        cnt, data = self.counter, self.data; cnt[x] -= 1
        while data and cnt[data[0]] == 0: heappop(data)
    @property
    def min(self): return self.data[0] if self.data else self.default
Back to top page