链接: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 预处理+二叉树优化的更多相关文章

  1. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  2. 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,   ...

  3. Educational Codeforces Round 12 E Beautiful Subarrays

    先转换成异或前缀和,变成询问两个数异或≥k的方案数. 分治然后Trie树即可. #include<cstdio> #include<algorithm> #define N 1 ...

  4. [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 ...

  5. Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. Educational Codeforces Round 12 D. Simple Subset 最大团

    D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...

  8. Educational Codeforces Round 12 C. Simple Strings 贪心

    C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...

  9. Educational Codeforces Round 12 B. Shopping 暴力

    B. Shopping 题目连接: http://www.codeforces.com/contest/665/problem/B Description Ayush is a cashier at ...

随机推荐

  1. [Java] HashMap、TreeMap、Hashtable排序

    Java中对Map(HashMap,TreeMap,Hashtable等)的排序时间 首先简单说一下他们之间的区别: HashMap: 最常用的Map,它根据键的HashCode 值存储数据,根据键可 ...

  2. Node.js module.exports和exports的区别

    require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...

  3. sql 自定义函数-16进制转10进制

    做过笔记,好记性不如烂笔头: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HEXTOINT]') and ...

  4. 4632 NOIP[2015] 运输计划

    4632 NOIP[2015] 运输计划  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题解       题目描述 Description 公元 2044 ...

  5. 【AngularJs】---实现select的ng-options

    controller .controller('MainController', function($scope, $http, $ionicModal, $timeout) { var post = ...

  6. Unity3D 之UGUI 滚动条

    先上效果图. 这里来说明下UGUI 滚动条,不涉及到代码. 主要用到的控件Scroll Rect ,Mask,Scrollbar. 第一步,建立一个Image,然后绑定一个滑动块的组件,添加一个mas ...

  7. As,is含义?using 语句

    Is:检查对象是否与给定的类型兼容.例如,下面的代码可以确定MyObject类型的一个实例,或者对象是否从MyObject派生的一个类型:        if(obj is MyObject){}   ...

  8. 解决ASP.NET网站发布问题

    目录 前言 开始 aspx.cs文件放到单独的类库项目 一个可选择勾选页面的发布工具:LimusicAddin 前言 Asp.net 发布分为:动态编译和预编译.预编译又分为:In Place Pre ...

  9. 面试之SQL(2)--left join, inner join 和 right join的区别

    表A记录如下: aID        aName 1           a1 2           a2 3           a3 4           a4 5           a5 ...

  10. Cocos2d-x开发实例介绍特效演示

    下面我们通过一个实例介绍几个特效的使用,这个实例下图所示,下图是一个操作菜单场景,选择菜单可以进入到下图动作场景,在下图动作场景中点击Go按钮可以执行我们选择的特性动作,点击Back按钮可以返回到菜单 ...