結果

問題 No.694 square1001 and Permutation 3
ユーザー H3PO4H3PO4
提出日時 2020-11-30 09:25:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 944 ms / 3,000 ms
コード長 790 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 86,648 KB
実行使用メモリ 216,224 KB
最終ジャッジ日時 2023-10-11 02:44:38
合計ジャッジ時間 7,856 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,388 KB
testcase_01 AC 70 ms
71,548 KB
testcase_02 AC 73 ms
71,388 KB
testcase_03 AC 81 ms
75,984 KB
testcase_04 AC 85 ms
76,128 KB
testcase_05 AC 84 ms
76,140 KB
testcase_06 AC 84 ms
75,860 KB
testcase_07 AC 478 ms
113,416 KB
testcase_08 AC 549 ms
124,112 KB
testcase_09 AC 944 ms
213,108 KB
testcase_10 AC 472 ms
109,628 KB
testcase_11 AC 908 ms
216,224 KB
testcase_12 AC 925 ms
213,132 KB
testcase_13 AC 73 ms
71,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


N = int(input())
A = [int(input()) for _ in range(N)]
d = {x: i for i, x in enumerate(sorted(list(set(A))), 1)}
Ac = [d[a] for a in A]

M = max(Ac)
ct = [0] * (M + 1)
for c in Ac:
    ct[c] += 1
for i in range(1, M + 1):
    ct[i] += ct[i - 1]

bit = Bit(M)
ans = [0] * N

for i, p in enumerate(Ac):
    bit.add(p, 1)
    ans[0] += i + 1 - bit.sum(p)
for i, p in enumerate(Ac[:-1]):
    ans[i + 1] = ans[i] - ct[p - 1] + (N - ct[p])
print(*ans, sep='\n')
0