codeforces 665E Beautiful Subarrays
题目链接
给一个数列, 让你找出异或结果大于等于k的子序列的个数。
因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和。
我们考虑字典树, 对于每一个前缀sum, 我们先查询现有的字典树中有多少个数可以与它异或后大于等于k, 在将这个sum插入到字典树中。 这样就可以求出所有区间的异或情况。
具体操作看代码。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
struct node
{
node *next[2];
ll cnt;
node() {
next[0] = next[1] = NULL;
cnt = 0;
}
};
node *root = new node();
void insert(int x) { //字典树插入
node *p = root;
for(int i = 29; i >= 0; i--) {
int tmp = x>>i&1;
if(!p->next[tmp]) {
p->next[tmp] = new node();
}
p->cnt ++;
p = p->next[tmp];
}
p->cnt++;
}
int query(int x, int k) {
ll res = 0;
node *p = root;
for(int i = 29; i >= 0; i--) {
int tmp = x>>i&1;
if(k>>i&1) {
tmp ^= 1; //如果k这一位是1, 那么如果想要结果大于等于k, 这一位必须向tmp^1这一个方向移动
} else { //否则的话结果一定小于k。
if(p->next[tmp^1]) //如果k这一位是0, 那么答案就可以加上cnt[tmp^1]了。
res += p->next[tmp^1]->cnt;
}
if(p->next[tmp])
p = p->next[tmp];
else
return res;
}
return res+p->cnt;
}
int main()
{
int n, k;
cin>>n>>k;
int sum = 0, x;
ll ans = 0;
insert(sum);
for(int i = 0; i < n; i++) {
scanf("%d", &x);
sum ^= x;
ans += query(sum, k);
insert(sum);
}
cout<<ans<<endl;
return 0;
}
codeforces 665E Beautiful Subarrays的更多相关文章
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
- Codeforces 655E Beautiful Subarrays【01trie树】
题目链接: http://codeforces.com/contest/665/problem/E 题意: 求异或值大于给定K的区间个数. 分析: 首先我们可以得到区间前缀的异或值. 这样我们将这个前 ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
- 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, ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- Beautiful Subarrays
Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
随机推荐
- ng-validate
客户端表单验证是AngularJS里面最酷的功能之一. AngularJS表单验证可以让你从一开始就写出一个具有交互性和可相应的现代HTML5表单. 在AngularJS中,有许多表单验证指令.在这里 ...
- 浅谈:SAMBA配置设置
通过以下命令安装samba: yum install -y samba samba拥有三个服务,分别是: smbd 提供文件及打印共享功能,使用139.445端口 nmbd 提供NetBIOS支持 ...
- JavaScript function函数种类(转)
转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...
- About
about: Thanks to NetEase company, the people contribute to NetEase online course, Stephen Prata(< ...
- Boost.Python:使用继承
An example #include <boost/python.hpp> #include <memory> #include <iostream> using ...
- ajax接收遍历处理json格式数据
ajax在前后端的交互中应用非常广泛,通过请求后台接口接收处理json格式数据展现在前端页面. 下面我们来简单用 ajax在本地做一个接收并处理json的小例子 首先我们要新建一个叫做data的jso ...
- PHP多线程的实现方法详解
PHP5中可以使用新增的stream_socket_client()函数直接替换掉fsocketopen().PHP5之前的版本,你需要自己动手,用sockets扩展解决问题.PHP5的先进之处在于, ...
- python运维开发(五)----模块、生成器
内容目录 双层装饰器 字符串格式化 生成器和迭代器 递归 模块 双层装饰器 需求场景介绍: 现有用户登录系统,普通用户能查看自己相关信息的权限,管理员用户能查看所有用户的权限,可以做两个装饰器来实现需 ...
- Pods 更新后提示Bundle资源找不到
http://www.oschina.net/question/101347_2159145
- Hopcroft-Karp算法模版
#include <cstdio> #include <cstring> #include <vector> #include <queue> #inc ...