結果

問題 No.694 square1001 and Permutation 3
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-10 20:56:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,500 ms / 3,000 ms
コード長 1,624 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 81,564 KB
実行使用メモリ 190,336 KB
最終ジャッジ日時 2023-10-20 00:52:50
合計ジャッジ時間 10,739 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,324 KB
testcase_01 AC 41 ms
53,324 KB
testcase_02 AC 37 ms
53,324 KB
testcase_03 AC 47 ms
61,236 KB
testcase_04 AC 54 ms
61,244 KB
testcase_05 AC 50 ms
61,248 KB
testcase_06 AC 49 ms
61,248 KB
testcase_07 AC 359 ms
113,684 KB
testcase_08 AC 746 ms
116,820 KB
testcase_09 AC 1,500 ms
190,204 KB
testcase_10 AC 342 ms
114,760 KB
testcase_11 AC 1,473 ms
190,292 KB
testcase_12 AC 1,492 ms
190,336 KB
testcase_13 AC 42 ms
53,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class fenwick_tree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i


n = int(input())
A = [int(input()) for _ in range(n)]

atoi = {a: [] for a in sorted(set(A))}
for i, a in enumerate(A):
    atoi[a].append(i)

small = [0] * n  # 自分より小さいやつの数
big = [0] * n  # 自分より大きいやつの数
cnt = 0
for a in atoi.keys():
    for i in atoi[a]:
        small[i] = cnt
        big[i] = n - cnt - len(atoi[a])
    cnt += len(atoi[a])


# calc inv
ans = 0
bit = fenwick_tree(n)
for v in reversed(atoi.values()):
    for i in v:
        ans += bit.sum(0, i)
    for i in v:
        bit.add(i, 1)

print(ans)
for i in range(n - 1):
    ans = ans - small[i] + big[i]
    print(ans)
0