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 ...
随机推荐
- 《Java虚拟机原理图解》 1.2、class文件中的常量池
了解JVM虚拟机原理 是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描述,很难给 ...
- tcp ip协议讲解
http://blog.csdn.net/zhangskd/article/details/7174682
- 新建JRapid项目(idea创建maven多模块项目)
1.第一步,新建项目(Create New Project) 2.parent项目,不勾选“Crate from archetype”,直接单击“Next”. 3.groupid填写com.codin ...
- Oracle版本–EBS R12.1.1
select * from v$version; Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Productio ...
- 【转】利用shell命令操作Memcached
原文: 张宴的博客 —— http://zyan.cc/post/384/ -------------------------------------------------------------- ...
- hadoop优质链接
http://wiki.apache.org/hadoop/FAQ
- HDU 3420 Bus Fair [补]
今天玩魔灵玩多了,耽误了时间,回去宿舍又没电. /*********************************************/ Bus Fair Time Limit: 2000/10 ...
- FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码(转)
本文来自 诗商·柳惊鸿 Poechant CSDN博客,转载请注明源地址:FastDFS的配置.部署与API使用解读(2)上传文件到FastDFS分布式文件系统的客户端代码 在阅读本文之前,请您先通过 ...
- no matching provisioning profiles found
问题:真机连上,执行这个提示. 解决: 项目->targets->Bulid Settings-> 1,Provisioning Profile->选择配置Bundle Ide ...
- 聊聊高并发(三十二)实现一个基于链表的无锁Set集合
Set表示一种没有反复元素的集合类,在JDK里面有HashSet的实现,底层是基于HashMap来实现的.这里实现一个简化版本号的Set,有下面约束: 1. 基于链表实现.链表节点依照对象的hashC ...