結果

問題 No.1209 XOR Into You
ユーザー rlangevinrlangevin
提出日時 2023-11-08 12:06:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 1,438 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 145,388 KB
最終ジャッジ日時 2023-11-08 12:06:22
合計ジャッジ時間 11,407 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,608 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 39 ms
55,608 KB
testcase_03 AC 39 ms
55,608 KB
testcase_04 AC 139 ms
131,468 KB
testcase_05 AC 167 ms
131,808 KB
testcase_06 AC 133 ms
131,588 KB
testcase_07 AC 254 ms
142,840 KB
testcase_08 AC 236 ms
136,788 KB
testcase_09 AC 246 ms
137,304 KB
testcase_10 AC 192 ms
121,296 KB
testcase_11 AC 230 ms
135,760 KB
testcase_12 AC 221 ms
133,856 KB
testcase_13 AC 262 ms
138,300 KB
testcase_14 AC 211 ms
133,504 KB
testcase_15 AC 215 ms
134,104 KB
testcase_16 AC 215 ms
134,008 KB
testcase_17 AC 217 ms
133,404 KB
testcase_18 AC 283 ms
145,388 KB
testcase_19 AC 219 ms
133,996 KB
testcase_20 AC 248 ms
138,372 KB
testcase_21 AC 197 ms
121,028 KB
testcase_22 AC 218 ms
142,700 KB
testcase_23 AC 224 ms
142,844 KB
testcase_24 AC 225 ms
142,844 KB
testcase_25 AC 276 ms
142,848 KB
testcase_26 AC 253 ms
142,848 KB
testcase_27 AC 260 ms
142,852 KB
testcase_28 AC 254 ms
142,860 KB
testcase_29 AC 252 ms
142,848 KB
testcase_30 AC 256 ms
142,860 KB
testcase_31 AC 183 ms
113,792 KB
testcase_32 AC 187 ms
113,788 KB
testcase_33 AC 182 ms
113,788 KB
testcase_34 AC 181 ms
113,888 KB
testcase_35 AC 186 ms
113,784 KB
testcase_36 AC 186 ms
113,788 KB
testcase_37 AC 177 ms
121,904 KB
testcase_38 AC 234 ms
143,956 KB
testcase_39 AC 213 ms
135,672 KB
testcase_40 AC 221 ms
142,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *

class Fenwick_Tree:
    def __init__(self, n):
        self._n = n
        self.data = [0] * n
 
    def add(self, p, x):
        assert 0 <= p < self._n
        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p
 
    def sum(self, l, r):
        assert 0 <= l <= r <= self._n
        return self._sum(r) - self._sum(l)
 
    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s
    
    def get(self, k):
        k += 1
        x, r = 0, 1
        while r < self._n:
            r <<= 1
        len = r
        while len:
            if x + len - 1 < self._n:
                if self.data[x + len - 1] < k:
                    k -= self.data[x + len - 1]
                    x += len
            len >>= 1
        return x

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
DA, DB = [], []
for i in range(N - 1):
    DA.append(A[i+1]^A[i])
    DB.append(B[i+1]^B[i])

if Counter(DA) != Counter(DB) or A[0] != B[0] or A[-1] != B[-1]:
    print(-1)
    exit()

ind = defaultdict(list)
C = []
for i in range(N-1):
    ind[DA[i]].append(i)

for k, v in ind.items():
    ind[k].reverse()

for i in range(N-1):
    C.append(ind[DB[i]].pop())

T = Fenwick_Tree(N - 1)
ans = 0
for i in range(N - 1):
    ans += T.sum(C[i], N - 1)
    T.add(C[i], 1)

print(ans)
0