結果

問題 No.694 square1001 and Permutation 3
ユーザー GrayCoderGrayCoder
提出日時 2018-06-09 20:18:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,045 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 66,524 KB
最終ジャッジ日時 2023-09-13 02:20:41
合計ジャッジ時間 10,020 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 19 ms
8,148 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 2,250 ms
21,568 KB
testcase_08 TLE -
testcase_09 RE -
testcase_10 AC 2,147 ms
19,988 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 16 ms
8,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
from bisect import bisect, bisect_left

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

    def add(self, i, x):
        'add c to the value at index i'
        while i <= self.size:
            self.bit[i] += x
            i += i & -i

    def sum(self, i):
        'get sum from the start to the index of i'
        ret = 0
        while i > 0:
            ret += self.bit[i]
            i -= i & -i
        return ret

    def range_sum(self, start, end):
        'Calculate a[start], a[start+1], ... a[end]'
        return self.sum(end) - self.sum(start - 1)

    def __str__(self):
        return str(self.bit)

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

    ans = 0
    bit = BIT(N)
    for i, v in enumerate(A):
        ans += i - bit.sum(v)
        bit.add(v, 1)
    print(ans)

    num = sorted(A)
    for i in range(N - 1):
        ans -= bisect_left(num, A[i])
        ans += N - bisect(num, A[i])
        print(ans)

main()
0