結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー lam6er
提出日時 2025-03-20 21:06:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 984 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 82,140 KB
実行使用メモリ 104,772 KB
最終ジャッジ日時 2025-03-20 21:07:01
合計ジャッジ時間 4,700 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

class FenwickTree:
    def __init__(self, size):
        self.n = size
        self.tree = [0] * (self.n + 2)  # 1-based indexing

    def update(self, idx):
        idx += 1  # convert to 1-based index
        while idx <= self.n:
            self.tree[idx] += 1
            idx += idx & -idx

    def query(self, idx):
        if idx < 0:
            return 0
        idx += 1  # convert to 1-based index
        res = 0
        while idx > 0:
            res += self.tree[idx]
            idx -= idx & -idx
        return res

def main():
    n = int(sys.stdin.readline())
    A = list(map(int, sys.stdin.readline().split()))
    B = list(map(int, sys.stdin.readline().split()))
    
    pos_b = [0] * (n + 1)
    for i in range(n):
        pos_b[B[i]] = i
    
    S = [pos_b[x] for x in A]
    
    ft = FenwickTree(n)
    ans = 0
    for s in reversed(S):
        ans += ft.query(s - 1)
        ft.update(s)
    
    print(ans)

if __name__ == "__main__":
    main()
0