結果

問題 No.2065 Sum of Min
ユーザー 👑 KazunKazun
提出日時 2022-09-02 22:16:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 936 ms / 2,000 ms
コード長 4,198 bytes
コンパイル時間 499 ms
コンパイル使用メモリ 87,012 KB
実行使用メモリ 120,636 KB
最終ジャッジ日時 2023-08-10 04:31:12
合計ジャッジ時間 17,375 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,344 KB
testcase_01 AC 71 ms
71,752 KB
testcase_02 AC 72 ms
71,352 KB
testcase_03 AC 72 ms
71,484 KB
testcase_04 AC 656 ms
119,304 KB
testcase_05 AC 646 ms
118,088 KB
testcase_06 AC 575 ms
117,236 KB
testcase_07 AC 295 ms
106,204 KB
testcase_08 AC 496 ms
111,496 KB
testcase_09 AC 867 ms
119,308 KB
testcase_10 AC 852 ms
118,592 KB
testcase_11 AC 782 ms
116,732 KB
testcase_12 AC 877 ms
118,464 KB
testcase_13 AC 936 ms
120,636 KB
testcase_14 AC 883 ms
119,536 KB
testcase_15 AC 850 ms
118,940 KB
testcase_16 AC 873 ms
118,928 KB
testcase_17 AC 864 ms
119,552 KB
testcase_18 AC 863 ms
118,760 KB
testcase_19 AC 864 ms
118,748 KB
testcase_20 AC 850 ms
119,012 KB
testcase_21 AC 882 ms
120,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Binary_Indexed_Tree():
    def __init__(self, L, calc, unit, inv, index=1):
        """ calc を演算とする N 項の Binary Indexed Tree を作成
        calc: 演算 (2変数関数, 可換群)
        unit: 群 calc の単位元 (x+e=e+x=xを満たすe)
        inv : 群 calc の逆元 (1変数関数, x+inv(x)=inv(x)+x=e をみたす inv(x))
        """
        self.calc=calc
        self.unit=unit
        self.inv=inv
        self.index=index

        N=len(L)
        d=max(1,(N-1).bit_length())
        k=2**d

        X=[None]+[unit]*k

        self.num=k
        self.depth=d

        if L:
            for i in range(len(L)):
                p=i+1
                while p<=k:
                    X[p]=self.calc(X[p],L[i])
                    p+=p&(-p)
        self.data=X

    def index_number(self, k, index=1):
        """ 第 k 要素の値を出力する.
        k    : 数列の要素
        index: 先頭の要素の番号
        """
        return self.sum(k,k,index)

    def add(self, k, x, index=1):
        """ 第 k 要素に x を加え, 更新を行う.
        k    : 数列の要素
        x    : 加える値
        index: 先頭の要素の番号
        """
        data=self.data; calc=self.calc
        p=k+(1-index)
        while p<=self.num:
            data[p]=calc(self.data[p],x)
            p+=p&(-p)

    def update(self, k, x, index=1):
        """ 第 k 要素を x に変え, 更新を行う.
        k: 数列の要素
        x: 更新後の値
        """

        a=self.index_number(k,index)
        y=self.calc(self.inv(a),x)

        self.add(k,y,index)

    def sum(self, From, To, index=1):
        """ 第 From 要素から第 To 要素までの総和を求める.
        ※From!=1を使うならば, 群でなくてはならない.
        From : 始まり
        To   : 終わり
        index: 先頭の要素の番号
        """
        alpha=max(1,From+(1-index))
        beta=min(self.num,To+(1-index))

        if alpha>beta:
            return self.unit
        elif alpha==1:
            return self.__section(beta)
        else:
            return self.calc(self.inv(self.__section(alpha-1)),self.__section(beta))

    def __section(self,x):
        """ B[1]+...+B[x] を求める. """
        data=self.data; calc=self.calc
        S=self.unit
        while x>0:
            S=calc(data[x],S)
            x-=x&(-x)
        return S

    def all_sum(self):
        return self.data[-1]

    def binary_search(self, cond, index=1):
        """ cond(B[1]+...+B[k]) を満たす最小の k を返す.

        cond: 単調増加

        ※ cond(unit)=True の場合の返り値は index-1
        ※ cond(B[1]+...+B[k]) なる k が存在しない場合の返り値は self.num+index
        """

        if cond(self.unit):
            return index-1

        j=0
        r=self.num
        t=r
        data=self.data; calc=self.calc
        alpha=self.unit

        for _ in range(self.depth+1):
            if j+t<=self.num:
                beta=calc(alpha,data[j+t])
                if not cond(beta):
                    alpha=beta
                    j+=t
            t>>=1

        return j+index

    def __getitem__(self,index):
        if isinstance(index,int):
            return self.index_number(index,self.index)
        else:
            return [self.index_number(t,self.index) for t in index]

    def __setitem__(self,index,val):
        self.update(index,val,self.index)
#==================================================
from operator import add, itemgetter,neg
import sys

input=sys.stdin.readline
write=sys.stdout.write

N,Q=map(int,input().split())
A=[0]+list(map(int,input().split()))
AA=[(i,A[i]) for i in range(1,N+1)]
AA.sort(key=itemgetter(1))

Query=[]
for q in range(Q):
    L,R,X=map(int,input().split())
    Query.append((X,L,R,q))
Query.sort(key=itemgetter(0),reverse=True)

B0=Binary_Indexed_Tree(A.copy(),add,0,neg,0)
B1=Binary_Indexed_Tree([0]*(N+1), add, 0, neg, 0)

Ans=[0]*Q
for x,l,r,q in Query:
    while bool(AA) and AA[-1][1]>x:
        i,a=AA.pop()
        B0.update(i,0,0)
        B1.update(i,1,0)
    Ans[q]=B0.sum(l,r,0)+x*B1.sum(l,r,0)

write("\n".join(map(str,Ans)))
0