Codeforces 655E Beautiful Subarrays【01trie树】
题目链接:
http://codeforces.com/contest/665/problem/E
题意:
求异或值大于给定K的区间个数。
分析:
首先我们可以得到区间前缀的异或值。
这样我们将这个前缀M和K一起走trie树,如果该位K的值为0,那么无论怎么走最后得到的答案都不会比K小,所以直接加上另一边的子树大小,然后继续沿着当前边走。如果该位K的值为1,那么想要大于等于K必须沿着另一边贪心的走。
代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 1000000 << 5, maxm = 1e6 + 5;
int f[maxn][2];
int cnt = 1;
int son[maxn], pre[maxm];
long long ans = 0;
#define pl(n) cout<<#n<<": "<<n<<endl;
#define sa(n) scanf("%d", &n)
void insert(int n)
{
int k;
int u = 1;
for(int i = 30; i >= 0; i--){
k = (n >> i) & 1;
if(!f[u][k]) f[u][k] = ++cnt;
son[u]++;
u = f[u][k];
}
son[u]++;
}
void query(int n, int m)
{
int k1, k2, u = 1;
for(int i = 30; i >= 0; i--){
k1 = (n >> i) & 1;
k2 = (m >> i) & 1;
if(k2 == 0){
ans += son[f[u][!k1]];
u = f[u][k1];
}
else u = f[u][!k1];
if(u == 0) break;
}
ans += son[u];
}
int main (void)
{
int n,k;sa(n);sa(k);
pre[0] = 0;
int num;
insert(pre[0]);
for(int i = 1; i <= n; i++){
sa(num);
pre[i] = pre[i - 1] ^ num;
query(pre[i], k);
insert(pre[i]);
//pl(ans);
}
printf("%I64d\n", ans);
return 0;
}
Codeforces 655E Beautiful Subarrays【01trie树】的更多相关文章
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
- E. Beautiful Subarrays 字典树
http://codeforces.com/contest/665/problem/E 给定一个序列,问其中有多少个区间,所有数字异或起来 >= k 看到异或,就应该想到异或的性质,A^B^B ...
- codeforces 665E Beautiful Subarrays
题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
- 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, ...
- Beautiful Subarrays
Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
随机推荐
- CF-1099 D. Sum in the tree
CF-1099 D. Sum in the tree 题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i], 然后定义一s[i]表示从根节点到 结点序号为i的结点的路途上所经过 ...
- Python之路-基础数据类型之列表 元组
列表的定义 列表是Python基础数据类型之一,它是以[ ]括起来, 每个元素用' , '隔开而且可以存放各种数据类型: lst = [1,2,'你好','num'] 列表的索引和切片 与字符串类似, ...
- Lex与Yacc学习(二)之第一个Lex程序
用lex识别单词 构建一个识别不同类型英语单词的简单程序.先识别词性(名词,动词等),然后再扩展到处理符合简单英语语法的多个单词的句子. 先列出要识别的一组动词: is am are w ...
- Java实现——字符串分割以及复制目录下的所有文件
0. 前言 今天有个需求,把Android中data/data目录下指定(通过读预置的XML文件)的多个应用下的多个目录全部内容通过OTG模式复制到U盘中. 首先读取XML文件内的某个节点的属性值, ...
- idea14配置tomcat
1.File -> New Project ,进入创建项目窗口 2.在 WEB-INF 目录下点击右键,New -> Directory,创建 classes 和 lib 两个目录 3.F ...
- luogu2634 聪聪可可
点分治裸题 #include <iostream> #include <cstdio> using namespace std; int n, uu, vv, ww, ans, ...
- Head First HTML5 Programming笔记--chapter2 介绍Javascript和DOM
你已经了解了HTML标记(也称为结构),而且知道了CSS样式(也称为表示),剩下的就是Javascript(也称为行为). JavaScript的工作方式 1. 编写 你创建HTML标记和JavaSc ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- WPF之DataAnnotations 注解说明
参考:https://www.cnblogs.com/yaosuc/p/4527886.html 1.基础验证: using System.ComponentModel.DataAnnotations ...
- .NET重构(四):窗体继承+模板方法,完美实现组合查询
导读:在机房重构中,有好些个查询都是大同小异,最为显著的就是组合查询了.怎样给自己省事儿,相同的东西能不能重复利用,就成了一个现实的问题.第一遍做机房的时候,使用的更多的是:复制+粘贴.学习了设计模式 ...