結果

問題 No.1209 XOR Into You
ユーザー kumatan400kumatan400
提出日時 2021-12-31 07:32:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,180 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 142,400 KB
最終ジャッジ日時 2024-04-16 17:56:28
合計ジャッジ時間 10,852 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,352 KB
testcase_01 AC 41 ms
52,352 KB
testcase_02 AC 41 ms
52,480 KB
testcase_03 AC 41 ms
52,224 KB
testcase_04 AC 95 ms
97,280 KB
testcase_05 AC 95 ms
97,152 KB
testcase_06 AC 133 ms
97,024 KB
testcase_07 AC 242 ms
129,688 KB
testcase_08 AC 221 ms
116,764 KB
testcase_09 AC 221 ms
116,292 KB
testcase_10 AC 189 ms
107,392 KB
testcase_11 AC 217 ms
116,112 KB
testcase_12 AC 205 ms
111,984 KB
testcase_13 AC 233 ms
125,016 KB
testcase_14 AC 203 ms
111,360 KB
testcase_15 AC 209 ms
112,804 KB
testcase_16 AC 206 ms
111,748 KB
testcase_17 AC 202 ms
111,244 KB
testcase_18 AC 281 ms
140,484 KB
testcase_19 AC 217 ms
112,464 KB
testcase_20 AC 240 ms
124,500 KB
testcase_21 AC 194 ms
106,800 KB
testcase_22 AC 208 ms
141,496 KB
testcase_23 AC 210 ms
141,496 KB
testcase_24 AC 207 ms
141,108 KB
testcase_25 AC 264 ms
142,284 KB
testcase_26 AC 262 ms
142,144 KB
testcase_27 AC 264 ms
142,400 KB
testcase_28 AC 261 ms
142,032 KB
testcase_29 AC 263 ms
142,396 KB
testcase_30 AC 262 ms
142,136 KB
testcase_31 AC 198 ms
105,020 KB
testcase_32 AC 198 ms
105,272 KB
testcase_33 AC 201 ms
105,272 KB
testcase_34 AC 202 ms
107,012 KB
testcase_35 AC 201 ms
105,144 KB
testcase_36 AC 195 ms
105,268 KB
testcase_37 AC 166 ms
110,228 KB
testcase_38 AC 230 ms
130,056 KB
testcase_39 AC 210 ms
116,436 KB
testcase_40 AC 207 ms
141,364 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[N-1] != B[N-1]:
    print(-1)
    exit()

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

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


X = {}
for i, b in enumerate(B):
    if b in X:
        X[b].append(i)
    else:
        X[b] = [i]
cur = {x:0 for x in X}

Z = []
for a in A:
    Z.append(X[a][cur[a]])
    cur[a] += 1



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


0