Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个。
莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减少对答案的贡献。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN (1<<20) int block;
struct Query{
int l,r,i;
bool operator<(const Query &q) const {
if(l/block==q.l/block) return r<q.r;
return l/block<q.l/block;
}
}query[]; int n,m,k,a[],sum[]; int cnt1[MAXN],cnt2[MAXN];
long long ans;
void left_insert(int i){
++cnt1[sum[i-]];
++cnt2[sum[i]];
ans+=cnt2[sum[i-]^k];
}
void left_remove(int i){
ans-=cnt2[sum[i-]^k];
--cnt1[sum[i-]];
--cnt2[sum[i]];
}
void right_insert(int i){
++cnt1[sum[i-]];
++cnt2[sum[i]];
ans+=cnt1[sum[i]^k];
}
void right_remove(int i){
ans-=cnt1[sum[i]^k];
--cnt1[sum[i-]];
--cnt2[sum[i]];
} long long res[]; int main(){
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; ++i){
scanf("%d",a+i);
sum[i]=sum[i-]^a[i];
}
for(int i=; i<m; ++i){
scanf("%d%d",&query[i].l,&query[i].r);
query[i].i=i;
} block=(int)(sqrt(n)+1e-);
sort(query,query+m); int l=,r=;
right_insert();
for(int i=; i<m; ++i){
while(l<query[i].l) left_remove(l++);
while(l>query[i].l) left_insert(--l);
while(r<query[i].r) right_insert(++r);
while(r>query[i].r) right_remove(r--);
res[query[i].i]=ans;
} for(int i=; i<m; ++i){
printf("%lld\n",res[i]);
}
return ;
}
Codeforces 617E XOR and Favorite Number(莫队算法)的更多相关文章
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- codeforces 617E E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- CODEFORCES 340 XOR and Favorite Number 莫队模板题
原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...
- CodeForces 617E XOR and Favorite Number
莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...
随机推荐
- September 20th 2016 Week 39th Tuesday
Failure is not fatal, but failure to change might be. 失败并不致命,但无法改变却可能是致命的. I need change, but it see ...
- Caffe初试(一)win7_64bit+VS2013+Opencv2.4.10+CUDA6.5配置Caffe环境
折腾了几天,终于在windows系统上成功配置了Caffe环境,期间遇到了很多问题,每个问题的解决也都花了不少时间,查过挺多资料,感觉挺有意义,这里写篇博客记录一下. 原来我使用的CUDA版本是7.5 ...
- Makefile_:=与=的区别
1."=" make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个makefile中最后被指定的值.看例子: x = foo y ...
- bnu24252 海盗分赃
题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24252 这是四川2012年省赛的一道题,背景:海盗分宝藏.大概题意:给你N种价值的物品,物品有两 ...
- C#调用C++DLL的小总结5---和C++的DLL的联合调试
http://fpcfjf.blog.163.com/blog/static/5546979320134922938373/ http://blog.csdn.net/jiangxinyu/artic ...
- Android Tab -- 使用ViewPager、PagerAdapter来实现
原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 效果:滑动切换,自动切换. 代码:https://github.com/ldb ...
- Installing MySQL Server on CentOS
MySQL is an open-source relational database. For those unfamiliar with these terms, a database is wh ...
- hdu 4023 2011上海赛区网络赛C 贪心+模拟
以为是贪心,结果不是,2333 贪心最后对自己绝对有利的情况 点我 #include<cstdio> #include<iostream> #include<algori ...
- iOS中图片动画的三种模式及基本的代码实现
-(void)play { //第一种图片动画模式 头尾方式 //头尾方式 [UIView beginAnimations:nil context:nil];//动画开始 [UIView setAni ...
- 通信原理实践(六)——基带传输
一.基带传输引入 1.从数字带通传输说起 以上系统可以等价为: 这里"等价"的假设条件是 •信号通过滤波器不失真 •不存在码间串扰 意义:可以通过评估基带传输系统来获得数字带通传输 ...