結果

問題 No.1733 Sum of Sorted Subarrays
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-11-06 22:39:25
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,032 bytes
コンパイル時間 646 ms
コンパイル使用メモリ 82,944 KB
実行使用メモリ 132,292 KB
最終ジャッジ日時 2024-04-25 11:06:45
合計ジャッジ時間 31,930 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,376 KB
testcase_01 AC 43 ms
53,248 KB
testcase_02 AC 43 ms
53,248 KB
testcase_03 AC 54 ms
60,160 KB
testcase_04 AC 43 ms
53,248 KB
testcase_05 AC 55 ms
61,824 KB
testcase_06 AC 53 ms
60,288 KB
testcase_07 AC 47 ms
53,888 KB
testcase_08 AC 2,184 ms
100,772 KB
testcase_09 TLE -
testcase_10 AC 2,050 ms
101,768 KB
testcase_11 AC 2,483 ms
103,748 KB
testcase_12 AC 2,590 ms
110,700 KB
testcase_13 AC 2,503 ms
110,320 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 2,097 ms
100,212 KB
testcase_17 TLE -
testcase_18 AC 2,951 ms
113,704 KB
testcase_19 TLE -
testcase_20 AC 2,834 ms
111,824 KB
testcase_21 TLE -
testcase_22 AC 2,360 ms
103,332 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 2,075 ms
132,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
class LazySegTree:

    def __init__(self, n, op, e, mapping, composition, id):
        self.n = n
        self.op = op
        self.e = e
        self.mapping = mapping
        self.composition = composition
        self.id = id
        self.log = (n-1).bit_length()
        self.size = 1 << self.log
        self.data = [e]*(2*self.size)
        self.lazy = [id]*(self.size)

    def update(self, k):
        self.data[k] = self.op(self.data[2*k],self.data[2*k+1])

    def build(self, lis):
        for i, l in enumerate(lis,self.size):
            self.data[i] = l
        for i in range(self.size-1, 0, -1):
            self.update(i)

    def point_set(self, p, x):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        self.data[p] = x
        for i in range(1,self.log+1):
            self.update(p>>i)

    def point_get(self, p):
        p += self.size
        for i in range(self.log, 0,-1):
            self.push(p >> i)
        return self.data[p]

    def apply(self, p, f):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        self.data[p] = self.mapping(f, self.data[p])
        for i in range(1, self.log+1):
            self.update(p >> i)

    def range_apply(self, l, r, f):
        ''' change the value you define in "mapping" and "composition" of [l,r) '''
        if l == r:
            return 
        l += self.size
        r += self.size
        for i in range(self.log, 0, -1):
            if ((l >> i) << i) != l:
                self.push(l >> i)
            if ((r >> i) << i) != r:
                self.push((r-1) >> i)
        l2 = l
        r2 = r
        while l2 < r2:
            if l2 & 1:
                self.all_apply(l2, f)
                l2 += 1
            if r2 & 1:
                r2 -= 1
                self.all_apply(r2, f)
            l2 >>= 1
            r2 >>= 1
        for i in range(1, self.log+1):
            if ((l >> i) << i) != l:
                self.update(l >> i)
            if ((r >> i) << i) != r:
                self.update((r-1) >> i)

    def all_apply(self, k, f):
        self.data[k] = self.mapping(f, self.data[k])
        if k < self.size:
            self.lazy[k] = self.composition(f, self.lazy[k])

    def push(self, k):
        self.all_apply(2*k, self.lazy[k])
        self.all_apply(2*k+1, self.lazy[k])
        self.lazy[k] = self.id

    def prod(self, l, r):
        ''' get the value you define in "op" of [l,r) '''
        if l == r:
            return self.e
        l += self.size
        r += self.size
        for i in range(self.log, 0, -1):
            if ((l>>i) <<i) != l:
                self.push(l>>i)
            if ((r>>i) <<i) != r:
                self.push(r>>i)
        sml = smr = self.e
        while l < r:
            if l & 1:
                sml = self.op(sml, self.data[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self.op(self.data[r], smr)
            l >>= 1
            r >>= 1
        return self.op(sml, smr)

    def all_prod(self):
        return self.data[1]



n = int(input())
A = list(map(int,input().split()))
sA = sorted([[a,i] for i,a in enumerate(A)])
mod = 998244353

def op(x, y):
    res = x+y
    if res > mod:
        res %= mod
    return res

def mapping(p, x):
    res = p*x
    if res > mod:
        res %= mod
    return res

def composition(p, q):
    res = p*q
    if res > mod:
        res %= mod
    return res

e = 0
id = 1
lseg = LazySegTree(n+1, op, e, mapping, composition, id)
lseg.build([1]*(n+1))
rseg = LazySegTree(n+1, op, e, mapping, composition, id)
rseg.build([1]*(n+1))
ans = 0

for a,i in sA:
    lsum = lseg.prod(0,i+1)
    rsum = rseg.prod(i+1,n+1)
    ln = lseg.point_get(i+1)
    rn = rseg.point_get(i)
    l = lsum*pow(ln,mod-2,mod)%mod
    r = rsum*pow(rn,mod-2,mod)%mod
    ans += l*r%mod*a%mod
    ans %= mod
    lseg.range_apply(0,i+1,2)
    rseg.range_apply(i+1,n+1,2)
print(ans)


0