結果

問題 No.1209 XOR Into You
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-11 23:03:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 1,528 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 86,764 KB
実行使用メモリ 122,176 KB
最終ジャッジ日時 2023-09-25 12:52:10
合計ジャッジ時間 14,328 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,732 KB
testcase_01 AC 91 ms
71,692 KB
testcase_02 AC 93 ms
71,744 KB
testcase_03 AC 97 ms
71,644 KB
testcase_04 AC 153 ms
106,736 KB
testcase_05 AC 151 ms
106,784 KB
testcase_06 AC 191 ms
116,172 KB
testcase_07 AC 268 ms
117,096 KB
testcase_08 AC 237 ms
113,896 KB
testcase_09 AC 242 ms
113,712 KB
testcase_10 AC 209 ms
110,448 KB
testcase_11 AC 227 ms
114,144 KB
testcase_12 AC 217 ms
111,580 KB
testcase_13 AC 232 ms
113,536 KB
testcase_14 AC 217 ms
112,688 KB
testcase_15 AC 220 ms
111,296 KB
testcase_16 AC 210 ms
111,944 KB
testcase_17 AC 211 ms
112,344 KB
testcase_18 AC 268 ms
122,176 KB
testcase_19 AC 239 ms
111,600 KB
testcase_20 AC 232 ms
112,884 KB
testcase_21 AC 208 ms
111,280 KB
testcase_22 AC 212 ms
116,296 KB
testcase_23 AC 213 ms
116,424 KB
testcase_24 AC 215 ms
116,568 KB
testcase_25 AC 230 ms
116,472 KB
testcase_26 AC 233 ms
116,208 KB
testcase_27 AC 232 ms
116,184 KB
testcase_28 AC 232 ms
116,292 KB
testcase_29 AC 236 ms
116,472 KB
testcase_30 AC 231 ms
116,232 KB
testcase_31 AC 197 ms
116,288 KB
testcase_32 AC 199 ms
116,332 KB
testcase_33 AC 201 ms
116,460 KB
testcase_34 AC 200 ms
116,848 KB
testcase_35 AC 201 ms
116,296 KB
testcase_36 AC 201 ms
116,196 KB
testcase_37 AC 192 ms
116,780 KB
testcase_38 AC 210 ms
116,912 KB
testcase_39 AC 195 ms
113,960 KB
testcase_40 AC 216 ms
116,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class fenwick_tree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i


def main():
    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]:
        return -1
    A = [a ^ b for a, b in zip(A[:-1], A[1:])]
    B = [a ^ b for a, b in zip(B[:-1], B[1:])]
    btoi = defaultdict(list)
    for i, b in enumerate(B):
        btoi[b].append(i)

    ans = 0
    bit = fenwick_tree(len(A))
    for a in reversed(A):
        if not btoi[a]:
            return - 1
        i = btoi[a].pop()
        ans += bit.sum(0, i)
        bit.add(i, 1)
    return ans


print(main())
0