結果
| 問題 | No.1209 XOR Into You | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-08-30 16:57:06 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 415 ms / 2,000 ms | 
| コード長 | 3,111 bytes | 
| コンパイル時間 | 2,367 ms | 
| コンパイル使用メモリ | 109,144 KB | 
| 最終ジャッジ日時 | 2025-01-14 00:20:34 | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 37 | 
ソースコード
#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<lint> 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;
    assert((int)yrev.size() == n - 1);
    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) {
        ans += seg.fold(x, n - 1);
        seg.update(x, 1);
    }
    std::cout << ans << "\n";
}
int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    solve();
    return 0;
}
            
            
            
        