链接: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. ASP.NET MVC 4 批量上传文件

    上传文件的经典写法: <form id="uploadform" action="/Home/UploadFile" method="post& ...

  2. 关于Linux的总结(三)

    1.man_page.txt 1.内部命令:echo 查看内部命令帮助:help echo 或者 man echo 2.外部命令:ls 查看外部命令帮助:ls --help 或者 man ls 或者 ...

  3. Dubbo认识

    Dubbo提供了服务注册.RPC服务调用.调用均衡.服务监控和服务failover等功能 Dubbo框架中有两个重要角色:(服务)提供者和(服务)消费者,这里为了简单起见,将包含了dubbo提供者或消 ...

  4. windows下编辑过的文件在Linux下用vi打开行尾会多出一个^M符号

    一般情况下,windows下编辑过的文件在Linux下用vi打开行尾会多出一个^M符号,如下图: 这是因为Windows等操作系统用的文本换行符和UNIX/Linux操作系统用的不同,Windows系 ...

  5. jQurey对表单表格的操作及更多应用(方法型)

  6. Git CMD - init: Create an empty Git repository or reinitialize an existing one

    命令格式 git init [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir &l ...

  7. replace替换语句

    t_sql语句:replace替换语句:update 表名 set 列名=REPLACE(列名,'替换的数据','替换后的数据')

  8. kettle学习-day1-介绍、下载、安装

    QQ群:306059317\ 技术支持:荷露叮咚网络学苑http://www.heludd.com/kettle 下载最新版,获取视频教程 kettle安装: 1).下载需要安装的kettle版本,官 ...

  9. SQL Server2008新特性Filesteam的使用

    Filesteam是SQL Server2008的新特性,它结合了SQL Server和NTFS文件系统,为Blob类型的数据提供了比较高效的存储和访问方案.我们最近的一个项目就是采用SQL Serv ...

  10. Centos6.5 64linux系统基础优化(一)

    1  SecureCRT配色方案 http://blog.csdn.net/zklth/article/details/8937905 2  32位和64位Centos linux系统的区别及实际查看 ...