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 ...
随机推荐
- Android(java)学习笔记207:开源项目使用之gif view
1. 由于android没有自带的gif动画,我在Android(java)学习笔记198:Android下的帧动画(Drawable Animation) 播客中提到可以使用AnimationVie ...
- github/hexo搭建个人博客几个问题总结
问题一:hexo ERROR Deployer not found: github or hexo ERROR Deployer not found: git npm install hexo-dep ...
- 织梦dedecms源码安装方法
织梦dedecms源码安装方法 第一步: 上传所有文件到空间 注意:(由于有很多人反应安装后首页样式都乱的,所以强烈要求安装到根目录,如:127.0.0.1 / www.xxx.com,或者二级域名也 ...
- JS中的原型继承和多重继承
概念:1原型继承是创建新类型对象----子类型,子类型基于父类型,子类型拥有父类型所有的属性和方法(从父类型继承得到),然后修改其中的部分内容或者添加新的内容.继承最好在子类型模型可以被视为父类型对象 ...
- 随机提取N条记录[多种数据库方法]
随机提取10条记录的例子: Sql server: select top 10 * from 表 order by newid() Access: SELECT top 10 * FROM 表 ORD ...
- asp.net php asp jsp 301重定向的代码
介绍一下针对各类程序系统实施301重定向的代码: 1.Linux主机重定向 Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可以了,一般 ...
- 使用TreeView+ListBox+TxtBox 资料管理器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- java 下载文件功能代码例子
public static void down(HttpServletRequest request, HttpServletResponse response) throws Exceptio ...
- PHP学习笔记 - 进阶篇(9)
PHP学习笔记 - 进阶篇(9) 图形图像操作 GD库简介 GD指的是Graphic Device,PHP的GD库是用来处理图形的扩展库,通过GD库提供的一系列API,可以对图像进行处理或者直接生成新 ...
- javascript笔记—— 构造函数
出处:http://www.cnblogs.com/RicCC/archive/2008/02/15/JavaScript-Object-Model-Execution-Model.html 数据类型 ...