题目链接
SUBXOR - SubXor
A straightforward question. Given an array of positive integers you have to print the number of subarrays whose XOR is less than K.
Subarrays are defined as a sequence of continuous elements Ai, Ai+1, ..., Aj . XOR of a subarray is defined as Ai^Ai+1^ ... ^Aj.
Symbol ^ is Exclusive Or. You can read more about it here:
http://en.wikipedia.org/wiki/Exclusive_or
Input Format:
First line contains T, the number of test cases. Each of the test case consists of N and K in one line, followed by N space separated integers in next line.
Output Format:
For each test case, print the required answer.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
1 ≤ A[i] ≤ 10^5
1 ≤ K ≤ 10^6
Sum of N over all testcases will not exceed 10^5.
Sample Input:
1
5 2
4 1 3 2 7
Sample Output:
3
Explanation:
Only subarrays satisfying the conditions are [1],[1,3,2] and [3,2].
Problem Setter: Lalit Kundu
题意:
T个样例,给出n,k,然后是n个数,求有多少个子序列的异或和小于k。
题解:
这题可以建一个字典树,先处理异或前缀和,然后从第一个前缀和开始根据k在找字典树上异或和小于k的个数,然后将此前缀和插入字典树,并更新字典树上该结点的数的个数。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1e5+100;
int b[maxn];
int t[20*maxn][3],f[maxn*20];
int cnt;
void insert(int x)
{
int now=0;
per(i,0,24)
{
if(x&(1<<i))
{
if(!t[now][1])
t[now][1]=++cnt;
now=t[now][1];
f[now]++;
}
else
{
if(!t[now][0])
t[now][0]=++cnt;
now=t[now][0];
f[now]++;
}
}
}
int k;
ll find(int i,int x,int now)
{
ll ans=0;
int p=k&(1<<i),q=x&(1<<i);
if(i==0)
{
if(p)
{
if(q)
return t[now][1]? f[t[now][1]]:0;
else return t[now][0]? f[t[now][0]]:0;
}
else
return 0;
}
if(p)
{
if(q)
{
ans+=t[now][1]? f[t[now][1]]:0;
ans+=t[now][0]? find(i-1,x,t[now][0]):0;
}
else
{
ans+=t[now][0]? f[t[now][0]]:0;
ans+=t[now][1]? find(i-1,x,t[now][1]):0;
}
}
else
{
if(q)
{
ans+=t[now][1]? find(i-1,x,t[now][1]):0;
}
else
{
ans+=t[now][0]? find(i-1,x,t[now][0]):0;
}
}
return ans;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int n;
scanf("%d%d",&n,&k);
int v=0;
memset(b,0,sizeof(b));
rep(i,1,n+1) scanf("%d",&v),b[i]=b[i-1]^v;
cnt=0;
memset(f,0,sizeof(f));
memset(t,0,sizeof(t));
ll ans=0;
//rep(i,1,n+1) printf("%d ",b[i]);
//insert(b[1]);
insert(0);
rep(i,1,n+1)
{
ans+=find(23,b[i],0);
//if(b[i]<k) ans++;
//cout << ans <<" ";
insert(b[i]);
}
printf("%lld\n",ans);
}
return 0;
}