結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー toyuzukotoyuzuko
提出日時 2020-07-19 09:58:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 1,067 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 11,012 KB
実行使用メモリ 23,284 KB
最終ジャッジ日時 2023-08-22 09:48:11
合計ジャッジ時間 8,594 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
9,640 KB
testcase_01 AC 17 ms
8,384 KB
testcase_02 AC 17 ms
8,236 KB
testcase_03 AC 370 ms
20,900 KB
testcase_04 AC 425 ms
23,012 KB
testcase_05 AC 367 ms
21,876 KB
testcase_06 AC 322 ms
19,936 KB
testcase_07 AC 421 ms
23,284 KB
testcase_08 AC 33 ms
9,160 KB
testcase_09 AC 206 ms
17,876 KB
testcase_10 AC 380 ms
23,124 KB
testcase_11 AC 17 ms
8,248 KB
testcase_12 AC 432 ms
23,248 KB
testcase_13 AC 421 ms
23,120 KB
testcase_14 AC 421 ms
23,148 KB
testcase_15 AC 16 ms
8,388 KB
testcase_16 AC 17 ms
7,864 KB
testcase_17 AC 16 ms
8,396 KB
testcase_18 AC 16 ms
8,364 KB
testcase_19 AC 17 ms
8,292 KB
testcase_20 AC 16 ms
8,288 KB
testcase_21 AC 16 ms
8,348 KB
testcase_22 AC 16 ms
8,224 KB
testcase_23 AC 44 ms
9,672 KB
testcase_24 AC 144 ms
14,408 KB
testcase_25 AC 320 ms
20,440 KB
testcase_26 AC 77 ms
11,176 KB
testcase_27 AC 178 ms
16,092 KB
testcase_28 AC 242 ms
18,300 KB
testcase_29 AC 332 ms
20,128 KB
testcase_30 AC 409 ms
22,640 KB
testcase_31 AC 98 ms
12,328 KB
testcase_32 AC 69 ms
11,020 KB
testcase_33 AC 331 ms
21,652 KB
testcase_34 AC 17 ms
8,236 KB
testcase_35 AC 16 ms
8,248 KB
testcase_36 AC 17 ms
8,208 KB
testcase_37 AC 16 ms
8,212 KB
testcase_38 AC 16 ms
8,340 KB
testcase_39 AC 17 ms
8,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryIndexedTree():  #1-indexed
    def __init__(self, n):
        self.n = n
        self.tree = [0 for _ in range(n + 1)]

    def sum(self, idx):
        res = 0
        while idx:
            res += self.tree[idx]
            idx -= idx & -idx
        return res

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

    def bisect_left(self, x):
        if x <= 0: return 0
        res, tmp = 0, 2**((self.n).bit_length() - 1)
        while tmp:
            if res + tmp <= self.n and self.tree[res + tmp] < x:
                x -= self.tree[res + tmp]
                res += tmp
            tmp >>= 1
        return res + 1

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ind = [None for _ in range(N)]

for i in range(N):
    ind[A[i] - 1] = i + 1

C = [None for _ in range(N)]

for i in range(N):
    C[i] = ind[B[i] - 1]

bit = BinaryIndexedTree(N)
res = 0

for i in range(N):
    bit.add(C[i], 1)
    res += i + 1 - bit.sum(C[i])

print(res)
0