結果
| 問題 |
No.1705 Mode of long array
|
| コンテスト | |
| ユーザー |
👑 Kazun
|
| 提出日時 | 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 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)))
Kazun