求助
查看原帖
求助
766182
czyzh楼主2023/1/31 11:47

dalao教的代码,没明白,求分析

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
int n, s[N], c[2][N], ans = 0;

inline int lowbit(int x)
{
	return x & -x;
}

inline void update(int tag, int x)
{
	while(x < N)
	{
		c[tag][x]++;
		x += lowbit(x);
	}
}

inline int query(int tag, int x)
{
	int res = 0;
	while(x)
	{
		res += c[tag][x];
		x -= lowbit(x);
	}
	return res;
}

int main(){
    scanf("%d" , &n);
    for(int i = 1; i <= n; i++)
	{
    	scanf("%d" , s + i);
		s[i] += s[i - 1];
	}
    for(int i = 0; i <= 20; i++)
	{
        int lim = (1 << i) - 1, pos;
		ll tot = 0;
		
        if(lim > s[n]) break;
        
        memset(c, 0, sizeof(c));
        
        update(0, 1);
        
        for(int j = 1; j <= n; j++)
		{
            pos = (s[j] & lim) + 1;
            if(s[j] & (1 << i))
            {
            	tot += query(0, pos) + query(1, 1000001) - query(1, pos);
				update(1, pos);
			}
            else 
            {
            	tot+=query(1, pos) + query(0, 1000001) - query(0, pos);
				update(0, pos);
			} 
        }
        if(tot & 1) ans |= (1 << i);
    }
    cout << ans;
    return 0;
}
2023/1/31 11:47
加载中...