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 ...
随机推荐
- "Hello world!"团队第一次会议
今天是我们"Hello world!"团队第一次召开会议,今天的会议可能没有那么正式,但是我们一起确立了选题——基于WEB的售票系统.博客内容是: 1.会议时间 2.会议成员 3. ...
- C# #pragma warning disable/restore
#pragma warning 可以启用或禁用特定警告. 语法 #pragma warning disable warning-list #pragma warning restore warning ...
- python json 序列化
如果我们要在不同的编程语言之间传递对象,就必须把对象序列化为标准格式,比如XML,但更好的方法是序列化为JSON,因为JSON表示出来就是一个字符串,可以被所有语言读取,也可以方便地存储到磁盘或者通过 ...
- 内核blackhole
1) 当arp表项不存在的时候,数据包等待表项存在了再发,还是直接把数据包给丢掉; 2)如果网络目的地址不可达,是在那一层把数据丢弃,再是路由层就判断还是arp层呢?
- Unsupported major.minor version 52.
面试的时候,京东和美团,360的面试官都问到了同一个问题,java1.7与java1.8的区别, 于是想做个小小的例子: 我的eclipse刚开始是1.7的,后来,我把环境改成了1.8的, 方法:右击 ...
- 【.Net】在C#中判断某个类是否实现了某个接口
有时我们需要判断某个类是否实现了某个接口(Interface),比如在使用反射机制(Reflection)来查找特定类型的时候. 简单来说,可以使用Type.IsAssignableFrom方法: t ...
- SPD各模块总结
一.用户角色绑定节点 1.库存操作员.库存主管.验货操作员:绑定任一节点 2.采购操作员.公药操作员:只能绑定药库节点 3.退库操作员.药品申领员:绑定药库以外的节点 二.采购计划模块 1.采购计划的 ...
- MATLAB中imfilter函数
功能:对任意类型数组或多维图像进行滤波. 用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,...) 或写作g = imfilter(f, w ...
- CMD命令提示符
mspaint 画图板 notepad 打开记事本 write 写字板 calc.exe 计算器 control.exe 控制面板 osk 打开屏幕键盘 rononce -p ----15 ...
- Dom样式操作-属性操作
1. 对样式进行操作: 1) 以样式(C1,C2等)为最小单位进行修改. className, classList, (以列表形式获得) classList.add("C2"), ...