結果
問題 | No.1209 XOR Into You |
ユーザー |
![]() |
提出日時 | 2023-08-07 19:10:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 336 ms / 2,000 ms |
コード長 | 1,122 bytes |
コンパイル時間 | 223 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 162,160 KB |
最終ジャッジ日時 | 2024-11-08 00:11:50 |
合計ジャッジ時間 | 12,747 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 37 |
ソースコード
class fenwick_tree():n=1data=[0 for i in range(n)]def __init__(self,N):self.n=Nself.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+=1while(p<=self.n):self.data[p-1]+=xp+=p& -pdef 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=0while(r>0):s+=self.data[r-1]r-=r&-rreturn sN = int(input())A = list(map(int, input().split()))B = list(map(int, input().split()))if A[0] != B[0]:print(-1)exit()X = [A[i+1]^A[i] for i in range(N-1)]Y = [B[i+1]^B[i] for i in range(N-1)]if sorted(X) != sorted(Y):print(-1)exit()import collectionsC = collections.defaultdict(collections.deque)for i, y in enumerate(Y):C[y].append(i)Z = []for x in X:Z.append(C[x].popleft())G = fenwick_tree(N)ans = 0for z in Z[::-1]:ans += G.sum0(z)G.add(z, 1)print(ans)