結果

問題 No.2089 置換の符号
ユーザー lloyz
提出日時 2022-09-30 21:55:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 774 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 76,108 KB
最終ジャッジ日時 2024-12-22 23:29:31
合計ジャッジ時間 2,953 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

class Fenwick_Tree:
    def __init__(self, n):
        self.n = n
        self.data = [0] * n

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

    def sum(self, l, r):
        '''範囲[l, r)(lからr-1まで)の総和を求める'''
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        '''範囲[0, r)(0からr-1まで)の総和を求める'''
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

n = int(input())
S = list(map(int, input().split()))

FT = Fenwick_Tree(n + 1)
ans = 0
for i in range(n):
    a = S[i]
    FT.add(a, 1)
    ans += FT.sum(a + 1, n + 1)
if ans % 2 == 0:
    print(1)
else:
    print(-1)
0