結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー titiatitia
提出日時 2020-07-17 21:38:14
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 10,860 KB
実行使用メモリ 28,624 KB
最終ジャッジ日時 2023-08-20 00:07:56
合計ジャッジ時間 8,846 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
9,952 KB
testcase_01 AC 15 ms
7,840 KB
testcase_02 AC 15 ms
7,968 KB
testcase_03 AC 381 ms
23,592 KB
testcase_04 AC 434 ms
28,624 KB
testcase_05 AC 380 ms
23,624 KB
testcase_06 AC 343 ms
22,512 KB
testcase_07 AC 429 ms
28,608 KB
testcase_08 AC 32 ms
9,208 KB
testcase_09 AC 239 ms
20,700 KB
testcase_10 AC 415 ms
28,384 KB
testcase_11 AC 15 ms
7,756 KB
testcase_12 AC 438 ms
28,468 KB
testcase_13 AC 451 ms
28,496 KB
testcase_14 AC 449 ms
28,572 KB
testcase_15 AC 15 ms
7,784 KB
testcase_16 AC 14 ms
7,884 KB
testcase_17 AC 14 ms
7,700 KB
testcase_18 AC 14 ms
7,788 KB
testcase_19 AC 14 ms
7,776 KB
testcase_20 AC 14 ms
7,768 KB
testcase_21 AC 14 ms
7,756 KB
testcase_22 AC 15 ms
7,756 KB
testcase_23 AC 45 ms
9,904 KB
testcase_24 AC 154 ms
15,228 KB
testcase_25 AC 332 ms
23,124 KB
testcase_26 AC 83 ms
11,680 KB
testcase_27 AC 193 ms
18,704 KB
testcase_28 AC 260 ms
21,524 KB
testcase_29 AC 348 ms
22,160 KB
testcase_30 AC 434 ms
27,620 KB
testcase_31 AC 110 ms
13,640 KB
testcase_32 AC 76 ms
11,556 KB
testcase_33 AC 370 ms
23,944 KB
testcase_34 AC 15 ms
7,728 KB
testcase_35 AC 14 ms
7,904 KB
testcase_36 AC 14 ms
7,844 KB
testcase_37 AC 14 ms
7,760 KB
testcase_38 AC 14 ms
7,704 KB
testcase_39 AC 14 ms
7,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

DICT=dict()

for i in range(n):
    DICT[B[i]]=i+1

for i in range(n):
    A[i]=DICT[A[i]]

LEN=len(A)
MAX=max(A)
MIN=min(A)

BIT=[0]*(MAX-MIN+2) # 出現回数をbit indexed treeの形でもっておく.

def update(v,w): # index vにwを加える
    while v<=MAX-MIN+1:
        BIT[v]+=w
        v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalue(v): # MIN~vの区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v)) # たとえばv=3→v=2へ
    return ANS

ANS=0        
for i in range(LEN): # A[0],A[1],...とBITを更新しながら,各A[i]について転倒数を求める.
    bit_ai=A[i]-MIN+1 # A[i]がBITの中で何番目か

    ANS+=i # 今まで出現した個数.
    ANS-=getvalue(bit_ai) # 今まで出現した中で,MIN~bit_aiの個数を減らす.
    # bit_ai~MAXの出現個数が転倒数

    update(bit_ai,1)

print(ANS)
0