結果

問題 No.694 square1001 and Permutation 3
コンテスト
ユーザー mkawa2
提出日時 2026-06-24 00:08:30
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 756 ms / 3,000 ms
コード長 690 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 217 ms
コンパイル使用メモリ 84,864 KB
実行使用メモリ 229,696 KB
最終ジャッジ日時 2026-06-24 00:08:41
合計ジャッジ時間 7,326 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input=sys.stdin.readline

def coordinate_compression(arr,rev=0):
    dec = []
    for a in sorted(arr,reverse=rev):
        if not dec or dec[-1] != a: dec.append(a)
    return {a: i for i, a in enumerate(dec)}, dec

def add(i,a):
    i+=1
    while i<bn:
        arr[i]+=a
        i+=i&-i

def bsum(i):
    res = 0
    while i > 0:
        res += arr[i]
        i -= i & -i
    return res

n=int(input())
aa=[int(input()) for _ in range(n)]
enc,dec=coordinate_compression(aa)
aa=[enc[a] for a in aa]
bn=n+5
arr=[0]*bn
cnt=[0]*bn
ans=0
for a in aa[::-1]:
    ans+=bsum(a)
    add(a,1)
    cnt[a]+=1
print(ans)
for a in aa[:-1]:
    s=bsum(a)
    ans+=n-2*s-cnt[a]
    print(ans)
0