Codeforces 1323D Present

Topic Link

Each \ (a_i \) will be added back again with his XOR, plus the number of each pair only once, can be optimized with Fenwick tree to ensure that each pair added only once, or according to different characteristics, we can follow the 2 hexadecimal digits calculated separately calculate every situation can be

#include<bits/stdc++.h>
using namespace std;
#define ms(x,y) memset(x, y, sizeof(x))
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;

const int maxn = 1e7+7;
int C[maxn];

void add(int pos, int val) {
    for(; pos; pos -= lowbit(pos))
        C[pos] += val;
}

int getsum(int pos) {
    if(pos < 1) return 1;
    int ret = 0;
    for(; pos < maxn; pos += lowbit(pos))
        ret += C[pos];
    return ret;
}


void run_case() {
    int n; cin >> n;
    vector<int> a(n);
    for(auto &x: a) cin >> x;
    int ans = 0;
    for(int i = 28; i >= 0; --i) {
        int cnt = 0;
        vector<int> v0, v1;
        for(int x: a) {
            if(x>>i&1)
                v1.push_back(x & ((2<<i)-1));
            else
                v0.push_back(x & ((2<<i)-1));
        }
        for(int x: v1) add(x, 1);
        for(int x: v0) {
            int now = getsum((2<<i) - x) - getsum((1<<i) - x);
            if(now & 1) cnt ^= 1;
            add(x, 1);
        }
        for(int x: v1) add(x, -1);
        for(int x: v0) add(x, -1);
        for(int &x: v1) {
            x &= ((1<<i)-1);
            if(getsum((1<<i)-x) & 1) cnt ^= 1;
            add(x, 1);
        }
        for(int x: v1) add(x, -1);
        if(cnt) ans ^= (1<<i);
    }
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(2);
    //int t; cin >> t;
    //while(t--)
    run_case();
    cout.flush();
    return 0;
}

Guess you like

Origin www.cnblogs.com/GRedComeT/p/12445645.html