結果

問題 No.1209 XOR Into You
ユーザー kumatan400kumatan400
提出日時 2022-07-12 00:41:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 1,171 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 170,312 KB
最終ジャッジ日時 2023-09-04 22:51:18
合計ジャッジ時間 16,954 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 177 ms
80,592 KB
testcase_01 AC 74 ms
70,952 KB
testcase_02 AC 176 ms
80,780 KB
testcase_03 AC 178 ms
80,808 KB
testcase_04 AC 115 ms
94,056 KB
testcase_05 AC 114 ms
93,924 KB
testcase_06 AC 154 ms
95,452 KB
testcase_07 AC 406 ms
156,832 KB
testcase_08 AC 387 ms
148,708 KB
testcase_09 AC 389 ms
148,708 KB
testcase_10 AC 344 ms
132,496 KB
testcase_11 AC 384 ms
146,576 KB
testcase_12 AC 370 ms
140,248 KB
testcase_13 AC 400 ms
152,868 KB
testcase_14 AC 373 ms
138,856 KB
testcase_15 AC 379 ms
141,492 KB
testcase_16 AC 381 ms
140,608 KB
testcase_17 AC 371 ms
138,456 KB
testcase_18 AC 458 ms
167,236 KB
testcase_19 AC 373 ms
140,680 KB
testcase_20 AC 401 ms
152,992 KB
testcase_21 AC 340 ms
131,380 KB
testcase_22 AC 373 ms
170,120 KB
testcase_23 AC 376 ms
170,312 KB
testcase_24 AC 374 ms
170,180 KB
testcase_25 AC 420 ms
170,188 KB
testcase_26 AC 418 ms
170,168 KB
testcase_27 AC 417 ms
170,188 KB
testcase_28 AC 422 ms
170,136 KB
testcase_29 AC 417 ms
170,172 KB
testcase_30 AC 418 ms
170,228 KB
testcase_31 AC 309 ms
101,696 KB
testcase_32 AC 312 ms
101,388 KB
testcase_33 AC 312 ms
101,364 KB
testcase_34 AC 310 ms
101,852 KB
testcase_35 AC 311 ms
101,380 KB
testcase_36 AC 308 ms
101,644 KB
testcase_37 AC 271 ms
101,780 KB
testcase_38 AC 377 ms
156,188 KB
testcase_39 AC 360 ms
146,104 KB
testcase_40 AC 372 ms
170,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class fenwick_tree():
    n=1
    data=[0 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.data=[0 for i in range(N)]
    def add(self,p,x):
        assert 0<=p<self.n,"0<=p<n,p={0},n={1}".format(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 and l<=r and r<=self.n),"0<=l<=r<=n,l={0},r={1},n={2}".format(l,r,self.n)
        return self.sum0(r)-self.sum0(l)
    def sum0(self,r):
        s=0
        while(r>0):
            s+=self.data[r-1]
            r-=r&-r
        return s





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

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


A = [A[i] ^ A[i+1] for i in range(N-1)]
B = [B[i] ^ B[i+1] for i in range(N-1)]


if sorted(A) != sorted(B):
    print(-1)
    exit()

import queue
X = {}
for i, b in enumerate(B):
    if b in X:
        X[b].append(i)
    else:
        X[b] = queue.deque([i])

Z = []
for a in A:
    Z.append(X[a].popleft())

bit = fenwick_tree(N-1)
ans = 0
for a in Z[::-1]:
    ans += bit.sum0(a)
    bit.add(a, 1)
print(ans)
0