結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー 👑 KazunKazun
提出日時 2021-02-10 02:00:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 137 ms / 2,500 ms
コード長 3,052 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 79,984 KB
最終ジャッジ日時 2023-09-21 16:54:02
合計ジャッジ時間 3,204 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,268 KB
testcase_01 AC 77 ms
71,296 KB
testcase_02 AC 76 ms
71,572 KB
testcase_03 AC 82 ms
71,620 KB
testcase_04 AC 78 ms
71,300 KB
testcase_05 AC 85 ms
76,060 KB
testcase_06 AC 91 ms
76,108 KB
testcase_07 AC 99 ms
76,884 KB
testcase_08 AC 101 ms
77,620 KB
testcase_09 AC 78 ms
71,116 KB
testcase_10 AC 79 ms
71,600 KB
testcase_11 AC 137 ms
79,984 KB
testcase_12 AC 129 ms
79,732 KB
testcase_13 AC 78 ms
71,120 KB
testcase_14 AC 79 ms
71,592 KB
testcase_15 AC 79 ms
71,492 KB
権限があれば一括ダウンロードができます

ソースコード

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(m+1,N,1)
print(X)
0