結果
| 問題 | 
                            No.1115 二つの数列 / Two Sequences
                             | 
                    
| コンテスト | |
| ユーザー | 
                             lam6er
                         | 
                    
| 提出日時 | 2025-03-20 20:26:44 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 118 ms / 2,000 ms | 
| コード長 | 1,088 bytes | 
| コンパイル時間 | 150 ms | 
| コンパイル使用メモリ | 82,672 KB | 
| 実行使用メモリ | 106,680 KB | 
| 最終ジャッジ日時 | 2025-03-20 20:27:51 | 
| 合計ジャッジ時間 | 4,945 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 35 | 
ソースコード
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# Bの各要素の位置を記録
pos_b = [0] * (n + 1)  # 値は1〜nなので、0番目は使わない
for idx in range(n):
    val = B[idx]
    pos_b[val] = idx
# AをBの位置に変換して配列Cを作成
C = []
for val in A:
    C.append(pos_b[val])
# 転倒数を計算するためのFenwick Tree
class FenwickTree:
    def __init__(self, size):
        self.n = size
        self.tree = [0] * (self.n + 2)  # 1-based indexing
    
    def update(self, idx, delta):
        # idxは1-based
        while idx <= self.n:
            self.tree[idx] += delta
            idx += idx & -idx
    
    def query(self, idx):
        # 1-basedのidxまでの合計を返す
        res = 0
        while idx > 0:
            res += self.tree[idx]
            idx -= idx & -idx
        return res
ft = FenwickTree(n)
ans = 0
# 逆順に処理して転倒数を計算
for x in reversed(C):
    ans += ft.query(x)
    ft.update(x + 1, 1)  # xは0-basedなので、1-basedのx+1に更新
print(ans)
            
            
            
        
            
lam6er