๐Ÿšง This website is under construction. ๐Ÿšง
AtCoder
ABC
221
E

Math
ๆ•ฐๅญฆ
ABC
E

ABC221 E - LEQ

https://atcoder.jp/contests/abc221/tasks/abc221_e (opens in a new tab)
ๆฐด่‰ฒไธŠไฝใ€‚ๆ•ฐๅญฆใ€‚

้กŒๆ„ใฏ AA ใฎ A1โ€ฒโ‰คAkโ€ฒA_1'\le A_k' ใ‚’ๆบ€ใŸใ™้ƒจๅˆ†ๅˆ— A1โ€ฒ,โ€ฆ,Akโ€ฒA_1',\ldots,A_k' ใฎ็ทๆ•ฐใ‚’ๆฑ‚ใ‚ใ‚‹ใ€‚

้ƒจๅˆ†ๅˆ—ใฎไธก็ซฏใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น i,ji,j ใŒๆฑบใพใ‚Œใฐใ€้–“ใฎ jโˆ’iโˆ’1j-i-1 ๅ€‹ใฎ่ฆ็ด ใซ้–ขใ—ใฆใฏๅ–ใ‚‹๏ผๅ–ใ‚‰ใชใ„ใฎ 2 ้€šใ‚Šใฎ้ธๆŠžใซใ‚ˆใ‚Š้ƒจๅˆ†ๅˆ—ใŒๆง‹ๆˆใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใ“ใฎๅ ดๅˆใฎ้ƒจๅˆ†ๅˆ—ใฎ็ทๆ•ฐใฏ 2jโˆ’iโˆ’12^{j-i-1} ๅ€‹ใจใชใ‚‹ใ€‚

ใคใพใ‚Š Aiโ‰คAjA_i\le A_j ใจใชใ‚‹ i,j(i<j)i,j(i<j) ใซๅฏพใ—ใฆใ€ 2jโˆ’iโˆ’12^{j-i-1} ใฎ็ทๅ’Œใ‚’ๆฑ‚ใ‚ใ‚Œใฐใ‚ˆใ„ใ“ใจใจใชใ‚‹ใ€‚

โˆ‘i=0nโˆ‘jโˆˆ{i<j,Aiโ‰คAj}n2jโˆ’iโˆ’1\sum_{i=0}^{n}\sum_{j\in\{i<j,A_i\le A_j\}}^{n}{2^{j-i-1}}
=โˆ‘i=0nโˆ‘jโˆˆ{i<j,Aiโ‰คAj}n2jโˆ’12i=\sum_{i=0}^{n}\sum_{j\in\{i<j,A_i\le A_j\}}^{n}{\frac{2^{j-1}}{2^i}} =โˆ‘i=0n12iโˆ‘jโˆˆ{i<j,Aiโ‰คAj}n2jโˆ’1=\sum_{i=0}^{n}\frac{1}{2^i}\sum_{j\in\{i<j,A_i\le A_j\}}^{n}{2^{j-1}}

ไปŠใ€ โˆ‘jโˆˆ{i<j,Aiโ‰คAj}n2jโˆ’1\sum_{j\in\{i<j,A_i\le A_j\}}^{n}{2^{j-1}} ใฏ Binary Indexed Tree ใซใ‚ˆใ‚Š้ซ˜้€Ÿใซ่จˆ็ฎ—ใŒใงใใ‚‹ใฎใงใ€ ii ใซใคใ„ใฆๅ…จๆŽข็ดขใ™ใ‚Œใฐ่งฃใŒๅพ—ใ‚‰ใ‚Œใ‚‹ใ€‚

from atcoder.fenwicktree import FenwickTree
 
MOD = 998244353
 
n = int(input())
a = list(map(int, input().split()))
 
compressed = {e: i for i, e in enumerate(sorted(set(a)))}
bit = FenwickTree(n)
 
remain = []
ans = 0
for i in reversed(range(n)):
    e = compressed[a[i]]
    ans += bit.sum(e, n) * pow(pow(2, MOD - 2, MOD), i, MOD)
    ans %= MOD
    if i > 0:
        bit.add(e, pow(2, i - 1, MOD))
    else:
        bit.add(e, pow(2, MOD - 2, MOD))
 
print(ans)
 

้€†ๅ…ƒใฎ่จˆ็ฎ—ใซๆ‰‹้–“ๅ–ใฃใŸใ€‚