結果

問題 No.694 square1001 and Permutation 3
コンテスト
ユーザー GrayCoder
提出日時 2018-06-10 04:08:48
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 2,791 ms / 3,000 ms
コード長 916 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 662 ms
コンパイル使用メモリ 20,828 KB
実行使用メモリ 81,244 KB
最終ジャッジ日時 2026-03-20 13:08:13
合計ジャッジ時間 16,867 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from sys import stdin
from bisect import bisect, bisect_left

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

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

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

def main():
    N = int(input())
    A = list(map(int, stdin.read().splitlines()))

    if N == 1:
        print(0)
        return

    cnt = 0
    ans = []
    apnd = ans.append
    bit = BIT(N)
    a = sorted(set(A))
    for i, v in enumerate(A):
        x = bisect(a, v)
        cnt += i - bit.sum(x)
        bit.add(x, 1)
    apnd(cnt)

    num = sorted(A)
    for i in range(N - 1):
        cnt -= bisect_left(num, A[i])
        cnt += N - bisect(num, A[i])
        apnd(cnt)
    print(*ans, sep='\n')
main()
0