結果

問題 No.1705 Mode of long array
ユーザー harurunharurun
提出日時 2021-09-19 16:19:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,191 ms / 3,000 ms
コード長 4,658 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 213,716 KB
最終ジャッジ日時 2024-07-23 03:41:35
合計ジャッジ時間 45,280 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,504 KB
testcase_01 AC 42 ms
54,016 KB
testcase_02 AC 42 ms
53,504 KB
testcase_03 AC 164 ms
79,212 KB
testcase_04 AC 134 ms
78,720 KB
testcase_05 AC 171 ms
79,344 KB
testcase_06 AC 350 ms
83,336 KB
testcase_07 AC 413 ms
84,328 KB
testcase_08 AC 379 ms
84,860 KB
testcase_09 AC 347 ms
83,012 KB
testcase_10 AC 348 ms
81,984 KB
testcase_11 AC 349 ms
84,304 KB
testcase_12 AC 358 ms
82,740 KB
testcase_13 AC 1,047 ms
194,852 KB
testcase_14 AC 864 ms
157,820 KB
testcase_15 AC 764 ms
136,860 KB
testcase_16 AC 877 ms
151,496 KB
testcase_17 AC 678 ms
128,152 KB
testcase_18 AC 621 ms
121,712 KB
testcase_19 AC 925 ms
164,400 KB
testcase_20 AC 696 ms
134,356 KB
testcase_21 AC 801 ms
171,676 KB
testcase_22 AC 1,191 ms
213,716 KB
testcase_23 AC 981 ms
119,908 KB
testcase_24 AC 960 ms
121,764 KB
testcase_25 AC 960 ms
119,200 KB
testcase_26 AC 941 ms
120,480 KB
testcase_27 AC 1,007 ms
121,888 KB
testcase_28 AC 1,016 ms
120,232 KB
testcase_29 AC 923 ms
119,968 KB
testcase_30 AC 956 ms
119,836 KB
testcase_31 AC 992 ms
121,568 KB
testcase_32 AC 951 ms
119,716 KB
testcase_33 AC 1,182 ms
211,856 KB
testcase_34 AC 1,146 ms
211,120 KB
testcase_35 AC 1,142 ms
212,396 KB
testcase_36 AC 1,126 ms
211,484 KB
testcase_37 AC 1,169 ms
211,368 KB
testcase_38 AC 1,176 ms
211,496 KB
testcase_39 AC 1,184 ms
210,992 KB
testcase_40 AC 1,153 ms
211,876 KB
testcase_41 AC 1,168 ms
211,496 KB
testcase_42 AC 1,178 ms
212,528 KB
testcase_43 AC 513 ms
136,380 KB
testcase_44 AC 540 ms
136,440 KB
testcase_45 AC 539 ms
136,112 KB
testcase_46 AC 548 ms
136,120 KB
testcase_47 AC 562 ms
136,624 KB
testcase_48 AC 562 ms
136,568 KB
testcase_49 AC 1,133 ms
168,100 KB
testcase_50 AC 1,123 ms
168,164 KB
testcase_51 AC 1,099 ms
166,176 KB
testcase_52 AC 1,141 ms
168,100 KB
testcase_53 AC 1,187 ms
167,332 KB
権限があれば一括ダウンロードができます

ソースコード

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
    #print("get_max: ",get_maxheap(self.heap))
    return get_maxheap(self.heap)
 

class INPUT:
  def __init__(self):
    self._l=open(0).read().split()
    self._length=len(self._l)
    self._index=0
    return

  def stream(self,k=1,f=int,f2=False):
    assert(-1<k)
    if self._length==self._index or self._length-self._index<k:
      raise Exception("There is no input!")
    elif f!=str:
      if k==0:
        ret=list(map(f,self._l[self._index:]))
        self._index=self._length
        return ret
      if k==1 and not f2:
        ret=f(self._l[self._index])
        self._index+=1
        return ret
      if k==1 and f2:
        ret=[f(self._l[self._index])]
        self._index+=1
        return ret
      ret=[]
      for _ in [0]*k:
        ret.append(f(self._l[self._index]))
        self._index+=1
      return ret
    else:
      if k==0:
        ret=list(self._l[self._index:])
        self._index=self._length
        return ret
      if k==1 and not f2:
        ret=self._l[self._index]
        self._index+=1
        return ret
      if k==1 and f2:
        ret=[self._l[self._index]]
        self._index+=1
        return ret
      ret=[]
      for _ in [0]*k:
        ret.append(self._l[self._index])
        self._index+=1
      return ret
pin=INPUT().stream

#pin(number[default:1],f[default:int],f2[default:False])
#if number==0 -> return left all
#listを変数で受け取るとき、必ずlistをTrueにすること。


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()
    #print("get: ",self.S[max_place].di)
    return self.S[max_place].get_max()+1
  
  def debug_count(self):
    for i in self.count:
      print(i,end=" ")
    print()
    return
  
  def debug_max_S(self):
    print(self.max_S.di)
    return
  
  def debug_S(self):
    for i in self.S:
      print(i.di,end=" ")
    print()
    return


def main():
  N,M=pin(2)
  a=pin(M,int,1)
  b=a[:]
  Q=pin(1)
  query=[]
  for i in range(Q):
    t,x,y=pin(3)
    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
  #print(0,end=" ")
  for i in range(1,len(b)):
    if b[i-1]!=b[i]:
      #print(b[i],end=" ")
      place[b[i]]=cnt
      cnt+=1
  #print()
  mode=Mode(M,place,a)
  for t,x,y in query:
    #mode.debug_count()
    #mode.debug_max_S()
    #mode.debug_S()
    if t==1:
      mode.add(x-1,y)
    elif t==2:
      mode.erase(x-1,y)
    else:
      print(mode.get())
  return

main()
0