結果

問題 No.1209 XOR Into You
ユーザー kumatan400kumatan400
提出日時 2022-12-07 17:20:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 1,204 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 166,684 KB
最終ジャッジ日時 2024-04-22 00:14:59
合計ジャッジ時間 12,069 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
67,200 KB
testcase_01 AC 38 ms
52,352 KB
testcase_02 AC 62 ms
67,328 KB
testcase_03 AC 63 ms
67,328 KB
testcase_04 AC 83 ms
97,016 KB
testcase_05 AC 87 ms
97,408 KB
testcase_06 AC 129 ms
104,528 KB
testcase_07 AC 320 ms
151,888 KB
testcase_08 AC 291 ms
140,196 KB
testcase_09 AC 290 ms
140,256 KB
testcase_10 AC 248 ms
128,580 KB
testcase_11 AC 289 ms
138,808 KB
testcase_12 AC 266 ms
134,916 KB
testcase_13 AC 312 ms
146,492 KB
testcase_14 AC 271 ms
134,016 KB
testcase_15 AC 272 ms
136,380 KB
testcase_16 AC 271 ms
135,704 KB
testcase_17 AC 265 ms
133,408 KB
testcase_18 AC 356 ms
163,392 KB
testcase_19 AC 265 ms
135,412 KB
testcase_20 AC 308 ms
146,372 KB
testcase_21 AC 250 ms
127,460 KB
testcase_22 AC 273 ms
166,316 KB
testcase_23 AC 275 ms
166,684 KB
testcase_24 AC 275 ms
166,596 KB
testcase_25 AC 321 ms
166,512 KB
testcase_26 AC 325 ms
166,644 KB
testcase_27 AC 321 ms
166,472 KB
testcase_28 AC 322 ms
166,560 KB
testcase_29 AC 317 ms
166,564 KB
testcase_30 AC 321 ms
166,628 KB
testcase_31 AC 215 ms
111,056 KB
testcase_32 AC 216 ms
111,052 KB
testcase_33 AC 212 ms
111,172 KB
testcase_34 AC 218 ms
112,168 KB
testcase_35 AC 216 ms
111,048 KB
testcase_36 AC 219 ms
111,116 KB
testcase_37 AC 177 ms
111,236 KB
testcase_38 AC 283 ms
151,180 KB
testcase_39 AC 254 ms
138,256 KB
testcase_40 AC 273 ms
166,480 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()

X = []
Y = []
for i in range(N-1):
    X.append(A[i+1] ^ A[i])
    Y.append(B[i+1] ^ B[i])

if sorted(X) != sorted(Y):
    print(-1)
    exit()

import queue
Z = {}
g = 0
for y in Y:
    if not y in Z:
        Z[y] = queue.deque([g])
        g += 1
    else:
        Z[y].append(g)
        g += 1


T = []
for x in X:
    T.append(Z[x].popleft())
X = T

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