結果

問題 No.1705 Mode of long array
ユーザー 👑 KazunKazun
提出日時 2021-10-08 22:27:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 673 ms / 3,000 ms
コード長 3,259 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 86,816 KB
実行使用メモリ 187,544 KB
最終ジャッジ日時 2023-09-30 11:01:42
合計ジャッジ時間 24,579 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,552 KB
testcase_01 AC 94 ms
71,544 KB
testcase_02 AC 94 ms
71,656 KB
testcase_03 AC 141 ms
78,424 KB
testcase_04 AC 130 ms
77,644 KB
testcase_05 AC 143 ms
78,108 KB
testcase_06 AC 191 ms
78,672 KB
testcase_07 AC 200 ms
79,276 KB
testcase_08 AC 215 ms
79,764 KB
testcase_09 AC 200 ms
79,648 KB
testcase_10 AC 196 ms
79,500 KB
testcase_11 AC 197 ms
79,488 KB
testcase_12 AC 198 ms
79,700 KB
testcase_13 AC 568 ms
168,184 KB
testcase_14 AC 468 ms
139,564 KB
testcase_15 AC 427 ms
122,168 KB
testcase_16 AC 483 ms
135,312 KB
testcase_17 AC 384 ms
117,784 KB
testcase_18 AC 358 ms
111,512 KB
testcase_19 AC 514 ms
145,080 KB
testcase_20 AC 394 ms
121,024 KB
testcase_21 AC 485 ms
153,116 KB
testcase_22 AC 629 ms
187,544 KB
testcase_23 AC 372 ms
84,116 KB
testcase_24 AC 372 ms
83,444 KB
testcase_25 AC 368 ms
83,184 KB
testcase_26 AC 361 ms
83,496 KB
testcase_27 AC 399 ms
85,136 KB
testcase_28 AC 388 ms
84,140 KB
testcase_29 AC 350 ms
83,612 KB
testcase_30 AC 356 ms
84,100 KB
testcase_31 AC 382 ms
83,440 KB
testcase_32 AC 370 ms
83,324 KB
testcase_33 AC 670 ms
161,596 KB
testcase_34 AC 673 ms
161,352 KB
testcase_35 AC 657 ms
160,808 KB
testcase_36 AC 673 ms
160,972 KB
testcase_37 AC 668 ms
161,172 KB
testcase_38 AC 660 ms
160,724 KB
testcase_39 AC 661 ms
161,976 KB
testcase_40 AC 667 ms
160,584 KB
testcase_41 AC 668 ms
161,600 KB
testcase_42 AC 664 ms
161,348 KB
testcase_43 AC 219 ms
100,324 KB
testcase_44 AC 225 ms
100,348 KB
testcase_45 AC 224 ms
100,504 KB
testcase_46 AC 224 ms
100,536 KB
testcase_47 AC 233 ms
100,508 KB
testcase_48 AC 232 ms
100,676 KB
testcase_49 AC 560 ms
126,200 KB
testcase_50 AC 551 ms
127,484 KB
testcase_51 AC 560 ms
127,096 KB
testcase_52 AC 537 ms
125,148 KB
testcase_53 AC 545 ms
124,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class Heap_Dict:
    def __init__(self, Mode=True):
        """

        Mode: True → 最小値, False → 最大値
        """
        self.heap=[]
        self.dict={}
        self.Mode=Mode
        self.__length=0

    def __bool__(self):
        return bool(self.heap)

    def __len__(self):
        return self.__length

    def __contains__(self, x):
        return self.is_exist(x)

    def insert(self, x):
        """ 要素 x を追加する. """

        if self.Mode and not self.is_exist(x):
            heapq.heappush(self.heap,x)
        elif not self.Mode and not self.is_exist(x):
            heapq.heappush(self.heap,-x)

        if x in self.dict:
            self.dict[x]+=1
        else:
            self.dict[x]=1

        self.__length+=1

    def erase(self, x):
        """ x を消す. """

        assert (x in self.dict) and (self.dict[x])

        self.dict[x]-=1
        self.__length-=1
        while self.heap:
            y=self.heap[0]
            if not self.Mode:y=-y
            if self.dict[y]==0:
                heapq.heappop(self.heap)
            else:
                break

    def is_exist(self, x):
        """ キューに x が存在するかどうかを判定する. """

        return (x in self.dict) and (self.dict[x])

    def get_min(self):
        """ キューにある最小値を返す.
        ※ Mode=True でないと使えない.

        """

        assert self.Mode

        if self.heap:
            return self.heap[0]
        else:
            return float("inf")

    def pop_min(self):
        """ キューにある最小値を取り出す.
        ※ Mode=True でないと使えない.

        """

        assert self.Mode

        x=self.get_min()
        self.erase(x)
        return x

    def get_max(self):
        """ キューにある最大値を返す.
        ※ Mode=False でないと使えない.

        """

        assert not self.Mode

        if self.heap:
            return -self.heap[0]
        else:
            return -float("inf")

    def pop_max(self):
        """ キューにある最小値を取り出す.
        ※ Mode = False でないと使えない.

        """

        assert not self.Mode

        x=self.get_max()
        self.erase(x)
        return x

    def count(self, x):
        """ x の個数を求める. """

        if x not in self.dict:
            return 0
        else:
            return self.dict[x]
#==================================================
from collections import defaultdict
import sys
input=sys.stdin.readline
write=sys.stdout.write

N,M=map(int,input().split())
A=list(map(int,input().split()))

H=Heap_Dict(False)
K=defaultdict(int)
L=defaultdict(lambda :Heap_Dict(False))
for x,a in enumerate(A,1):
    K[x]+=a
    H.insert(a)
    L[a].insert(x)

Q=int(input())
Ans=[]
for _ in range(Q):
    T,X,Y=map(int,input().split())

    if T==1:
        H.erase(K[X])
        L[K[X]].erase(X)

        K[X]+=Y
        H.insert(K[X])
        L[K[X]].insert(X)

    elif T==2:
        H.erase(K[X])
        L[K[X]].erase(X)

        K[X]-=Y
        H.insert(K[X])
        L[K[X]].insert(X)
    else:
        alpha=H.get_max()
        Ans.append(L[alpha].get_max())

write("\n".join(map(str,Ans)))
0