結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー irumo8202
提出日時 2022-01-26 17:18:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 792 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 104,748 KB
最終ジャッジ日時 2024-12-23 09:15:06
合計ジャッジ時間 5,728 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


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

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

    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s


ind = [-1] * (N + 1)
for i, b in enumerate(B, 1):
    ind[b] = i

bit = Fenwick_Tree(N + 1)
ans = 0
for i, a in enumerate(A):
    id = ind[a]
    ans += i - bit.sum(0, id)
    bit.add(id, 1)


print(ans)
0