题目链接

给一个数列, 让你找出异或结果大于等于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的更多相关文章

  1. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  2. 【Codeforces】665E Beautiful Subarrays

    E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...

  3. Codeforces 655E Beautiful Subarrays【01trie树】

    题目链接: http://codeforces.com/contest/665/problem/E 题意: 求异或值大于给定K的区间个数. 分析: 首先我们可以得到区间前缀的异或值. 这样我们将这个前 ...

  4. codeforces 665E E. Beautiful Subarrays(trie树)

    题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...

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

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

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

  7. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  8. Beautiful Subarrays

    Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  9. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

随机推荐

  1. ChildNodes详解及其兼容性处理方式

    写在前面:在做insertBefore插入节点练习时发现一个问题,插入childNodes[0]和childNodes[1]时插入的位置是一样的!于是有了childNodes的了解,有了这篇文章,欢迎 ...

  2. .Net 缓存依赖详解

    缓存命名空间的讲解流程 16.1  System.Web.Caching简介 本节从缓存命名空间的总体简介和组成结构入手,从整体上对System.Web.Caching进行概述. 16.1.1  Sy ...

  3. mysql 查看mysql版本的四种方法

    1 命令行中使用status可以查看. mysql> status;--------------mysql  Ver 14.14 Distrib 5.5.25a, for Linux (x86_ ...

  4. Android EditText屏蔽默认长按粘贴复制事件

    et.setCustomSelectionActionModeCallback(new ActionMode.Callback()); 添加全部的方法即可,不需要任何改动.

  5. CSS自学笔记(16):CSS3 用户界面

    CSS3中,也新增了一些关于用户界面的属性,这些属性可以重设元素或者盒子的尺寸.轮廓等等. 新增的部分属性的浏览器支持情况 属性 浏览器支持 resize IE Firefox Chrome Safa ...

  6. 算法分析-快速排序QUICK-SORT

    设要排序的数组是A[0]……A[N-1],首先任意选取一个数据(通常选用数组的第一个数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序.值得注意的 ...

  7. spring MVC通过json与前台交互

    这里用的是spring4.1.4,jquery2.1.3,其它环境为:myeclipse2014,tomcat7,jdk7 首先,新建一个web工程,并导入springMVC的jar包(为了方便起见我 ...

  8. JAVA之数组查询binarySearch()方法详解

    binarySearch()方法提供了多种重载形式,用于满足各种类型数组的查找需要,binarySearch()有两种参数类型 注:此法为二分搜索法,故查询前需要用sort()方法将数组排序,如果数组 ...

  9. Struts2 请求参数接收

    在Struts2中提供了更为简单的参数请求与接收方法,可以直接在Action中定义属性:Struts2通过反射机制将参数反射到属性的set方法上实现参数的传递: GET方式传送参数 <strut ...

  10. 使用Transaction访问数据库(C#,TransactionScope,.NET 2.0)

    针对SQL2005和.NET 2.0的事物机制有了新的突破传统数据库事物访问机制,代码如下:                   或者这种,其实都差不多                         ...