Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E
题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数;
思路:xor为和模2的性质,所以先预处理之后,可以枚举这种确实子串区间的方法为O(n^2);
优化?把每一个前缀xor扔到二叉树中,从根节点出发,以高位前缀值的二进制数构造一颗二叉树,这样就可以在构造的时候,实现对子树节点个数的求解;之后在线求解到当前节点的可选的前缀xor的个数,
即以k为主线
如果当前位k为1,则在前缀二叉树中只能找和当前节点xor出结果为1的节点,并且这个节点的num值不能加到结果中;
如果是当前位k为0,则直接加上xor结果为1的节点个数(num),方向不变;
注:最后一个叶子节点不能求到,要在最后加上叶子节点的个数
时间复杂度为O(nlgn)
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = ;
int num[MAXN],tot = ,d[MAXN][],n,k;
void update(int a)
{
for(int i = , p = ;i >= ;i--){
if(d[p][(a>>i)&] == ) d[p][(a>>i)&] = ++tot;
p = d[p][(a>>i)&];
num[p]++;
}
}
long long solve(int x)
{
long long ans = , p = ;
for(int i = ;i >= ;i--){
if((k>>i)&) p = d[p][^((x>>i)&)];
else{
ans += num[d[p][^((x>>i)&)]];
p = d[p][(x>>i)&];
}
}
return ans + num[p]; // leaf
}
int main()
{
cin>>n>>k;
int prefix = ,x;
long long ans = ;
for(int i = ;i < n;i++){
update(prefix);
scanf("%d",&x);
prefix ^= x;
ans += solve(prefix);
}
printf("%I64d",ans);
}
Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化的更多相关文章
- 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, ...
- 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 ...
- 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 ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- Educational Codeforces Round 12 B. Shopping 暴力
B. Shopping 题目连接: http://www.codeforces.com/contest/665/problem/B Description Ayush is a cashier at ...
随机推荐
- ASP.NET MVC and jqGrid 学习笔记 1-基本配置
新建一个mvc项目后
- rpmdb: BDB0113 错误
解决方法: rpm --rebuilddb yum clean all
- 怎么在html页面和js里判断是否是IE浏览器
HTML里: HTML代码中,在编写网页代码时,各种浏览器的兼容性是个必须考虑的问题,有些时候无法找到适合所有浏览器的写法,就只能写根据浏览器种类区别的代码,这时就要用到判断代码了.在HTML代码中, ...
- 【转】【CDC翻客】移动端App测试实用指南
译者注:本文从测试人员的角度出发,提出了100多个在测试移动App过程中需要考虑的问题.不管你是测试人员.开发.产品经理或是交互设计师,在进行移动App开发时,这些问题都很有参考价值.我和Queen ...
- 开始学习css
今天开始学习css:应用一本<HTML5与CSS3网页设计基础> 先学习css样式规则声明. Body{ color:blue} 对应:选择符:{声明属性:声明值}: Background ...
- HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integr
将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序"PageHandlerFactory-Integr"在 ...
- Rebind and Rewind in Execution Plans
http://www.scarydba.com/2011/06/15/rebind-and-rewind-in-execution-plans/ Ever looked at an execution ...
- zDialog无法获取未定义或 null 引用的属性“_dialogArray”
zDialog无法获取未定义或 null 引用的属性"_dialogArray" 贴出错误:这个错误是从IE浏览器的控制台复制出来的. zDialog无法获取未定义或 null 引 ...
- js 验证电话号 座机及手机号
function CheckTel() { /*验证电话号码 验证规则:区号+号码,区号以0开头,3位或4位号码由7位或8位数字组成 区号与号码之间可以无连接符,也可以“-”连接 如010888888 ...
- 【知识分享】UIButton setTitle 设置为空 失效
今天开发练习超级猜图,但是碰到了一个奇怪的问题 困扰我一个晚上,低效的夜晚 可恨~ 示例说明1 [button setTitle:@"" forState:UIControlSta ...