Codeforces 1323D Present

题目链接

每个\(a_i\)都会与他后面的相加再异或, 每对数只加一次, 用树状数组可以优化,保证每一对只加一次, 根据异或的特点,我们可以按照2进制位数进行计算,分别计算每一位的情况即可

#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;
}

猜你喜欢

转载自www.cnblogs.com/GRedComeT/p/12445645.html
今日推荐