結果

問題 No.1209 XOR Into You
ユーザー kumatan400kumatan400
提出日時 2021-10-25 19:32:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,288 KB
実行使用メモリ 130,356 KB
最終ジャッジ日時 2024-04-14 15:54:49
合計ジャッジ時間 8,652 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,156 KB
testcase_01 AC 39 ms
53,340 KB
testcase_02 AC 38 ms
53,632 KB
testcase_03 AC 38 ms
52,588 KB
testcase_04 AC 84 ms
96,980 KB
testcase_05 AC 83 ms
97,324 KB
testcase_06 AC 123 ms
117,516 KB
testcase_07 AC 211 ms
118,768 KB
testcase_08 AC 186 ms
106,732 KB
testcase_09 AC 187 ms
106,480 KB
testcase_10 AC 158 ms
96,948 KB
testcase_11 AC 184 ms
106,268 KB
testcase_12 AC 171 ms
103,452 KB
testcase_13 AC 188 ms
114,508 KB
testcase_14 AC 165 ms
102,780 KB
testcase_15 AC 170 ms
103,156 KB
testcase_16 AC 170 ms
103,076 KB
testcase_17 AC 163 ms
102,568 KB
testcase_18 AC 222 ms
128,176 KB
testcase_19 AC 173 ms
103,080 KB
testcase_20 AC 194 ms
114,420 KB
testcase_21 AC 155 ms
96,996 KB
testcase_22 AC 171 ms
130,196 KB
testcase_23 AC 170 ms
130,072 KB
testcase_24 AC 172 ms
129,952 KB
testcase_25 AC 193 ms
130,192 KB
testcase_26 AC 192 ms
130,188 KB
testcase_27 AC 191 ms
129,936 KB
testcase_28 AC 192 ms
129,940 KB
testcase_29 AC 191 ms
130,088 KB
testcase_30 AC 195 ms
130,356 KB
testcase_31 AC 155 ms
106,264 KB
testcase_32 AC 154 ms
106,276 KB
testcase_33 AC 158 ms
106,244 KB
testcase_34 AC 157 ms
107,376 KB
testcase_35 AC 156 ms
106,116 KB
testcase_36 AC 154 ms
106,252 KB
testcase_37 AC 147 ms
112,232 KB
testcase_38 AC 168 ms
118,512 KB
testcase_39 AC 152 ms
106,144 KB
testcase_40 AC 169 ms
129,928 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()


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

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

bit = fenwick_tree(N)

M = {}
for b in B2:
    M[b] = []
for i, b in enumerate(B2):
    M[b].append(i)


ans = 0
for i in range(N-2, -1, -1):
    a = A2[i]

    if not a in M or not M[a]:
        print(-1)
        exit()

    p = M[a][-1] + 1

    ans += bit.sum0(p)
    bit.add(p, 1)
    M[a].pop()
print(ans)




0