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. /etc/fstab 文件如何填写(转)

    转载自 http://hi.baidu.com/jingzhongchen/blog/item/8e6f552dcead7ce98b139952.html 看你对/etc/fstab文件了解多少?   ...

  2. 基于NABCD评论“欢迎来怼”团队Alpha版作品

    NABCD分析 N(需求) 随着博客园网页版的出现,大家希望能够随时看自己博客,查看别人的博客,以及写博客,评论博客等功能.对于学生的我们,及时了解作业的动态很重要,电脑不能随时携带,但手机随身携带, ...

  3. LintCode-12.带最小值操作的栈

    带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 注意事项 如果堆栈中 ...

  4. Linux服务器记录并查询历史操作记录

    Linux服务器在使用过程中,经常会有除自己之外的其他人员使用.并不是每个人都对Linux服务器特别熟悉,难免会有一些操作导致服务器报错. 因此,监控Linux服务器的操作并记录下来,是非常有必要的! ...

  5. foreach循环2

    <select id="test" parameterType="java.util.List" resultType="user"& ...

  6. 【Python】Python中的引用和赋值

    本文转自:http://my.oschina.net/leejun2005/blog/145911 在 python 中赋值语句总是建立对象的引用值,而不是复制对象.因此,python 变量更像是指针 ...

  7. 7款很棒的 HTML5 视频播放器

    做个连接:http://www.cnblogs.com/lhb25/archive/2011/06/27/7-great-html-5-video-player-scripts.html

  8. 【bzoj4229】选择 离线+LCT

    题目描述 现在,我想知道自己是否还有选择. 给定n个点m条边的无向图以及顺序发生的q个事件. 每个事件都属于下面两种之一: 1.删除某一条图上仍存在的边 2.询问是否存在两条边不相交的路径可以从点u出 ...

  9. 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛 树形dp

    题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...

  10. c/c++中的关键字(static、const、inline、friend)

    static:1.a.c语言中static修饰的局部变量在编译时赋初始值,只赋初始值一次,在函数运行时已有初值,每次调用函数时不用重新赋值,指示保留上次 函 数调用结束时的值. 如果定义局部变量不赋初 ...