結果

問題 No.1209 XOR Into You
コンテスト
ユーザー lucas lwi
提出日時 2026-01-11 16:20:24
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 2,123 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,540 ms
コンパイル使用メモリ 345,180 KB
実行使用メモリ 24,676 KB
最終ジャッジ日時 2026-01-11 16:20:37
合計ジャッジ時間 12,464 ms
ジャッジサーバーID
(参考情報)
judge2 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 5e5 + 10;
const int inf = 1e18;
int n,a[N],b[N],c[N];
map<int,vector<int> > mp;

template <typename T>
struct Fenwick {
    int n;
    vector<T> a,p;
    
    Fenwick(int n_ = 0) {
        init(n_);
    }
    
    void init(int n_) {
        n = n_;
        a.assign(n + 1, T{});
        p.assign(n + 1, T{});
    }
    
    void add(int x, const T &v) {
        p[x] = p[x] + v;
        for (int i = x; i <= n; i += i & -i) {
            a[i] = a[i] + v;
        }
    }
    
    T sum(int x) {
        T ans{};
        for (int i = x; i > 0; i -= i & -i) {
            ans = ans + a[i];
        }
        return ans;
    }
    
    T query(int l, int r) {
        T res{};
        while(l <= r) {
            if(r - (r & (-r)) + 1 >= l) {
                res = res + a[r];
                r -= r & (-r);
            }
            else {
                res = res + p[r];
                r--;
            }
        }
        return res;
    }

    int select(const T &k) {
        int x = 0;
        T cur{};
        for (int i = 1 << std::__lg(n); i; i /= 2) {
            if (x + i <= n && cur + a[x + i] <= k) {
                x += i;
                cur = cur + a[x];
            }
        }
        return x;
    }
};

void solve() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    for(int i = 1;i <= n;i++) cin >> b[i];
    for(int i = 1;i <= n;i++) c[i] = a[i] ^ a[i - 1];
    for(int i = n;i >= 1;i--) b[i] = b[i] ^ b[i - 1];
    for(int i = n;i >= 1;i--) mp[b[i]].push_back(i);
    for(int i = 1;i <= n;i++) {
        if(mp[c[i]].empty()) return cout << -1 << '\n',void();
        int tmp = c[i];
        c[i] = mp[c[i]].back();
        mp[tmp].pop_back();
        // cout << c[i] << ' ';
    }
    // cout << '\n';
    Fenwick<int> f(n + 10);
    int ans = 0;
    for(int i = n;i >= 1;i--) {
        ans += f.sum(c[i]);
        f.add(c[i],1);
    }
    cout << ans << '\n';
}

signed main() {
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
    int t = 1;
    while(t--) solve();
    
    return 0;
}
0