結果

問題 No.1209 XOR Into You
ユーザー MisterMister
提出日時 2020-08-30 16:50:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,141 bytes
コンパイル時間 1,199 ms
コンパイル使用メモリ 112,924 KB
実行使用メモリ 23,680 KB
最終ジャッジ日時 2024-04-27 10:19:04
合計ジャッジ時間 10,412 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 22 ms
6,940 KB
testcase_05 AC 22 ms
6,944 KB
testcase_06 AC 123 ms
14,976 KB
testcase_07 AC 169 ms
20,096 KB
testcase_08 AC 155 ms
18,816 KB
testcase_09 AC 156 ms
18,816 KB
testcase_10 AC 119 ms
15,360 KB
testcase_11 RE -
testcase_12 AC 137 ms
17,792 KB
testcase_13 AC 161 ms
19,584 KB
testcase_14 AC 133 ms
17,152 KB
testcase_15 AC 139 ms
18,048 KB
testcase_16 AC 135 ms
17,792 KB
testcase_17 AC 131 ms
17,408 KB
testcase_18 RE -
testcase_19 AC 139 ms
17,792 KB
testcase_20 RE -
testcase_21 AC 120 ms
15,104 KB
testcase_22 AC 156 ms
23,296 KB
testcase_23 AC 159 ms
23,296 KB
testcase_24 AC 153 ms
23,296 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 122 ms
13,952 KB
testcase_32 AC 112 ms
13,952 KB
testcase_33 AC 112 ms
13,952 KB
testcase_34 AC 119 ms
13,952 KB
testcase_35 AC 116 ms
13,952 KB
testcase_36 AC 115 ms
13,952 KB
testcase_37 AC 77 ms
14,080 KB
testcase_38 AC 162 ms
20,224 KB
testcase_39 AC 138 ms
18,432 KB
testcase_40 AC 150 ms
23,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <functional>
#include <cassert>

template <class T>
struct SegmentTree {
    using Merger = std::function<T(T, T)>;

    int length;
    std::vector<T> dat;
    T unit;
    Merger merge;

    SegmentTree() = default;

    SegmentTree(int n, const T& unit, const Merger& merge)
        : length(1), unit(unit), merge(merge) {
        while (length < n) length <<= 1;
        dat.assign(length * 2, unit);
    }

    template <class Container>
    SegmentTree(const Container& elems, const T& unit, const Merger& merge)
        : length(1), unit(unit), merge(merge) {
        int n = elems.size();
        while (length < n) length <<= 1;

        dat.assign(length * 2, unit);

        std::copy(elems.begin(), elems.end(), dat.begin() + length);

        for (int nidx = length - 1; nidx >= 1; --nidx) {
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = merge(vl, vr);
        }
    }

    void update(int nidx, const T& elem) {
        nidx += length;
        dat[nidx] = elem;

        while (nidx > 0) {
            nidx >>= 1;
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = merge(vl, vr);
        }
    }

    T fold(int ql, int qr) const {
        ql = std::max(ql, 0);
        qr = std::min(qr, length);
        ql += length, qr += length;

        T lacc = unit, racc = unit;
        while (ql < qr) {
            if (ql & 1) {
                lacc = merge(lacc, dat[ql]);
                ++ql;
            }
            if (qr & 1) {
                --qr;
                racc = merge(dat[qr], racc);
            }
            ql >>= 1, qr >>= 1;
        }

        return merge(lacc, racc);
    }

    T get(int idx) const { return dat[idx + length]; }
    T fold_all() const { return dat[1]; }
};

using lint = long long;

void solve() {
    int n;
    std::cin >> n;

    std::vector<lint> xs(n), ys(n);
    for (auto& x : xs) std::cin >> x;
    for (auto& y : ys) std::cin >> y;

    if (xs[0] != ys[0]) {
        std::cout << "-1\n";
        return;
    }

    std::vector<int> xds(n - 1), yds(n - 1);
    std::map<int, int> xcnt, ycnt;
    for (int i = 0; i < n - 1; ++i) {
        lint d = xs[i + 1] ^ xs[i];
        if (!xcnt.count(d)) xcnt[d] = 0;
        xds[i] = d * n + (xcnt[d]++);
    }

    for (int i = 0; i < n - 1; ++i) {
        lint d = ys[i + 1] ^ ys[i];
        if (!ycnt.count(d)) ycnt[d] = 0;
        yds[i] = d * n + (ycnt[d]++);
    }

    if (xcnt != ycnt) {
        std::cout << "-1\n";
        return;
    }

    std::map<lint, int> yrev;
    for (int i = 0; i < n - 1; ++i) yrev[yds[i]] = i;
    for (auto& x : xds) x = yrev[x];

    SegmentTree<lint>
        seg(n - 1, 0, [](auto a, auto b) { return a + b; });

    lint ans = 0;
    for (auto x : xds) {
        assert(0 <= x && x < n - 1);
        ans += seg.fold(x, n - 1);

        assert(seg.get(x) == 0);
        seg.update(x, 1);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0