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 ...
随机推荐
- Honda HDS IMMO PCM Code calculator Free Download
HDS IMMO PCM Code calculator software for Honda vehicle models is free download available in Eobd2.f ...
- python(5)–sys模块
sys.argv 命令行参数list, 第一个元素是程序本身路径 sys.exit(n) 退出程序,退出时输入信息n sys.version 获取python解释程序的版本信息 sys.maxint ...
- 【思路、优化】UVa 11491 - Erasing and Winning
Juliano is a fan of the TV show Erasing and Winning, where participants are selected in a draw and r ...
- Angular 全局页面切换动画 me-pageloading
最近看了Codrops的一篇文章, 里面讲到了一个页面切换的效果, 详情点击此处. 看了这个效果感觉很赞, 觉得这个效果可以用在angular的页面切换中, 所以将这个效果移植到angular中, 做 ...
- 《跨终端Web》读书笔记
跨终端的Web成为了趋势,而这本书就是讲了在这种趋势下进行开发的常见问题及其解决方案,可能是限于篇幅,每个方面都没有展开细说,但这是这样让本书干货满满,几乎没有一句废话. 下面是一些笔记. Web的本 ...
- FindControl 无法找到控件问题解决方案
若用 string cdept =((HtmlInputText)FindControl("dept0" + i.ToString())).Value; 提示结果为空值,即无法找到 ...
- 第一次使用github很高端的赶脚
- Android 中的图像特效(Matrix)
以前在线性代数中学习了矩阵,对矩阵的基本运算有一些了解,现在在Android中有一个Matrix类,它的中文意思就是矩阵.Matrix主要是用于图像的缩放.平移.旋转.扭曲等操作.图像处理,主要用到的 ...
- 关闭“编辑窗体”后, 主窗体的DatagridView刷新数据的问题
问题:在关闭一个窗体2后,要刷新窗体1内的datagridview的数据,直接窗体1.datagridview.datasource=dt 是没用的. 解决办法如下: 在主窗体里 创建编辑窗体时 加上 ...
- 【转载】颜色空间-RGB、HSI、HSV、YUV、YCbCr的简介
转载自缘佳荟的博客. 颜色通常用三个相对独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间.而颜色可以由不同的角度,用三个一组的不同属性加以描述,就产生了不同的颜色空间.但 ...