题解连接:http://blog.youkuaiyun.com/lxy767087094/article/details/77623448
倒是能想得起来分治做,但是不会分治。题解中讲得挺好。膜。。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100100;
const int mod = 1e9+7;
int Num[MAXN],And[MAXN],Or[MAXN],Cnt[MAXN];
LL res;
void solve(int l, int r)
{
if(l > r) return;
if(l == r)
{
res = (res+Num[l]*(LL)Num[l])%mod;
return;
}
int mid = (l+r) >> 1;
int pos = mid+1;
And[pos] = Or[pos] = Num[mid+1];
Cnt[pos] = 0;
for(int i = mid+1; i <= r; ++i)
{
if(((And[pos]&Num[i]) != And[pos]) || ((Or[pos]|Num[i]) != Or[pos]))
{
And[++pos] = And[pos-1]&Num[i];
Or[pos] = Or[pos-1]|Num[i];
Cnt[pos] = 1;
}
else
Cnt[pos]++;
}
int tmp_or,tmp_and;
tmp_or = tmp_and = Num[mid];
for(int i = mid; i >= l; --i)
{
tmp_or |= Num[i];
tmp_and &= Num[i];
for(int j = mid+1; j <= pos; ++j)
res = ((res + (LL)(tmp_and & And[j]) * (LL)(tmp_or | Or[j]) % mod) * Cnt[j] % mod) % mod;
}
solve(l,mid);
solve(mid+1,r);
}
int main()
{
int n;
scanf("%d",&n);
for(int i = 1; i <= n; ++i)
scanf("%d",&Num[i]);
solve(1,n);
printf("%I64d\n",res);
return 0;
}