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 ][ ...
随机推荐
- Android EditText小结
防止EditText获取焦点弹出输入法 android:focusable="true" android:focusableInTouchMode="true" ...
- java工厂类与反射机制
java 简单工厂类 2012-04-22 15:44:07| 分类: java | 标签:java工厂类 简单工厂类 |举报|字号 订阅 简单工厂模式需要由以下角色组成: 接口 ...
- git使用前配置
1. git config --global user.name "Trey" 2. git config --global user.email 164355949@qq.com ...
- append, appendTo, after区别(preappend、before与这几个原理相同)
append在被选元素结尾插入内容,是被包围在所选元素的标签内的. <script> $('p').append('<a>http</a>'); </scri ...
- 原生JS研究:学习jquery源码,收集整理常用JS函数
原生JS研究:学习jquery源码,收集整理常用JS函数: 1. JS获取原生class(getElementsByClass) 转自:http://blog.csdn.net/kongjiea/ar ...
- php实现多表(四表)连接
<?php include_once "DBHelper.php"; define('HOST', '127.0.0.1'); define('USER', 'root'); ...
- php遍历目录输出目录及其下的所有图片文件
在做网站的时候,需要给文章内所有的图片添加上logo,如何利用ThinkPHP来实现. ThinkPHP为我们很好的提供了图像处理类,给文章中的所有图片加上水印的思路,上传的图片文件都保存在一个文件夹 ...
- C++ 常用容器or数据结构
queue 队列 参考 1.入队:如q.push(x):将x元素接到队列的末端: 2.出队:如q.pop() 弹出队列的第一个元素,并不会返回元素的值:T 3,访问队首元素:如q.front() 4, ...
- 凯恩斯主义VS货币主义
Milton Friedman在1960年代后期以及整个1970年代,到处不知疲倦地像传教士一般地宣讲他的货币主义.当时,美联储成员几乎清一色地是凯恩斯主义者.你可以想像Friedman的对手是多么强 ...
- 矩阵转置 O(1)空间
题目:用O(1)的空间实现矩阵的转置 为了方便,使用一维数组来分析.所谓矩阵转置,行变列,列变行.在转置的过程中,有的元素位置是不变的:对于变化位置的元素,要求O(1)空间完成,那么这些位置的变化一定 ...