codeforces 665E E. Beautiful Subarrays(trie树)
题目链接:
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 题意: 给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+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e7+;
int n,k,cnt=;
int tree[N][],val[N];
void add_num(int num)
{
int x=;
for(int i=;i>=;i--)
{
int y=(num>>i)&;
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=;
int x=;
for(int i=;i>=;i--)
{
int y=(fy>>i)&;
int fx=(num>>i)&^;
if(y==)
{
sum+=(LL)val[tree[x][fx]];
x=tree[x][fx^];
}
else x=tree[x][fx];
}
return sum+(LL)val[x];
}
int main()
{
scanf("%d%d",&n,&k);
int pre=;
LL ans=;
add_num();
Riep(n)
{
int a;
scanf("%d",&a);
pre^=a;
ans+=(LL)get_ans(pre,k);
add_num(pre);
}
printf("%I64d\n",ans); return ;
}
codeforces 665E E. Beautiful Subarrays(trie树)的更多相关文章
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- 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, ...
- Codeforces 852G Bathroom terminal 【Trie树】
<题目链接> 题目大意: 现在给定出n个字符串,并且进行m此询问,每次询问给出一个匹配串,每次询问都给出该匹配串能够匹配的字符串个数(题目只出现字符'a'~'e').'?'可以看成任意字符 ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- Codeforces 1085G(1086E) Beautiful Matrix $dp$+树状数组
题意 定义一个\(n*n\)的矩阵是\(beautiful\)的,需要满足以下三个条件: 1.每一行是一个排列. 2.上下相邻的两个元素的值不同. 再定义两个矩阵的字典序大的矩阵大(从左往右从上到下一 ...
- E. Beautiful Subarrays 字典树
http://codeforces.com/contest/665/problem/E 给定一个序列,问其中有多少个区间,所有数字异或起来 >= k 看到异或,就应该想到异或的性质,A^B^B ...
- Codeforces 1464F - My Beautiful Madness(树的直径)
Codeforces 题面传送门 & 洛谷题面传送门 树上数据结构大杂烩(?) 首先考虑什么样的点能够在所有路径的 \(d\) 邻居的交集内.显然如果一个点在一条路径的 \(d\) 邻居内则必 ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
随机推荐
- Iass、Pass、SasS三种云服务区别?
Iass.Pass.SasS三种云服务区别 我们可以把云计算理解成一栋大楼,而这栋楼又可以分为顶楼.中间.低层三大块.那么我们就可以把Iass(基础设施).Pass(平台).Sass(软件)理解成这栋 ...
- JDK内置工具jstack(Java Stack Trace)(转)
1.介绍 jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的js ...
- Spring的IoC容器-Spring ApplicationContext容器
Application Context是spring中较高级的容器.和BeanFactory类似,它可以加载配置文件中定义的bean,将所有的bean集中在一起,当有请求的时候分配bean. 另外,它 ...
- ElasticSearch集群本机搭建
ElasticSearch集群本机搭建 elasticsearch elasticsearch -Ehttp.port=8200 -Epath.data=node2 elasticsearch -Eh ...
- 转:为什么Uber宣布从Postgres切换到MySQL?
转: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=2653547609&idx=1&sn=cbb55ee823dd ...
- table合并单元格
table合并单元格 新建一张表格.要求表格有四行四列,当中第一行的四列合并,第二行.第三行和第四行的第一列合并 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- JAVA_MyEclipse常见配置NETGEAR路由器如何设置
1 把宽带网线接到路由器的Internet口,然后用一个网线连接PC和路由器的任意端口(1,2,3,4一般四个),如下图所示 2 在浏览器地址栏中输入下面的网址,中途会弹出对话框询问账号密码,默认的账 ...
- vc常用类总结(转载)
常用类 CRect:用来表示矩形的类,拥有四个成员变量:top left bottom right.分别表是左上角和右下角的坐标.可以通过以下的方法构造: CRect( int l, int t, i ...
- 《Java虚拟机原理图解》4.JVM机器指令集
0. 前言 Java虚拟机和真实的计算机一样,执行的都是二进制的机器码:而我们将.java 源码编译成.class 文件,class文件便是Java虚拟机可以认识的二进制机器码,Java可以识别cla ...
- iOS 小动画
一.图片旋转 CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPa ...