CodeForces 665E Beautiful Subarrays
2016-04-23 00:03
183 查看
题意:问你有多少个区间,异或起来大于等于k
思路:显然求个前缀和之后,就等于有多少对数异或起来大于等于k了,每次暴力爬字典树就好了,当k这一位等于0的时候,我们可以直接加上另外一边1的子树大小,因为爬那边之后,我怎么爬都是大于等于k的
Description
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!
Input
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.
Output
Print the only integer c — the number of beautiful subarrays of the array a.
Sample Input
Input
Output
Input
Output
Input
Output
思路:显然求个前缀和之后,就等于有多少对数异或起来大于等于k了,每次暴力爬字典树就好了,当k这一位等于0的时候,我们可以直接加上另外一边1的子树大小,因为爬那边之后,我怎么爬都是大于等于k的
#include<bits/stdc++.h> using namespace std; const int maxn = 2e7+6; struct Tri { int ch[maxn][2]; int sz[maxn]; int tot; void init() { memset(ch,0,sizeof(ch)); memset(sz,0,sizeof(sz)); tot=2; } void insert(int x) { int u=1; for(int i=30;i>=0;i--) { int p = (x>>i)&1; if(!ch[u][p])ch[u][p]=tot++; sz[u]++; u=ch[u][p]; } sz[u]++; } int get(int x,int y) { int u=1; long long ans = 0; for(int i=30;i>=0;i--) { int p = (x>>i)&1^1; int q = (y>>i)&1; if(q==0)ans+=sz[ch[u][p]],u=ch[u][p^1]; else u=ch[u][p]; } return ans+sz[u]; } }T; int main() { T.init(); int n,k; scanf("%d%d",&n,&k); int pre = 0; long long ans = 0; T.insert(0); for(int i=1;i<=n;i++) { int x;scanf("%d",&x); pre^=x; ans+=T.get(pre,k); T.insert(pre); } cout<<ans<<endl; }
Description
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!
Input
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.
Output
Print the only integer c — the number of beautiful subarrays of the array a.
Sample Input
Input
3 1 1 2 3
Output
5
Input
3 2 1 2 3
Output
3
Input
3 31 2 3
Output
2
相关文章推荐
- c++的重载、覆盖和隐藏
- 文章标题
- Privacy Policy of ColorfulBroswer
- Timer的用法
- 新时代编辑神器:Atom
- 使用spring框架中的组件发送邮件功能说明
- 一、Mycat 环境搭建
- ASP.NET MVC的Localization本地化多语言支持
- ASP.NET中MultiView和View选项卡控件的使用方法
- ASP.NET中 Panel 控件的使用方法
- ASP.NET中 PlaceHolder 控件的使用方法
- ASP.NET获取URL方法汇总
- VS2013设置护眼背景颜色
- IIS7伪静态web.config配置的方法和规则
- Linux 发邮件磁盘空间监控(python)
- 一键搞定python连接mysql驱动有关问题(windows版本)
- Python 探针的实现原理
- Python 中的 else详解
- Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法
- 简单谈谈PHP中的include、include_once、require以及require_once语句