結果

問題 No.1209 XOR Into You
ユーザー ayaoni
提出日時 2021-07-22 14:33:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 1,337 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 136,088 KB
最終ジャッジ日時 2024-07-17 14:45:33
合計ジャッジ時間 8,161 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N = I()
A,B = LI(),LI()

if A[0] != B[0]:
    print(-1)
    exit()

AA = [0]+[A[i]^A[i+1] for i in range(N-1)]
BB = [0]+[B[i]^B[i+1] for i in range(N-1)]
dic_AA = defaultdict(list)
dic_BB = defaultdict(list)
for i in range(1,N):
    aa,bb = AA[i],BB[i]
    dic_AA[aa].append(i)
    dic_BB[bb].append(i)

P = [0]*(N-1)
for k in dic_AA.keys():
    if len(dic_AA[k]) != len(dic_BB[k]):
        print(-1)
        exit()
    for i,j in zip(dic_AA[k],dic_BB[k]):
        P[i-1] = j


def inversion(N,A):  # 1~Nの順列Aの転倒数を求める
    tree = [0]*(N+1)
    res = N*(N-1)//2
    for i in range(N):
        a = A[i]

        x = A[i]-1
        while x:
            res -= tree[x]
            x -= x & (-x)
        y = a
        while y <= N:
            tree[y] += 1
            y += y & (-y)

    return res


ans = inversion(N-1,P)
print(ans)
0