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系统将普通应用升级为系统应用
作为一名程序员,有的时候并不是使用软件,而是去改造软件,不仅仅只是会编程而已,还要满足客户的需求.这样,才能开发出符合客户需求的应用,在关于到涉及到android底层的应用的时候,手机就需要root了 ...
- 在Fedora 23 Server和Workstation上安装LAMP(Linux, Apache, MariaDB和PHP)
在安装LAMP之前,建议先更新系统包$ sudo dnf update 第一步:安装Apache Web服务器1.在Fedora 23安装Apache,你可以运行下面的命令:$ sudo dnf in ...
- Java中 map.values转换为list或者string[]
@Test public void testMap2List() throws Exception{ Map<String, String> map = new HashMap<St ...
- oc结构
结构 在oc中只能声明变量 不能声明函数和类 结构声明 struct DateT { int month; int day; int year; }; 结构可以在起最后的分号之后定义结构变量,并且可以 ...
- panel控件 换行
Panel1.Controls.Add(new LiteralControl("<BR/>"));
- HDU 1032 The 3n + 1 problem
还以为要递归推一推的 结果暴力就过了 要注意 i,j 大小 #include <iostream> using namespace std; int a,b; long long cnt, ...
- Char型和string型字符串比较整理
1.赋值 char赋值: char ch1[] = "give me"; char ch2[] = "a cup"; strcpy(ch1,ch2); cout ...
- sublime text帮你更好的写python
在Google的Python风格指南中,有这样的要求: 用4个空格来缩进代码 但是每次在敲代码的时候,用一个tab确实比敲四次空格方便的多.令人欣慰的是sublime text 2能够把tab转换成4 ...
- PDO的事物处理机制
Mysql的事务处理: 1.MySQL目前只有InnoDB 和BDB两个数据表类型才支持事务. 2.在默认条件下,MySQL是以自动提交(autocommit)模式运行的,这就意味着所执行的每一个语句 ...
- WinCmd
Q1:tracert Tracert (跟踪路由)是路由跟踪实用程序,用于确定 IP 数据报访问目标所采取的路径. Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定 ...