Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays
题目连接:
http://www.codeforces.com/contest/665/problem/E
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
3 1
1 2 3
Sample Output
5
Hint
题意
问你有多少个区间,异或起来大于等于k
题解:
显然求个前缀和之后,就等于有多少对数异或起来大于等于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;
}
Educational Codeforces Round 12 E. Beautiful Subarrays 字典树的更多相关文章
- Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数
E. Beautiful Subarrays One day, ZS the Coder wrote down an array of integers a with elements a1, ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...
- Educational Codeforces Round 12 E Beautiful Subarrays
先转换成异或前缀和,变成询问两个数异或≥k的方案数. 分治然后Trie树即可. #include<cstdio> #include<algorithm> #define N 1 ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- E. Beautiful Subarrays 字典树
http://codeforces.com/contest/665/problem/E 给定一个序列,问其中有多少个区间,所有数字异或起来 >= k 看到异或,就应该想到异或的性质,A^B^B ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
随机推荐
- 转:字符集和字符编码(Charset & Encoding)
转自:http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html ——每个软件开发人员应该无条件掌握的知识! ——Unicode伟大的创 ...
- python基础之常用内置函数
前言 python有许多内置的函数,它们定义在python的builtins模块,在python的代码中可以直接使用它们. 常用的内置函数 类型转换 int python的整数类型都是int类型的实例 ...
- 【Python学习】request库
Requests库(https://www.python-requests.org/)是一个擅长处理那些复杂的HTTP请求.cookie.header(响应头和请求头)等内容的Python第三方库. ...
- 【bzoj4293】【PA2015】Siano
如题,首先可以考虑相对大小是不变的. 那么先sort,之后每次在线段树上二分即可. #include<bits/stdc++.h> typedef long long ll; using ...
- geoip 扩展包根据ip定位详情
教程:https://laravel-china.org/courses/laravel-package/2024/get-the-corresponding-geo-location-informa ...
- acm专题--并查集
题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...
- HDRtools-OpenExr
转:http://blog.csdn.net/lqhbupt/article/details/7917764 OpenEXR是由工业光魔(Industrial Light& Magic)开发的 ...
- day4 递归原理及解析
递归 递归是一种调用自身的方法,在函数执行过程中重复不断的调用自身的过程,递归的规模每次都要缩小,一般前一步的程序作为后一步的参数.但是必须有递归结束条件. 递归算法是一种直接或者间接地调用自身算法的 ...
- C#窗体内嵌外部程序(cmd.exe)的显示【转载】
[DllImport("User32.dll ", EntryPoint = "SetParent")] private static extern IntPt ...
- HTML如何创建二级目录
#classify ul li div{width:100px; height:200px; display:none; position:absolute; left:100px; top ...