题目
展开
题目描述
One day, ZS the Coder wrote down an array of integers aa with elements a_{1},a_{2},…,a_{n}a
1
,a
2
,…,a
n
.
A subarray of the array aa is a sequence a_{l},a_{l+1},…,a_{r}a
l
,a
l+1
,…,a
r
for some integers (l,r)(l,r) such that 1<=l<=r<=n1<=l<=r<=n . ZS the Coder thinks that a subarray of aa is beautiful if the bitwise xor of all the elements in the subarray is at least kk .
Help ZS the Coder find the number of beautiful subarrays of aa !
输入格式
The first line contains two integers nn and kk ( 1<=n<=10{6},1<=k<=10{9}1<=n<=10
6
,1<=k<=10
9
) — the number of elements in the array aa and the value of the parameter kk .
The second line contains nn integers a_{i}a
i
( 0<=a_{i}<=10^{9}0<=a
i
<=10
9
) — the elements of the array aa .
输出格式
Print the only integer cc — the number of beautiful subarrays of the array aa .
题意翻译
一天,某编程爱好者写下了含有元素 a1, a2, …, an 的一个整数数组 a。
数组 a 的一个子数组,是指以某些整数 (l, r) 为下标的一个序列 al, al + 1, …, ar 满足 1 ≤ l ≤ r ≤ n 。这位编程爱好者认为,如果 a 的一个子数组中全部元素的位异或至少是 k ,那么这个子数组就是漂亮的。
请帮助这位编程爱好者,找出数组 a 有多少个漂亮子数组。
第一行包含两个整数 n 和 k (1 ≤ n ≤ 106, 1 ≤ k ≤ 109) — 数组 a 中的元素数目,以及参数 k 的值。
第二行包含了 n 个整数 ai (0 ≤ ai ≤ 109) — 数组 a 的元素。
输入输出样例
输入 #1复制
3 1
1 2 3
输出 #1复制
5
输入 #2复制
3 2
1 2 3
输出 #2复制
3
输入 #3复制
3 3
1 2 3
输出 #3复制
2
思路
我们知道,一个数异或其本身为0,而任何数异或0还是自身,
就可以用异或前缀和s[0…n]来表示区间异或和了。
有了异或前缀和,我们对于1~n中的每个i,
求以第i的数结尾的区间异或和大于等于k的有多少,
即s[0…i−1]中有多少个与s[i]异或大于等于k。
对s[0…i−1]建一棵trie树,
由高位到低位考虑每位,
如果到该位异或的结果已经大于k,就累计,
如果小于k,直接无视,
到该位不能确定,接着往下一层走
代码
#include<bits/stdc++.h>
using namespace std;
const int X=2147483647,N=1e6+77,L=30;
int n,k;
int t[30*N][2],cnt;
int a[30*N];
int work(int s)
{
int v,sum=0,p=k,q=s,i;
v=1;
for(i=0;i<=L;i++){
if(!v)break;
if(p>>L){
v=t[v][1^(q>>L)];
}
else{
sum+=a[t[v][1^(q>>L)]];
v=t[v][q>>L];
}
p=(p<<1)&X;
q=(q<<1)&X;
}
v=1;
++a[v];
for(i=0;i<=L;i++){
if(t[v][s>>L])v=t[v][s>>L];
else ++cnt,t[v][s>>L]=cnt,v=cnt;
s=(s<<1)&X;
++a[v];
}
return sum;
}
int main()
{
int i,p,s=0;
scanf("%d%d",&n,&k);
cnt=1;
k=k-1;
long long ans=work(s);
for(i=1;i<=n;i++){
scanf("%d",&p);
s=s^p;
ans+=work(s);
}
printf("%lld",ans);
return 0;
}