题目链接

给一个数列, 让你找出异或结果大于等于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. Java日志管理

    首页 资讯 精华 论坛 问答 博客 专栏 群组 更多 ▼ 您还未登录 ! 登录 注册 JavaCrazyer的ItEye(codewu.com)技术博客   博客 微博 相册 收藏 留言 关于我   ...

  2. oracle中的B-TREE索引

    在字段值情况不同的条件下测试B-TREE索引效率 清空共享池和数据缓冲区alter system flush shared_pool;alter system flush buffer_cache; ...

  3. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

  4. php输出json中文显示编码-解决办法

    $str = "中华人民共和国";$ar = array( "a" => "a0", "b" => &quo ...

  5. C++ vector 实现二维数组时, 在类的头文件中定义时遇到"应输入类型符"的问题?

    见下,当我在类的声明文件中定义二维vector时,提示我应输入类型说明符; 但是相同的格式定义,在类中将二维vector修改为在源文件中定义就可以顺利通过,并顺利执行打印 打印结果如下: 望大神来解惑 ...

  6. JavaScript基本概念(操作符)

    一元操作符 一元操作符在处理所有的非数值时,相当于将该值经过Number()转换成数值,如 +"12" 将把 "12" 字符串转换为数字. 位操作符 负数在计算 ...

  7. ThinkPHP框架研究之一 基本函数 M和D的区别

    http://my.oschina.net/wxweven/blog/56563?fromerr=32n4Nf7V https://segmentfault.com/q/101000000298807 ...

  8. httpClient download file(爬虫)

    package com.opensource.httpclient.bfs; import java.io.DataOutputStream; import java.io.File; import ...

  9. ssh 应用

    SSH反向连接及Autossh ssh 隧道: http://www.cnblogs.com/robinyjj/archive/2008/11/02/1325018.html This guy wri ...

  10. getline(cin,s) bug workaround

    #include<iostream>using namespace std;#include<string> int main(){int n;stirng s;cin> ...