Codeforces 617E XOR and Favorite Number莫队
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莫队的更多相关文章
- 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 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- 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 E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- 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 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...
- 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 ...
随机推荐
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
- POJ 2987 Firing(最大流最小割の最大权闭合图)
Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...
- 一次大量TIME_WAIT和Recv-Q 堵塞问题排查思路
记录一下周末出现问题~ 仅自己摘记不做任何参考. 第一天故障: 现象: 公司销售群和售后群炸了,说老后台(1.0版本)崩溃了,因为还有部门的业务没来得及迁移到新后台,我当时正在打农药哈哈~ 后 ...
- Python中的Dictionary
Dictionary的创建 1 字面量 >>>D = {'a': 1, 'b': 2} >>>D {'b': 2, 'a': 1} 2 keyword参数 > ...
- 最多水容器(M)
题目 给定n个非负整数a 1,a 2,...,a n,其中每个代表坐标(i,a i)处的一个点.绘制n条垂直线,使得线i的两个端点处于(i,a i)和(i,0)处.找到两条线,它们与x轴一起形成一个容 ...
- Linux 路由 学习笔记 之一 相关的数据结构
http://blog.csdn.net/lickylin/article/details/38326719 从现在开始学习路由相关的代码,在分析代码之前, 我们还是先分析数据结构,把数据结构之间的关 ...
- 【beta】Scrum站立会议第1次....11.3
beta阶段,我们nice!团队将进行为期两周的冲刺,Scrum站立会议10次. 小组名称:nice! 组长:李权 成员:于淼 刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 时间:2016.1 ...
- windows批处理学习(字符换操作)---04
转自:https://www.cnblogs.com/DswCnblog/p/5432326.html 1.截取字符串 截取字符串可以说是字符串处理功能中最常用的一个子功能了,能够实现截取字符串中的特 ...
- array to object
array to object native js & ES6 https://stackoverflow.com/questions/4215737/convert-array-to-obj ...
- DELPHI Showmodal 模式窗体
Showmodal 是个函数, Show 是个过程 1. Showmodal: 概念 : 当你调用一个窗口用 SHOWMODAL 时 , 当这个窗口显示出来后 , 程序不会继续自己执行 , 而 ...