题目大概说给一个序列,多次询问区间异或和为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(莫队算法)的更多相关文章

  1. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

  2. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

  3. codeforces 617E. XOR and Favorite Number 莫队

    题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...

  4. 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 ...

  5. 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 ...

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

  7. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  8. 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 ...

  9. CodeForces 617E XOR and Favorite Number

    莫队算法. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...

随机推荐

  1. express响应前端ajax请求

    后端其实并不需要知道前端发起的请求是不是ajax,后端只需要响应请求即可.例子: 前端这样写: $('button').on('click', function(event) { event.prev ...

  2. CSS3与页面布局学习总结——Box Model、边距折叠、内联与块标签、CSSReset

    目录 一.盒子模型(Box Model) 1.1.宽度测试 1.2.溢出测试 1.3.box-sizing属性 1.4.利用CSS画图 二.边距折叠 2.1.概要 2.2.垂直方向外边距合并计算 三. ...

  3. 【转】深入Windows内核——C++中的消息机制

    上节讲了消息的相关概念,本文将进一步聊聊C++中的消息机制. 从简单例子探析核心原理 在讲之前,我们先看一个简单例子:创建一个窗口和两个按钮,用来控制窗口的背景颜色.其效果 图1.效果图  Win32 ...

  4. 忘记Mysql登录密码

    1,使用安全模式跳过验证: 如果 Mysql在运行,Kill掉. 如果mysqld_safe无法启动,可用管理员权限sudo . 2,本地登录: 启动Mysql 3,修改密码: 5.7之后, 更改密码 ...

  5. JavaScript基础——理解变量作用域

    一旦你开始在JavaScript应用程序中添加条件.函数和循环,就需要理解变量作用域.变量作用域规定了如何确定正在执行的代码行上的一个特定变量名的值. JavaScript允许你既定义全局版本又定义局 ...

  6. Linux USB Project

    转自:http://www.linux-usb.org/ Welcome to the home of the Linux USB Project This web site was created ...

  7. probe函数何时调用的

    转自:http://blog.csdn.net/xiafeng1113/article/details/8030248 Linux中 probe函数何时调用的 所以的驱动教程上都说:只有设备和驱动的名 ...

  8. 手机站点动态效果插件TouchSlide

    今天看到TouchSlide插件,觉得非常不错,关于使用情况请看demo,下载地址:http://www.superslide2.com/TouchSlide/downLoad.html

  9. <转>Java 常用排序算法小记

    排序算法很多地方都会用到,近期又重新看了一遍算法,并自己简单地实现了一遍,特此记录下来,为以后复习留点材料. 废话不多说,下面逐一看看经典的排序算法: 1. 选择排序 选择排序的基本思想是遍历数组的过 ...

  10. Windows和Windows Phone应用终于可以使用FFmpeg了

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:曾经在Windows Phone上想开发一个支持多种格式的媒体播放器是比较困难的一件事 ...