http://codeforces.com/contest/617/problem/E

题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况。

思路:没有区间修改,而且扩展端点,减小端点在前缀和的处理下都是O(n)的,使用莫队算法,每次移动区间时,注意计数是否先减.

/** @Date    : 2016-12-09-19.31
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct sion
{
int l, r;
int id;
}s[N]; LL a[N];
LL cnt[N*100];
int blc[N];
LL res[N]; int cmp(sion a, sion b)
{
if(blc[a.l] == blc[b.l])
return a.r < b.r;
return a.l < b.l;
} int main()
{
int n, q, k;
cin >> n >> q >> k;
a[0] = 0;
for(int i = 1; i <= n; i++)
{
scanf("%lld", a + i);
a[i] = a[i] ^ a[i-1];
}
int dlt = sqrt(n);
for(int i = 1; i <= n; i++)
{
blc[i] = (i - 1)/dlt + 1;
} for(int i = 1; i <= q; i++)
{
scanf("%d%d", &s[i].l, &s[i].r);
s[i].id = i;
}
sort(s + 1, s + 1 + q, cmp);
MMF(cnt);
cnt[0] = 1;
LL ans = 0;
int l = 0, r = 0;//前缀和的关系l初始为0
for(int i = 1; i <= q; i++)
{
while(l > s[i].l - 1)//一定要注意边界问题
l--, ans += cnt[k ^ a[l]], cnt[a[l]]++;//扩增区间先增加下标
while(r < s[i].r)
r++, ans += cnt[k ^ a[r]], cnt[a[r]]++;
while(l < s[i].l - 1)
cnt[a[l]]--,ans -= cnt[k ^ a[l]], l++;//缩小区间,由于前缀和的关系,先计数减1
while(r > s[i].r)
cnt[a[r]]--,ans -= cnt[k ^ a[r]] , r--;
res[s[i].id] = ans; }
for(int i = 1; i <= q; i++)
printf("%lld\n", res[i]);
return 0;
}

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 莫队

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

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

  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 - 617E XOR and Favorite Number (莫队+前缀和)

    Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...

  6. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

  7. XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)

    题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...

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

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

随机推荐

  1. css修改placeholder的样式

    css修改placeholder的样式 input::-webkit-input-placeholder { /* WebKit browsers */ font-size:14px; color: ...

  2. 《剑指Offer》题二十一~题三十

    二十一.调整数组顺序使奇数位于偶数前面 题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 测试用例: 功能测试:输入数组中的奇 ...

  3. Thunder团队第六周 - Scrum会议3

    Scrum会议3 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...

  4. wwnjld第二轮迭代测试报告

    1.引言 1.1测试报告目的 被测试报告为wwnjld小组我们的时间管理软件的第二轮迭代所写的软件测试报告.在经过本小组大家不懈的努力之下,我们小组第二轮迭代的产品终于新鲜出炉了.这次测试小组的主要成 ...

  5. Notes of the scrum meeting before publishing(12.17)

    meeting time:18:30~20:30p.m.,December 17th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  6. linux下php环境配置

    参: http://www.laozuo.org/5542.html LAMP,基于Linux/Apache/MySQL/PHP架构的网站建设环境,对于一般的网站来说足够使用,如果我们的网站访问量或者 ...

  7. IIS安装出现“安装程序无法复制文件CONVLOG.EX_”的解决办法

    重新安装了一次IIS,结果就在重新安装的时候,出现安装程序无法复制文件CONVLOG.EX_,上网找了找资料,是因为secedit.sdb 数据库的问题,既然是因为这个文件的问题,那么我们就可以使用w ...

  8. using指令含义

    using指令作用: 就是导入命名空间,这样你比如用StringBuilder类,就不用System.Text.StringBuilder builder = new System.Text.Stri ...

  9. python数据类型二

    阅读目录 1.列表的去嵌套 2.元组 3.range 列表的增删改查 一,增: 注意  list和str是不一样的,lst可以发生改变,所以直接就在原来的对象上进行可操作 追加模式 lst = ['麻 ...

  10. Redis集群分布(Windows版)

    Redis系列 作者Mr.Chen,转载请注明博客出处:http://www.cnblogs.com/cjh-notes/ 第一步:下载安装redis windows版的下载地址:https://gi ...