【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays
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
Understanding
Solution
前缀和思想,记录前缀异或值s[i],考虑贪心,一个数异或上自己为0,对于一段区间[l~r],异或值=s[l-1]^s[r],证明:s[l-1]^s[r]=s[l-1]^(s[l-1]^s[l~r])=(s[l-1]^s[l-1])^s[l~r]=s[l~r].
一般异或问题用trie树维护,所以,每次吧s[i]丢入trie树,sz记录当前节点的size,统计答案:将k二进制分解,顺着trie走,当前为0,答案+=sz[1](儿子)else 顺着trie树走下去,记得最后统计=的结果。
// <E.cpp> - Sun Oct 9 19:01:04 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
#define IN inline
#define RG register
using namespace std;
typedef long long LL;
typedef long double LB;
const int MAXN=2e+7;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline LL gi() {
register LL w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
int c[MAXN][2],s[MAXN],sz[MAXN];LL ans;int k,tot;
void insert(int x){
int u=1;
for(int i=30;i>=0;i--){
int p=(x>>i)&1;
if(!c[u][p])c[u][p]=++tot;
sz[u]++;u=c[u][p];
}sz[u]++;
}
void src(int x){
int u=1;
for(int i=30;i>=0;i--){
int p=((x>>i)&1)^1;
if(!((k>>i)&1))ans+=sz[c[u][p]],u=c[u][p^1];
else u=c[u][p];
}ans+=sz[u];
}
int main()
{
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
int n=gi();k=gi();int now=0;tot=1;insert(0);
while(n--){
int a=gi();now^=a;
src(now);insert(now);
}printf("%lld",ans);
return 0;
}
【Codeforces】665E Beautiful Subarrays的更多相关文章
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- 【题解】P4755 Beautiful Pair(启发式合并的思路+分治=启发式分治)
[题解]P4755 Beautiful Pair upd: 之前一个first second烦了,现在AC了 由于之前是直接抄std写的,所以没有什么心得体会,今天自己写写发现 不知道为啥\(90\) ...
- 【CodeForces】582 C. Superior Periodic Subarrays
[题目]C. Superior Periodic Subarrays [题意]给定循环节长度为n的无限循环数列,定义(l,s)表示起点为l的长度为s的子串,(l,s)合法要求将子串从该起点开始以s为循 ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
- 【CodeForces】925 C.Big Secret 异或
[题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...
- 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块
[题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...
随机推荐
- poj 1363 火车进站 (栈的应用)
Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...
- 杭电 2803 The MAX(sort)
Description Giving N integers, V1, V2,,,,Vn, you should find the biggest value of F. Input Each tes ...
- Codeforce 810C Do you want a date?
题意: 给定n个不重复的数, 求出这些数的所有子集, 然后设一个数Ni 为 第i个子集中,最大的数 - 最小的数. 然后将i个 Ni求和, 结果mod 1e9 + 7. 分析: 首先将n个数排列,生成 ...
- linux配置固定ip
vi /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO=static ONBOOT=yes 其他默认即可 重启network服务
- UI & 原型
UI & 原型 产品原型设计与团队实时协作平台 https://www.xiaopiu.com/download https://www.xiaopiu.com/forum/posts/5b9 ...
- SPOJ 3261 (树套树傻逼题)
As another one of their crazy antics, the N (1 ≤ N ≤ 100,000) cows want Farmer John to race against ...
- [NOIP2006] 提高组 洛谷P1065 作业调度方案
题目描述 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作,我们用记号j-k表示一个 ...
- Uva - 11181 Probability|Given (条件概率)
设事件B为一共有r个人买了东西,设事件Ai为第i个人买了东西. 那么这个题目实际上就是求P(Ai|B),而P(Ai|B)=P(AiB)/P(B),其中P(AiB)表示事件Ai与事件B同时发生的概率,同 ...
- 1085 数字游戏 2003年NOIP全国联赛普及组
丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分为m个部分 ...
- IOS开发之简单计算器
用Object-C写的一个简单的计算机程序,主要学习按钮的action动作. 以下是主界面: 以下代码时界面按钮和ViewController.h连接的地方: - (IBAction)button_0 ...