結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー c-yanc-yan
提出日時 2021-03-23 06:21:00
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 666 ms / 2,000 ms
コード長 840 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 33,164 KB
最終ジャッジ日時 2024-11-24 22:11:56
合計ジャッジ時間 11,842 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryIndexedTree:
    def __init__(self, size):
        self._data = [0] * size

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

    def _sum(self, stop):
        data = self._data
        result = 0
        i = stop
        while i > 0:
            result += data[i - 1]
            i -= i & -i
        return result

    def range_sum(self, start, stop):
        return self._sum(stop) - self._sum(start)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

d = {}
for i in range(n):
    d[a[i] - 1] = i
for i in range(n):
    b[i] = d[b[i] - 1]

bit = BinaryIndexedTree(n + 1)

result = 0
for x in b:
    result += bit.range_sum(x + 1, n + 1)
    bit.add(x, 1)
print(result)
0