結果

問題 No.1705 Mode of long array
ユーザー harurun
提出日時 2021-09-19 18:27:06
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,795 ms / 3,000 ms
コード長 2,876 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 132,100 KB
最終ジャッジ日時 2024-07-23 03:43:53
合計ジャッジ時間 53,305 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

# multiset: https://tsubo.hatenablog.jp/entry/2020/06/15/124657
from heapq import*

def maxheappush(heap,x):
  heappush(heap,-x)
  return
    
def maxheappop(heap):
  return -1*(heappop(heap))

def get_maxheap(heap):
  return -1*heap[0]

class pseudo_set:
  def __init__(self):
    self.heap=[]
    self.di=set()
    return

  def insert(self,x):
    maxheappush(self.heap,x)
    if x not in self.di:
      self.di.add(x)
    return
    
  def erase(self,x):
    self.di.discard(x)
    while self.heap:
      p=maxheappop(self.heap)
      if p in self.di:
        maxheappush(self.heap,p)
        break
    return

  def get_max(self):
    while self.heap:
      p=maxheappop(self.heap)
      if p in self.di:
        maxheappush(self.heap,p)
        break
    return get_maxheap(self.heap)
  

class pseudo_Multiset:
  def __init__(self):
    self.heap=[]
    self.di=dict()
    return

  def insert(self,x):
    maxheappush(self.heap,x)
    if x not in self.di:
      self.di[x]=1
    else:
      self.di[x]+=1
    return
    
  def erase(self,x):
    if x in self.di:
      self.di[x]-=1
      if self.di[x]==0:
        self.di.pop(x)
    while self.heap:
      p=maxheappop(self.heap)
      if p in self.di:
        maxheappush(self.heap,p)
        break
    return

  def get_max(self):
    while self.heap:
      p=maxheappop(self.heap)
      if p in self.di:
        maxheappush(self.heap,p)
        break
    return get_maxheap(self.heap)
 
class Mode:
  def __init__(self,M,um,A):
    self.place=um
    self.count=[0]*M
    self.S=[pseudo_set() for _ in [0]*len(um)]
    self.max_S=pseudo_Multiset()
    for i in range(M):
      self.max_S.insert(0)
      self.add(i,A[i])
    return 

  def add(self,x,y):
    now=self.count[x]
    self.S[self.place[now]].erase(x)
    self.max_S.erase(self.place[now])
    self.S[self.place[now+y]].insert(x)
    self.max_S.insert(self.place[now+y])
    self.count[x]+=y
    return

  def erase(self,x,y):
    now=self.count[x]
    self.S[self.place[now]].erase(x)
    self.max_S.erase(self.place[now])
    self.S[self.place[now-y]].insert(x)
    self.max_S.insert(self.place[now-y])
    self.count[x]-=y
    return

  def get(self):
    max_place=self.max_S.get_max()
    return self.S[max_place].get_max()+1

def main():
  N,M=map(int,input().split())
  a=list(map(int,input().split()))
  b=a[:]
  Q=int(input())
  query=[]
  for i in range(Q):
    t,x,y=map(int,input().split())
    query.append((t,x,y))
    if t==1:
      b.append(b[x-1])
      b[x-1]+=y
    elif t==2:
      b.append(b[x-1])
      b[x-1]-=y
  b.append(0)
  b.sort()
  place=dict()
  place[b[0]]=0
  cnt=1
  for i in range(1,len(b)):
    if b[i-1]!=b[i]:
      place[b[i]]=cnt
      cnt+=1
  mode=Mode(M,place,a)
  for t,x,y in query:
    if t==1:
      mode.add(x-1,y)
    elif t==2:
      mode.erase(x-1,y)
    else:
      print(mode.get())
  return

main()
0