結果

問題 No.2065 Sum of Min
ユーザー とりゐとりゐ
提出日時 2022-04-30 21:22:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 728 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 157,976 KB
最終ジャッジ日時 2024-04-24 11:31:39
合計ジャッジ時間 13,379 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
54,144 KB
testcase_01 AC 41 ms
54,144 KB
testcase_02 AC 41 ms
54,016 KB
testcase_03 AC 43 ms
54,528 KB
testcase_04 AC 496 ms
122,496 KB
testcase_05 AC 465 ms
120,692 KB
testcase_06 AC 344 ms
110,376 KB
testcase_07 AC 252 ms
107,196 KB
testcase_08 AC 350 ms
110,424 KB
testcase_09 AC 615 ms
157,976 KB
testcase_10 AC 638 ms
155,316 KB
testcase_11 AC 629 ms
155,568 KB
testcase_12 AC 691 ms
157,848 KB
testcase_13 AC 699 ms
157,208 KB
testcase_14 AC 694 ms
157,472 KB
testcase_15 AC 704 ms
157,976 KB
testcase_16 AC 712 ms
157,600 KB
testcase_17 AC 728 ms
157,600 KB
testcase_18 AC 727 ms
157,856 KB
testcase_19 AC 710 ms
157,344 KB
testcase_20 AC 693 ms
157,844 KB
testcase_21 AC 704 ms
157,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
input=lambda :stdin.readline()[:-1]

class segtree():
  def __init__(self,init,func,ide):
    self.n=len(init)
    self.func=func
    self.ide=ide
    self.size=1<<(self.n-1).bit_length()
    self.tree=[self.ide for i in range(2*self.size)]
    for i in range(self.n):
      self.tree[self.size+i]=init[i]
    for i in range(self.size-1,0,-1):
      self.tree[i]=self.func(self.tree[2*i], self.tree[2*i|1])
  
  def update(self,k,x):
    k+=self.size
    self.tree[k]=x
    k>>=1
    while k:
      self.tree[k]=self.func(self.tree[2*k],self.tree[k*2|1])
      k>>=1
  
  def get(self,i):
    return self.tree[i+self.size]
  
  def query(self,l,r):
    l+=self.size
    r+=self.size
    l_res=self.ide
    r_res=self.ide
    while l<r:
      if l&1:
        l_res=self.func(l_res,self.tree[l])
        l+=1
      if r&1:
        r-=1
        r_res=self.func(self.tree[r],r_res)
      l>>=1
      r>>=1
    return self.func(l_res,r_res)
  
  def debug(self,s=10):
    print([self.get(i) for i in range(min(self.n,s))])

from collections import defaultdict

n,q=map(int,input().split())
a=list(map(int,input().split()))
d=defaultdict(list)
s=set(a)
for i in range(n):
  d[a[i]].append((i,-1,-1))

for i in range(q):
  l,r,x=map(int,input().split())
  d[x].append((l-1,r,i))
  s.add(x)

def op(x,y):return x+y
seg1=segtree([0]*n,op,0)
seg2=segtree([1]*n,op,0)

ans=[0]*q
for i in sorted(list(s)):
  for x,y,id in d[i]:
    if id==-1:
      seg1.update(x,i)
      seg2.update(x,0)
    else:
      ans[id]=seg1.query(x,y)+seg2.query(x,y)*i

print(*ans,sep='\n')
0