結果

問題 No.1209 XOR Into You
ユーザー kumatan400kumatan400
提出日時 2023-02-24 00:47:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 1,144 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 86,908 KB
実行使用メモリ 165,736 KB
最終ジャッジ日時 2023-09-30 23:47:33
合計ジャッジ時間 17,091 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,192 KB
testcase_01 AC 78 ms
71,256 KB
testcase_02 AC 95 ms
71,540 KB
testcase_03 AC 93 ms
71,428 KB
testcase_04 AC 116 ms
93,792 KB
testcase_05 AC 113 ms
93,740 KB
testcase_06 AC 150 ms
95,388 KB
testcase_07 AC 352 ms
150,660 KB
testcase_08 AC 307 ms
142,704 KB
testcase_09 AC 309 ms
142,852 KB
testcase_10 AC 272 ms
128,352 KB
testcase_11 AC 307 ms
140,928 KB
testcase_12 AC 286 ms
138,668 KB
testcase_13 AC 316 ms
145,076 KB
testcase_14 AC 282 ms
137,168 KB
testcase_15 AC 290 ms
139,448 KB
testcase_16 AC 287 ms
138,588 KB
testcase_17 AC 276 ms
136,160 KB
testcase_18 AC 360 ms
162,488 KB
testcase_19 AC 283 ms
138,600 KB
testcase_20 AC 314 ms
144,684 KB
testcase_21 AC 260 ms
127,120 KB
testcase_22 AC 290 ms
165,100 KB
testcase_23 AC 285 ms
165,724 KB
testcase_24 AC 288 ms
165,340 KB
testcase_25 AC 327 ms
165,248 KB
testcase_26 AC 325 ms
165,600 KB
testcase_27 AC 325 ms
165,476 KB
testcase_28 AC 326 ms
165,456 KB
testcase_29 AC 327 ms
165,456 KB
testcase_30 AC 328 ms
165,736 KB
testcase_31 AC 235 ms
100,368 KB
testcase_32 AC 235 ms
100,380 KB
testcase_33 AC 235 ms
100,212 KB
testcase_34 AC 238 ms
101,356 KB
testcase_35 AC 237 ms
100,068 KB
testcase_36 AC 235 ms
100,208 KB
testcase_37 AC 202 ms
99,460 KB
testcase_38 AC 296 ms
149,960 KB
testcase_39 AC 275 ms
140,928 KB
testcase_40 AC 288 ms
165,400 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 collections
Y = collections.defaultdict(collections.deque)
for i, b in enumerate(B):
    Y[b].append(i)

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

G = fenwick_tree(N-1)
ans = 0
for x in X[::-1]:
    ans += G.sum0(x)
    G.add(x, 1)

print(ans)
0