結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー mkawa2mkawa2
提出日時 2023-04-12 22:42:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,914 ms / 5,000 ms
コード長 1,713 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 323,492 KB
最終ジャッジ日時 2024-04-17 08:18:11
合計ジャッジ時間 16,202 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
92,032 KB
testcase_01 AC 274 ms
106,880 KB
testcase_02 AC 93 ms
77,796 KB
testcase_03 AC 1,667 ms
274,284 KB
testcase_04 AC 4,914 ms
323,492 KB
testcase_05 AC 1,619 ms
274,152 KB
testcase_06 AC 1,515 ms
277,168 KB
testcase_07 AC 1,585 ms
284,868 KB
testcase_08 AC 1,655 ms
274,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# md = 10**9+7
md = 998244353

class BitSum:
    def __init__(self, n):
        self._n = n+1
        self._table = [0]*self._n
        self._origin = [0]*n

    def __getitem__(self, item):
        return self._origin[item]

    def add(self, i, x):
        self._origin[i] += x
        i += 1
        while i < self._n:
            self._table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self._table[i]
            i -= i & -i
        return res

n = II()
aa = LI()

dec = sorted(set(aa))
enc = {a: i for i, a in enumerate(dec)}
aa = [enc[a] for a in aa]

ans = 0
for _ in range(2):
    left, right, pair = BitSum(n), BitSum(n), BitSum(n)
    for a in aa: right.add(a, 1)

    for i in range(1, n-1):
        a = aa[i-1]
        l = left[a]
        r = right[a]
        pair.add(a, r-l-1)
        left.add(a, 1)
        right.add(a, -1)
        a = aa[i]
        ans += left.sum(a-1)*right.sum(a-1)-pair.sum(a-1)

    aa = [n-1-a for a in aa]

print(ans)
0