結果
問題 | No.1705 Mode of long array |
ユーザー |
👑 ![]() |
提出日時 | 2021-10-08 22:27:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 651 ms / 3,000 ms |
コード長 | 3,259 bytes |
コンパイル時間 | 195 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 183,532 KB |
最終ジャッジ日時 | 2024-07-23 05:06:57 |
合計ジャッジ時間 | 21,410 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 51 |
ソースコード
import heapqclass Heap_Dict:def __init__(self, Mode=True):"""Mode: True → 最小値, False → 最大値"""self.heap=[]self.dict={}self.Mode=Modeself.__length=0def __bool__(self):return bool(self.heap)def __len__(self):return self.__lengthdef __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]+=1else:self.dict[x]=1self.__length+=1def erase(self, x):""" x を消す. """assert (x in self.dict) and (self.dict[x])self.dict[x]-=1self.__length-=1while self.heap:y=self.heap[0]if not self.Mode:y=-yif self.dict[y]==0:heapq.heappop(self.heap)else:breakdef is_exist(self, x):""" キューに x が存在するかどうかを判定する. """return (x in self.dict) and (self.dict[x])def get_min(self):""" キューにある最小値を返す.※ Mode=True でないと使えない."""assert self.Modeif self.heap:return self.heap[0]else:return float("inf")def pop_min(self):""" キューにある最小値を取り出す.※ Mode=True でないと使えない."""assert self.Modex=self.get_min()self.erase(x)return xdef get_max(self):""" キューにある最大値を返す.※ Mode=False でないと使えない."""assert not self.Modeif self.heap:return -self.heap[0]else:return -float("inf")def pop_max(self):""" キューにある最小値を取り出す.※ Mode = False でないと使えない."""assert not self.Modex=self.get_max()self.erase(x)return xdef count(self, x):""" x の個数を求める. """if x not in self.dict:return 0else:return self.dict[x]#==================================================from collections import defaultdictimport sysinput=sys.stdin.readlinewrite=sys.stdout.writeN,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]+=aH.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]+=YH.insert(K[X])L[K[X]].insert(X)elif T==2:H.erase(K[X])L[K[X]].erase(X)K[X]-=YH.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)))