結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー 👑 KazunKazun
提出日時 2021-02-10 01:59:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,052 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 86,524 KB
実行使用メモリ 79,508 KB
最終ジャッジ日時 2023-09-21 16:53:02
合計ジャッジ時間 2,764 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 73 ms
71,328 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 78 ms
70,996 KB
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Binary_Indexed_Tree():
    def __init__(self,L,calc,unit,inv,index=1):
        """calcを演算とするN項のBinary Indexed Treeを作成

        calc:演算(2変数関数,群)
        unit:群calcの単位元(xe=ex=xを満たすe)
        inv:群calcの逆元(1変数関数)
        """
        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(self,k,index=1):
        """第k要素の値を出力する.

        k:数列の要素
        index:先頭の要素の番号
        """

        p=k+(1-index)
        return self.sum(p,p,index)

    def add(self,k,x,index=1,right=False):
        """第k要素にxを左から加え,更新を行う.

        k:数列の要素
        x:更新後の値
        index:先頭の要素の番号
        right:「左から」が「右から」になる
        """
        p=k+(1-index)
        while p<=self.num:
            if right==False:
                #左から
                self.data[p]=self.calc(x,self.data[p])
            else:
                #右から
                self.data[p]=self.calc(self.data[p],x)
            p+=p&(-p)

    def update(self,k,x,index=1,right=False):
        """第k要素をxに変え,更新を行う.

        k:数列の要素
        x:更新後の値
        """

        a=self.index(k,index)
        if right==False:
            #左から
            y=self.calc(x,self.inv(a))
        else:
            #右から
            y=self.calc(self.inv(a),x)

        self.add(k,y,index,right)

    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==1:
            return self.__section(beta)
        else:
            return self.calc(self.inv(self.__section(alpha-1)),self.__section(beta))

    def __section(self,To):
        S=self.unit
        x=To
        while x>0:
            S=self.calc(self.data[x],S)
            x-=x&(-x)
        return S

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

    def __getitem__(self,index):
        if isinstance(index,int):
            return self.sum(index,index,self.index)
        else:
            return [self.sum(t,t,self.index) for t in index]
#================================================
import sys
from operator import add,neg
input=sys.stdin.readline
N=int(input())
B=Binary_Indexed_Tree([0]*N,add,0,neg,1)
X=0
for i in range(N):
    m=int(input())
    B.add(m,1,1)
    X+=B.sum(1,m-1,1)
print(X)
0