結果

問題 No.2111 Sum of Diff
ユーザー lilictakalilictaka
提出日時 2022-11-04 13:31:31
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,190 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 173,756 KB
最終ジャッジ日時 2023-09-25 16:50:38
合計ジャッジ時間 4,683 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
76,076 KB
testcase_01 AC 91 ms
71,348 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v,ope):
        self.update(i, ope(self[i],v))

    def sum(self, l, r): #これで[l,r)から取り出す。
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)
from collections import defaultdict
def compress(A):
    tmp = defaultdict(int)
    B = list(set(A))
    B.sort()
    zipped = [] #i番目のAの要素は何番目に大きいか
    unzipped = [] # i番目に大きいAの要素は何か
    for index,b in enumerate(B):
        tmp[b] = index
        unzipped.append(b)
    for a in A:
        zipped.append(tmp[a])
    return zipped,unzipped
N = int(input())
A = list(map(int,input().split()))
mod = 998244353
zipped,unzipped = compress(A)
n = len(set(A))
ans = 0
cumJ = 0
cumI = 0
ope = lambda x,y:(x+y) % mod
for i in range(N):
    ans -=(cumJ * pow(2,N-1-i))%mod * A[i]
    ans %=mod
    '''
    ans += segJ.sum(0,zipped[i]) * pow(2,N-1-i) * A[i]
    ans %=mod
    '''
    cumJ += pow(2,i,mod)
    cumJ %=mod
rev = pow(2,mod-2,mod)
for i in reversed(range(N)):
    '''
    ans -= segI.sum(zipped[i]+1,n) * pow(2,N-1+i) * A[i]
    ans %=mod
    '''
    ans += (cumI * pow(2,N-1+i)) % mod * A[i]
    ans %=mod
    cumI += pow(rev,i,mod)
    cumI %=mod
print(ans)
0