您的位置:首页 > 其它

codeforces 665E E. Beautiful Subarrays(trie树)

2016-05-03 23:59 393 查看
题目链接:E. Beautiful Subarraystime limit per test
3 secondsmemory limit per test
512 megabytesinput
standard inputoutput
standard outputOne day, ZS the Coder wrote down an array of integers a with elements a1,  a2,  ...,  an.A subarray of the array a is a sequence al,  al  +  1,  ...,  ar for some integers (l,  r) such that 1  ≤  l  ≤  r  ≤  n. ZS the Coder thinks that a subarray of a is beautiful if the bitwise xor of all the elements in the subarray is at least k.Help ZS the Coder find the number of beautiful subarrays of a!InputThe first line contains two integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 109) — the number of elements in the array a and the value of the parameter k.The second line contains n integers ai (0 ≤ ai ≤ 109) — the elements of the array a.OutputPrint the only integer c — the number of beautiful subarrays of the array a.Examplesinput
3 1
1 2 3
output
5
input
3 2
1 2 3
output
3
input
3 3
1 2 3
output
2

题意:

给n个数,问有多少对数满足a[i]^a[i+1]^...^a[j]>=k;

思路:

处理出前缀异或和,再用trie树搞;

AC代码
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(b));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e7+6;
int n,k,cnt=2;
int tree
[2],val
;
void add_num(int num)
{
int x=1;
for(int i=30;i>=0;i--)
{
int y=(num>>i)&1;
if(!tree[x][y])tree[x][y]=cnt++;
val[x]++;
x=tree[x][y];
}
val[x]++;
}
LL get_ans(int num,int fy)
{
LL sum=0;
int x=1;
for(int i=30;i>=0;i--)
{
int y=(fy>>i)&1;
int fx=(num>>i)&1^1;
if(y==0)
{
sum+=(LL)val[tree[x][fx]];
x=tree[x][fx^1];
}
else x=tree[x][fx];
}
return sum+(LL)val[x];
}
int main()
{
scanf("%d%d",&n,&k);
int pre=0;
LL ans=0;
add_num(0);
Riep(n)
{
int a;
scanf("%d",&a);
pre^=a;
ans+=(LL)get_ans(pre,k);
add_num(pre);
}
printf("%I64d\n",ans);

return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: