跳到正文

剪贴板

剪贴板 zf0ov9fj

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define u unsigned
#define ll long long
using namespace std;
u read()
{
	char ch=getchar();
	u s=0;
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s;
}
const int w=32;
const int mxn=1e5+5;
u rem[mxn*4],*top=rem;
struct CNT_2_A
{
	u *a;
	int len;
	friend CNT_2_A operator +(CNT_2_A x,CNT_2_A y)
	{
		if(x.len<y.len) swap(x,y);
		CNT_2_A t;
		u p=0;
		int i;
		t.a=top,top+=t.len=x.len;
		for(i=0;i<y.len;i++)
			t.a[i]=x.a[i]^y.a[i]^p,
			p=(x.a[i]&y.a[i])|((x.a[i]^y.a[i])&p);
		for(;i<x.len;i++)
			t.a[i]=x.a[i]^p,p=x.a[i]&p;
		if(p) t.a[t.len++]=p,top++;
		return t;
	}
	ll getans(int n)
	{
		ll sum=0;
		int i,j,s[w]={0};
		for(i=0;i<len;i++)
			for(j=0;j<w;j++)
				s[j]+=((a[i]>>j)&1)*(1<<i);
		for(i=0;i<w;i++)
			sum+=(1ll<<i)*s[i]*(n-s[i]+1);
		return sum;
	}
};
u a[100005];
CNT_2_A solve(int l,int r)
{
	if(l==r)
	{
		CNT_2_A t;
		(t.a=top++)[0]=a[l],t.len=1;
		return t;
	}
	int mid=l+r>>1;
	return solve(l,mid)+solve(mid+1,r);
}
int main()
{
	int n=read(),i;
	for(i=1;i<=n;i++)
		a[i]=read()^a[i-1];
	cout<<solve(1,n).getans(n);
}