Beautiful Subarrays
3 seconds
512 megabytes
standard input
standard output
One 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!
The 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.
Print the only integer c — the number of beautiful subarrays of the array a.
3 1
1 2 3
5
3 2
1 2 3
3
3 3
1 2 3
2
分析:trie树,保留每个前缀再异或即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e7+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,ch[maxn][],sz[maxn],tot;
ll ans;
void insert(int p)
{
int now=;
for(int i=;i>=;i--)
{
int q=(p>>i)&;
if(!ch[now][q])ch[now][q]=++tot;
now=ch[now][q],sz[now]++;
}
}
ll get(int p)
{
int now=;
ll ans=;
for(int i=;i>=;i--)
{
int q=(p>>i)&^,t=(m>>i)&;
if(!t)ans+=sz[ch[now][q]],now=ch[now][q^];
else now=ch[now][q];
if(!now)return ans;
}
return ans+sz[now];
}
int main()
{
int i,j;
insert();
scanf("%d%d",&n,&m);
while(n--)
{
scanf("%d",&j),k^=j;
ans+=get(k);
insert(k);
}
printf("%lld\n",ans);
//system("Pause");
return ;
}
最新文章
- selenium-----chromedriver与chrome版本映射
- 移动端设置字体px转换rem的脚本
- Control Flow
- form作为module name 悲剧了
- Cookie和Session专题
- MongoDB安装及简单实验
- nodejs 第一次使用
- Windows 7下可以使用的各个命令语句+C#打开
- BZOJ 1022
- 文件比较,文件夹比较-- vimdiff,beyond compare, compare suite, WinMerge,Kdiff3
- .net core nlog记录日志
- eclipse is missing required source folder src/test/java
- EFM32G232F64时钟树
- Python语言中的按位运算
- 强化学习3-蒙特卡罗MC
- 服务器搭建--Linux安装erlang
- zuul网关源码解析
- OI中的一些模板
- css3怎么分清伪类和伪元素
- 2017-2018-1 20155230 《信息安全技术》实验二——Windows口令破解