結果

問題 No.1705 Mode of long array
ユーザー H3PO4
提出日時 2021-10-08 23:45:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 444 ms / 3,000 ms
コード長 1,335 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 119,472 KB
最終ジャッジ日時 2024-07-23 08:12:08
合計ジャッジ時間 15,570 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq
from collections import Counter

input = sys.stdin.buffer.readline


class Counter_min(Counter):
    def __init__(self, iterable=tuple()):
        super(Counter_min, self).__init__(iterable)
        self.h = list(set(iterable))
        heapq.heapify(self.h)

    def add(self, x):
        if x in self:
            self[x] += 1
        else:
            self[x] = 1
            heapq.heappush(self.h, x)

    def remove(self, x):
        if x not in self:
            raise ValueError
        if self[x] == 1:
            del self[x]
        else:
            self[x] -= 1

    def min(self):
        if not super(Counter_min, self).__len__():
            raise ValueError
        while self.h and self.h[0] not in self:
            heapq.heappop(self.h)
        return self.h[0]


N, M = map(int, input().split())
A = list(map(int, input().split()))
ct = Counter_min()
for i, a in enumerate(A):
    ct.add((-a, -i))

Q = int(input())
for _ in range(Q):
    T, X, Y = map(int, input().split())
    if T == 1:
        X -= 1
        ct.remove((-A[X], -X))
        A[X] += Y
        ct.add((-A[X], -X))
    elif T == 2:
        X -= 1
        ct.remove((-A[X], -X))
        A[X] -= Y
        ct.add((-A[X], -X))
    else:
        # assert T == 3
        _, ans = ct.min()
        ans = 1 - ans
        print(ans)
0