結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー yuly3yuly3
提出日時 2020-07-18 22:16:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,205 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 86,804 KB
実行使用メモリ 106,164 KB
最終ジャッジ日時 2023-08-21 02:06:16
合計ジャッジ時間 9,182 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
77,816 KB
testcase_01 AC 71 ms
71,232 KB
testcase_02 AC 71 ms
71,084 KB
testcase_03 AC 147 ms
100,332 KB
testcase_04 AC 157 ms
105,928 KB
testcase_05 AC 146 ms
100,516 KB
testcase_06 AC 139 ms
97,868 KB
testcase_07 AC 158 ms
105,912 KB
testcase_08 AC 86 ms
76,668 KB
testcase_09 AC 128 ms
102,532 KB
testcase_10 AC 151 ms
105,892 KB
testcase_11 AC 72 ms
71,500 KB
testcase_12 AC 160 ms
106,032 KB
testcase_13 AC 158 ms
106,164 KB
testcase_14 AC 158 ms
105,968 KB
testcase_15 AC 71 ms
71,228 KB
testcase_16 AC 70 ms
71,232 KB
testcase_17 AC 69 ms
71,552 KB
testcase_18 AC 70 ms
71,260 KB
testcase_19 AC 71 ms
71,384 KB
testcase_20 AC 69 ms
71,244 KB
testcase_21 AC 70 ms
71,400 KB
testcase_22 AC 72 ms
71,300 KB
testcase_23 AC 90 ms
77,828 KB
testcase_24 AC 111 ms
88,660 KB
testcase_25 AC 141 ms
99,196 KB
testcase_26 AC 99 ms
80,636 KB
testcase_27 AC 120 ms
93,768 KB
testcase_28 AC 135 ms
102,784 KB
testcase_29 AC 143 ms
97,396 KB
testcase_30 AC 159 ms
105,616 KB
testcase_31 AC 102 ms
83,456 KB
testcase_32 AC 95 ms
79,680 KB
testcase_33 AC 142 ms
100,452 KB
testcase_34 AC 72 ms
71,040 KB
testcase_35 AC 70 ms
71,236 KB
testcase_36 AC 72 ms
71,568 KB
testcase_37 AC 72 ms
71,196 KB
testcase_38 AC 74 ms
71,508 KB
testcase_39 AC 74 ms
71,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


class BinaryIndexedTree:
    # 1-indexed
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
    
    def add(self, i, x):
        # Accessed by 0-indexed
        i += 1
        while i <= self.n:
            self.data[i] += x
            i += i & -i
    
    def sum(self, i):
        # [0, i)
        res = 0
        while i:
            res += self.data[i]
            i -= i & -i
        return res
    
    def bisect_left(self, w):
        if w <= 0:
            return 0
        i = 0
        k = 1 << (self.n.bit_length() - 1)
        while 0 < k:
            if i + k <= self.n and self.data[i + k] < w:
                w -= self.data[i + k]
                i += k
            k >>= 1
        return i + 1


def solve():
    n = int(rl())
    a = list(map(int, rl().split()))
    b = list(map(int, rl().split()))
    
    b_to_idx = {bi: i for i, bi in enumerate(b)}
    c = [b_to_idx[ai] for ai in a]
    
    bit = BinaryIndexedTree(n)
    ans = 0
    for i, ci in enumerate(c):
        bit.add(ci, 1)
        ans += i + 1 - bit.sum(ci + 1)
    print(ans)


if __name__ == '__main__':
    solve()
0