結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー kojihashimoto-kobe
提出日時 2021-08-18 12:15:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 696 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 105,872 KB
最終ジャッジ日時 2024-10-11 12:48:27
合計ジャッジ時間 4,938 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#簡単のため、aを1, 2, 3,4, …だとして考えるための操作
tmp = [0]*(n+1)
for i in range(n):
  tmp[a[i]] = i+1

b = [0]*n
for i in range(n):
  b[i] = tmp[B[i]]

# i<j かつ b[i]>b[j]となる組の個数が求めるもの(∵操作によって左の数が1減るから)

# 1-indexed
bit = [0]*(n+1)

def bit_query(i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i & (-i)
    return res

def bit_update(i, x):
    while i <= n:
        bit[i] += x
        i += i & (-i)

ans = 0
for j in range(n):
    ans += j-bit_query(b[j])
    bit_update(b[j]+1, 1)

print(ans)
0