結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー ttr
提出日時 2020-07-17 22:03:59
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 519 ms / 2,000 ms
コード長 675 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 24,612 KB
最終ジャッジ日時 2024-11-30 00:02:10
合計ジャッジ時間 9,688 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]

Bidx = [0]*(N+1)
for i in range(N):
    Bidx[B[i]] = i

for i in range(N):
    B[Bidx[A[i]]] = i+1

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

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

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

num = 1
while num <= N:
    num *= 2

bit = Bit(num)
ans = 0

for i, p in enumerate(B):
    bit.add(p, 1)
    ans += i + 1 - bit.sum(p)

print(ans)
0