結果

問題 No.1209 XOR Into You
ユーザー salmon0852salmon0852
提出日時 2022-08-12 17:04:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 683 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 11,784 KB
実行使用メモリ 47,464 KB
最終ジャッジ日時 2023-10-24 02:29:08
合計ジャッジ時間 22,339 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,100 KB
testcase_01 AC 31 ms
10,100 KB
testcase_02 AC 32 ms
10,100 KB
testcase_03 AC 31 ms
10,100 KB
testcase_04 AC 97 ms
25,068 KB
testcase_05 AC 95 ms
25,068 KB
testcase_06 AC 268 ms
45,652 KB
testcase_07 AC 590 ms
39,724 KB
testcase_08 AC 525 ms
37,108 KB
testcase_09 AC 527 ms
37,244 KB
testcase_10 AC 411 ms
32,244 KB
testcase_11 AC 511 ms
36,916 KB
testcase_12 AC 482 ms
35,128 KB
testcase_13 AC 540 ms
38,152 KB
testcase_14 AC 469 ms
34,612 KB
testcase_15 AC 509 ms
35,900 KB
testcase_16 AC 486 ms
35,272 KB
testcase_17 AC 468 ms
34,284 KB
testcase_18 AC 683 ms
46,048 KB
testcase_19 AC 482 ms
35,288 KB
testcase_20 AC 550 ms
38,224 KB
testcase_21 AC 413 ms
31,756 KB
testcase_22 AC 596 ms
47,440 KB
testcase_23 AC 598 ms
47,336 KB
testcase_24 AC 604 ms
47,464 KB
testcase_25 AC 646 ms
47,416 KB
testcase_26 AC 642 ms
47,416 KB
testcase_27 AC 637 ms
47,416 KB
testcase_28 AC 651 ms
47,308 KB
testcase_29 AC 642 ms
47,416 KB
testcase_30 AC 645 ms
47,416 KB
testcase_31 AC 561 ms
30,572 KB
testcase_32 AC 559 ms
30,308 KB
testcase_33 AC 563 ms
30,308 KB
testcase_34 AC 568 ms
30,216 KB
testcase_35 AC 565 ms
30,572 KB
testcase_36 AC 566 ms
30,572 KB
testcase_37 AC 505 ms
31,488 KB
testcase_38 AC 510 ms
39,512 KB
testcase_39 AC 455 ms
36,616 KB
testcase_40 AC 607 ms
47,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

if a[0] != b[0] or a[-1] != b[-1]:
    print(-1)
    exit()

xa, xb = [], []
for i in range(n-1):
    xa.append(a[i] ^ a[i+1])
    xb.append(b[i] ^ b[i+1])

d = defaultdict(list)
for i, x in enumerate(xb): d[x].append(i)

max_n = 1
while max_n < n - 1: max_n <<= 1
bit = [0] * (max_n + 1)

def add(i):
    while i <= max_n:
        bit[i] += 1
        i += (~i + 1) & i

def query(i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= (~i + 1) & i
    return res

ans = 0
for x in xa[::-1]:
    x = xa.pop()
    if not d[x]:
        ans = -1
        break
    
    i = d[x].pop() + 1
    ans += query(i)
    add(i)
print(ans)

0