http://acm.tju.edu.cn/toj/showp4114.html
好感动~
终于过了
要使得(sum(r)−sum(l−1))mod2k+1≥2k
其实就是两种情况
sum(l−1)mod2k+1≤(sum(r)mod2k+1)−2k
且
0≤sum(l−1)mod2k+1
的,也可能是满足
sum(l−1)mod2k+1≤(sum(r)mod2k+1)−2k+2k=(sum(r)mod2k+1)+2k
且
sum(r)mod2k+1<sum(l−1)mod2k+1
时间复杂度O(nlognlog∑
k = 3
010 -------> 110
0 ------------->10 110 --------------->210(1010)
时间复杂度 O(nlognlog∑A)
#include <functional>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include<cstdlib>
#include<math.h>
#include<iostream>
#include<limits.h>
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define maxn 1000000
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
int a[maxn+5];
int tree[2*maxn+5];
int n;
inline int low(int x)
{
return x&(-x);
}
void update(int x)
{
for(x=x*2+1; x <= 2*maxn; x+=low(x))
tree[x]++;
}
int query(int x)
{
int res = 0;
for(x=x*2+1; x > 0; x -= low(x))//因为插入0的时候会死循环,我直接开了两倍空间,好浪费~~~
res += tree[x];
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int ans = 0;
scanf("%d",&n);
a[0] = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d",a+i);
a[i] += a[i-1];
}
<span style="white-space:pre"> </span>
for(ll k = 0; 1<<k <= a[n]; k++)
{
clr(tree,0);
int tmp = 0;
for(int i = 1; i <= n; i++)
{
int w = a[i]&((1<<(k+1)) - 1);
int f = 0;
if(w&(1<<k)) f = 1;
ll last = w + (1<<k);
if(last > maxn)
last = maxn;
tmp += f + query(w-(1<<k))+query(last)-query(w);
update(w);
}
if(tmp&1)
ans |= 1<<k;
}
printf("%d\n",ans);
}
}