結果

問題 No.1209 XOR Into You
ユーザー kumatan400
提出日時 2021-10-25 19:30:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,133 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 131,788 KB
最終ジャッジ日時 2024-10-03 15:49:02
合計ジャッジ時間 7,503 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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())) + [0]
B = list(map(int, input().split())) + [0]

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

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

bit = fenwick_tree(N+1)

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


ans = 0
for i in range(N-1, -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