結果

問題 No.1209 XOR Into You
ユーザー shotoyooshotoyoo
提出日時 2021-05-17 23:48:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 2,634 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 120,704 KB
最終ジャッジ日時 2024-04-16 11:24:39
合計ジャッジ時間 9,197 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,736 KB
testcase_01 AC 41 ms
52,480 KB
testcase_02 AC 41 ms
53,120 KB
testcase_03 AC 40 ms
52,864 KB
testcase_04 AC 120 ms
96,896 KB
testcase_05 AC 122 ms
97,096 KB
testcase_06 AC 127 ms
97,152 KB
testcase_07 AC 244 ms
111,324 KB
testcase_08 AC 208 ms
100,480 KB
testcase_09 AC 207 ms
106,924 KB
testcase_10 AC 180 ms
96,580 KB
testcase_11 AC 208 ms
100,096 KB
testcase_12 AC 195 ms
97,408 KB
testcase_13 AC 223 ms
106,752 KB
testcase_14 AC 196 ms
97,792 KB
testcase_15 AC 201 ms
97,792 KB
testcase_16 AC 197 ms
97,792 KB
testcase_17 AC 194 ms
98,176 KB
testcase_18 AC 263 ms
118,784 KB
testcase_19 AC 198 ms
97,508 KB
testcase_20 AC 221 ms
106,752 KB
testcase_21 AC 172 ms
96,016 KB
testcase_22 AC 172 ms
119,544 KB
testcase_23 AC 172 ms
119,296 KB
testcase_24 AC 173 ms
119,296 KB
testcase_25 AC 238 ms
120,704 KB
testcase_26 AC 239 ms
120,680 KB
testcase_27 AC 235 ms
120,576 KB
testcase_28 AC 239 ms
120,548 KB
testcase_29 AC 236 ms
120,576 KB
testcase_30 AC 238 ms
120,704 KB
testcase_31 AC 179 ms
99,072 KB
testcase_32 AC 180 ms
99,328 KB
testcase_33 AC 180 ms
99,072 KB
testcase_34 AC 180 ms
100,352 KB
testcase_35 AC 176 ms
99,248 KB
testcase_36 AC 176 ms
99,328 KB
testcase_37 AC 142 ms
104,832 KB
testcase_38 AC 202 ms
111,104 KB
testcase_39 AC 181 ms
99,712 KB
testcase_40 AC 176 ms
119,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

class BIT:
    ### BIT binary
    def __init__(self, n, values=None):
        self.bit = [0]*(n+1)
        self.n = n
        self.total = 0
        if values is not None:
            for i,v in enumerate(values):
                self.add(i,v)
                self.total += v
    def check(self):
        l = []
        prv = 0
        for i in range(1,self.n+1):
            val = self.query(i)
            l.append(val-prv)
            prv = val
        print(" ".join(map(str, l)))
    #a1 ~ aiまでの和 O(logn)
    def query(self,i):
        res = 0
        while i > 0:
            res += self.bit[i]
            # res %= M
            i -= i&(-i)
        return res
    def get(self,i):
        return self.query(i+1) - self.query(i)
    #ai += x(logN)
    def add(self,i,x):
        i += 1
        if i==0:
            raise RuntimeError
        self.total += x
        while i <= self.n:
            self.bit[i] += x
            # self.bit[i] %= M
            i += i&(-i)
    def index(self, v):
        """a0,...,aiの和がv以上になる最小のindexを求める
        存在しないとき配列サイズを返す
        """
        if v <= 0:
            return 0
        if self.total<v:
            return self.n
        x = 0
        r = 1
        while r < self.n:
            r = r << 1;
        ll = r
        while ll>0:
            if x+ll<self.n and self.bit[x+ll]<v:
                v -= self.bit[x+ll]
                x += ll
            ll = ll>>1
        return x
# 転倒数を求める例: 圧縮なし
from bisect import bisect_left
def press(l):
    # xs[inds[i]]==l[i]となる
    xs = sorted(set(l))
    inds = [None] * len(l)
#     d = {}
    for i,item in enumerate(l):
        inds[i] = bisect_left(xs, item)
#         d[item] = inds[i]
    return xs, inds

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = [a[i]^a[i+1] for i in range(n-1)]
bb = [b[i]^b[i+1] for i in range(n-1)]
if sorted(aa)!=sorted(bb) or a[0]!=b[0] or a[-1]!=b[-1]:
    ans = -1
else:
    index = {}
    for i in range(n-1):
        index.setdefault(bb[i], [])
        index[bb[i]].append(i)
    for k,v in index.items():
        index[k] = v[::-1]
    bit = BIT(n-1)
#     na = [index[aa[i]] for i in range(n-1)]
    ans = 0
    for i in range(n-1):
        v = index[aa[i]].pop()
        ans += i - bit.query(v+1)
        bit.add(v, 1)
print(ans)
0