zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Swift-基础语法之变量&常量&元组
    Swift
    安装MySQL
    LNMP 简介
    LNMP
    Django 定义数据模型
    Django 添加应用
    Django 创建第一个项目
    Django 安装
    Django 简介
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12445645.html
Copyright © 2011-2022 走看看