結果

問題 No.2804 Fixer And Ratism
ユーザー mattu34mattu34
提出日時 2024-07-12 21:29:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 7,555 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 91,036 KB
最終ジャッジ日時 2024-07-12 21:29:15
合計ジャッジ時間 10,789 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
88,620 KB
testcase_01 AC 155 ms
88,700 KB
testcase_02 AC 150 ms
88,748 KB
testcase_03 AC 228 ms
90,788 KB
testcase_04 AC 216 ms
90,620 KB
testcase_05 AC 201 ms
90,480 KB
testcase_06 AC 238 ms
90,756 KB
testcase_07 AC 216 ms
90,748 KB
testcase_08 AC 195 ms
90,460 KB
testcase_09 AC 193 ms
90,624 KB
testcase_10 AC 243 ms
90,780 KB
testcase_11 AC 272 ms
90,964 KB
testcase_12 AC 241 ms
90,752 KB
testcase_13 AC 213 ms
90,740 KB
testcase_14 AC 247 ms
90,724 KB
testcase_15 AC 191 ms
90,472 KB
testcase_16 AC 245 ms
90,588 KB
testcase_17 AC 214 ms
90,452 KB
testcase_18 AC 298 ms
90,684 KB
testcase_19 AC 187 ms
90,640 KB
testcase_20 AC 188 ms
90,324 KB
testcase_21 AC 316 ms
90,876 KB
testcase_22 AC 170 ms
89,776 KB
testcase_23 AC 325 ms
90,648 KB
testcase_24 AC 271 ms
90,464 KB
testcase_25 AC 319 ms
90,876 KB
testcase_26 AC 298 ms
90,768 KB
testcase_27 AC 266 ms
90,508 KB
testcase_28 AC 310 ms
91,036 KB
testcase_29 AC 294 ms
90,792 KB
testcase_30 AC 293 ms
90,652 KB
testcase_31 AC 299 ms
90,428 KB
testcase_32 AC 303 ms
90,696 KB
testcase_33 AC 153 ms
88,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
import sys
import heapq
from heapq import heapify, heappop, heappush
import bisect
import itertools
from functools import lru_cache
from types import GeneratorType
from fractions import Fraction
import math
import copy
import random

# import numpy as np

# sys.setrecursionlimit(int(1e7))
# @lru_cache(maxsize=None) # CPython特化
# @bootstrap # PyPy特化(こっちのほうが速い) yield dfs(), yield Noneを忘れずに


def bootstrap(f, stack=[]):  # yield
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc


dxdy1 = ((0, 1), (0, -1), (1, 0), (-1, 0))  # 上下右左
dxdy2 = (
    (0, 1),
    (0, -1),
    (1, 0),
    (-1, 0),
    (1, 1),
    (-1, -1),
    (1, -1),
    (-1, 1),
)  # 8方向すべて
dxdy3 = ((0, 1), (1, 0))  # 右 or 下
dxdy4 = ((1, 1), (1, -1), (-1, 1), (-1, -1))  # 斜め
INF = float("inf")
_INF = 1 << 60
MOD = 998244353
mod = 998244353
MOD2 = 10**9 + 7
mod2 = 10**9 + 7
# memo : len([a,b,...,z])==26
# memo : 2^20 >= 10^6
# 小数の計算を避ける : x/y -> (x*big)//y  ex:big=10**9
# @:小さい文字, ~:大きい文字,None: 空の文字列
# ユークリッドの互除法:gcd(x,y)=gcd(x,y-x)
# memo : d 桁以下の p 進表記を用いると p^d-1 以下のすべての
#        非負整数を表現することができる
# memo : (X,Y) -> (X+Y,X−Y) <=> 点を原点を中心に45度回転し、√2倍に拡大
# memo : (x,y)のx正から見た偏角をラジアンで(-πからπ]: math.atan2(y, x)
# memo : a < bのとき ⌊a⌋ ≦ ⌊b⌋

input = lambda: sys.stdin.readline().rstrip()
mi = lambda: map(int, input().split())
li = lambda: list(mi())
ii = lambda: int(input())
py = lambda: print("Yes")
pn = lambda: print("No")
pf = lambda: print("First")
ps = lambda: print("Second")


# https://github.com/tatyam-prime/SortedSet/blob/main/SortedMultiset.py
import math
from bisect import bisect_left, bisect_right, insort
from typing import Generic, Iterable, Iterator, TypeVar, Union, List

T = TypeVar("T")


class SortedMultiset(Generic[T]):
    BUCKET_RATIO = 50
    REBUILD_RATIO = 170

    def _build(self, a=None) -> None:
        "Evenly divide `a` into buckets."
        if a is None:
            a = list(self)
        size = self.size = len(a)
        bucket_size = int(math.ceil(math.sqrt(size / self.BUCKET_RATIO)))
        self.a = [
            a[size * i // bucket_size : size * (i + 1) // bucket_size]
            for i in range(bucket_size)
        ]

    def __init__(self, a: Iterable[T] = []) -> None:
        "Make a new SortedMultiset from iterable. / O(N) if sorted / O(N log N)"
        a = list(a)
        if not all(a[i] <= a[i + 1] for i in range(len(a) - 1)):
            a = sorted(a)
        self._build(a)

    def __iter__(self) -> Iterator[T]:
        for i in self.a:
            for j in i:
                yield j

    def __reversed__(self) -> Iterator[T]:
        for i in reversed(self.a):
            for j in reversed(i):
                yield j

    def __len__(self) -> int:
        return self.size

    def __repr__(self) -> str:
        return "SortedMultiset" + str(self.a)

    def __str__(self) -> str:
        s = str(list(self))
        return "{" + s[1 : len(s) - 1] + "}"

    def _find_bucket(self, x: T) -> List[T]:
        "Find the bucket which should contain x. self must not be empty."
        for a in self.a:
            if x <= a[-1]:
                return a
        return a

    def __contains__(self, x: T) -> bool:
        if self.size == 0:
            return False
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        return i != len(a) and a[i] == x

    def count(self, x: T) -> int:
        "Count the number of x."
        return self.index_right(x) - self.index(x)

    def add(self, x: T) -> None:
        "Add an element. / O(√N)"
        if self.size == 0:
            self.a = [[x]]
            self.size = 1
            return
        a = self._find_bucket(x)
        insort(a, x)
        self.size += 1
        if len(a) > len(self.a) * self.REBUILD_RATIO:
            self._build()

    def discard(self, x: T) -> bool:
        "Remove an element and return True if removed. / O(√N)"
        if self.size == 0:
            return False
        a = self._find_bucket(x)
        i = bisect_left(a, x)
        if i == len(a) or a[i] != x:
            return False
        a.pop(i)
        self.size -= 1
        if len(a) == 0:
            self._build()
        return True

    def lt(self, x: T) -> Union[T, None]:
        "Find the largest element < x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] < x:
                return a[bisect_left(a, x) - 1]

    def le(self, x: T) -> Union[T, None]:
        "Find the largest element <= x, or None if it doesn't exist."
        for a in reversed(self.a):
            if a[0] <= x:
                return a[bisect_right(a, x) - 1]

    def gt(self, x: T) -> Union[T, None]:
        "Find the smallest element > x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] > x:
                return a[bisect_right(a, x)]

    def ge(self, x: T) -> Union[T, None]:
        "Find the smallest element >= x, or None if it doesn't exist."
        for a in self.a:
            if a[-1] >= x:
                return a[bisect_left(a, x)]

    def __getitem__(self, x: int) -> T:
        "Return the x-th element, or IndexError if it doesn't exist."
        if x < 0:
            x += self.size
        if x < 0:
            raise IndexError
        for a in self.a:
            if x < len(a):
                return a[x]
            x -= len(a)
        raise IndexError

    def index(self, x: T) -> int:
        "Count the number of elements < x."
        ans = 0
        for a in self.a:
            if a[-1] >= x:
                return ans + bisect_left(a, x)
            ans += len(a)
        return ans

    def index_right(self, x: T) -> int:
        "Count the number of elements <= x."
        ans = 0
        for a in self.a:
            if a[-1] > x:
                return ans + bisect_right(a, x)
            ans += len(a)
        return ans


ms = SortedMultiset()
ms_g = SortedMultiset()
rate = defaultdict(int)
N, Q = mi()
pc = N
for _ in range(Q):
    cmd, *line = map(str, input().split())
    if cmd == "1":
        s, r = line
        rate[s] = int(r)
        ms.add((int(r), s))
    elif cmd == "2":
        x = line[0]
        pc -= int(x)
    else:
        s, x = line
        pc += int(x)
        ms.discard((rate[s], s))
        if (rate[s], s) not in ms_g:
            ms_g.add((rate[s], s))
    tarinai = len(ms) + len(ms_g) - pc
    kitaku = []
    if tarinai > 0:
        while ms and tarinai > 0:
            r, s = ms[0]
            kitaku.append((r, s))
            ms.discard((r, s))
            tarinai -= 1
        while ms_g and tarinai > 0:
            r, s = ms_g[0]
            kitaku.append((r, s))
            ms_g.discard((r, s))
            tarinai -= 1
        kitaku.sort()
        for _, name in kitaku:
            print(name)
    # print(ms_g)
    # print(ms)
    # print("--")
0