跳到正文

个人记录

1

RoHe约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年10月17日
收录
2026年10月17日
更新
2026年10月17日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e6+6;
int b[N],cnt[35];
bool a[N][35];
void check(int le,int ri,int w) {
	int W=(1<<w);
	if(le>ri||ri-le+1<W) return;
	vector<int> num(W,0);
	int l=le,y=0;
	for(int r=le; r<=ri; r++) {
		if(b[r]>W) continue;
		if(!num[b[r]]) y++;
		num[b[r]]++;
		if(y==W) {
			while(b[l]>W||num[b[l]]>1) {
				if(b[l]<W) num[b[l]]--;
				l++;
			}
			cnt[w]+=l-le+1;
		}
	}
}
signed main() {
	int n;
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>b[i];
		int r=-1,num=b[i];
		while(num) {
			a[i][++r]=num&1;
			num>>=1;
		}
	}
	for(int i=0; i<30; i++) {
		int s=0;
		cnt[i]=(n+1)*n/2;
		for(int j=1; j<=n; j++)
			if(a[j][i]) {
				cnt[i]-=s*(s+1)/2;
				check(j-s,j-1,i);
				s=0;
			} else s++;
		cnt[i]-=s*(s+1)/2;
	}
	unsigned long long ans=0;
	for(int i=0; i<30; i++)
		ans+=cnt[i]*(1<<i);
	cout<<ans;
	return 0;
}
原文