結果

問題 No.1975 Zigzag Sequence
ユーザー tamatotamato
提出日時 2022-06-10 21:56:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,118 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 81,356 KB
実行使用メモリ 123,900 KB
最終ジャッジ日時 2023-10-21 05:08:15
合計ジャッジ時間 10,342 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,356 KB
testcase_01 AC 37 ms
53,356 KB
testcase_02 AC 37 ms
53,356 KB
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 AC 37 ms
53,356 KB
testcase_14 AC 36 ms
53,356 KB
testcase_15 AC 36 ms
53,356 KB
testcase_16 AC 36 ms
53,356 KB
testcase_17 AC 35 ms
53,356 KB
testcase_18 AC 231 ms
96,952 KB
testcase_19 AC 234 ms
96,952 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 314 ms
123,636 KB
testcase_23 AC 334 ms
123,636 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    input = sys.stdin.readline

    class Bit:
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

        def add(self, i, x):
            while i <= self.size:
                self.tree[i] += x
                i += i & -i

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

    N = int(input())
    A = list(map(int, input().split()))

    A_unique = sorted(list(set(A)))
    I = {a: [] for a in A_unique}
    for i, a in enumerate(A):
        I[a].append(i + 1)

    ans = 0
    # asc
    bit_left = Bit(N)
    bit_right = Bit(N)
    pow2 = [1]
    for i in range(1, N+1):
        bit_left.add(i, pow2[-1])
        bit_right.add(N - i + 1, pow2[-1])
        pow2.append((pow2[-1] * 2) % mod)
    for a in A_unique:
        for i in I[a]:
            bit_left.add(i, -pow2[i - 1])
            bit_right.add(i, -pow2[N - i])
        for i in I[a]:
            val_l = bit_left.sum(i)
            val_r = bit_right.sum(N) - bit_right.sum(i)
            ans = (ans + (val_l * val_r)%mod)%mod

    # desc
    bit_left = Bit(N)
    bit_right = Bit(N)
    pow2 = [1]
    for i in range(1, N + 1):
        bit_left.add(i, pow2[-1])
        bit_right.add(N - i + 1, pow2[-1])
        pow2.append((pow2[-1] * 2) % mod)
    for a in A_unique[::-1]:
        for i in I[a]:
            bit_left.add(i, -pow2[i - 1])
            bit_right.add(i, -pow2[N - i])
        for i in I[a]:
            val_l = bit_left.sum(i)
            val_r = bit_right.sum(N) - bit_right.sum(i)
            ans = (ans + (val_l * val_r) % mod) % mod
    print(ans)


if __name__ == '__main__':
    main()
0