結果

問題 No.1209 XOR Into You
ユーザー salmon0852
提出日時 2022-08-12 17:04:55
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 750 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 46,736 KB
最終ジャッジ日時 2024-09-22 19:24:36
合計ジャッジ時間 24,044 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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