XOR and Favorite Number (莫对算法)
4 seconds
256 megabytes
standard input
standard output
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such thatl ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.
The first line of the input contains integers n, m and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.
The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.
Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.
Print m lines, answer the queries in the order they appear in the input.
6 2 3
1 2 1 1 0 3
1 6
3 5
7
0
5 3 1
1 1 1 1 1
1 5
2 4
1 3
9
4
4
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5,6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length.
题意:有n个数和m次询问,每一询问会有一个L和R,表示所询问的区间,
问在这个区间中有多少个连续的子区间的亦或和为k
假设我们现在有一个前缀异或和数组sum[],现在我们要求区间[L,R]的异或的值,
用sum数组表示就是sum[L-1]^sum[R]==K,或者说是K^sum[R]==sum[L-1]
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e6 + ;
int n, m, k, L, R, sz, a[maxn];
LL sum[maxn], ans, ANS[maxn];
struct node {
int l, r, id;
node() {}
node(int l, int r, int id): l(l), r(r), id(id) {}
bool operator <(const node & a)const {
if (l / sz == a.l / sz) return r < a.r;
return l < a.l;
}
} qu[maxn];
void add(int x) {
ans += sum[a[x] ^ k];
sum[a[x]]++;
}
void del(int x) {
sum[a[x]]--;
ans -= sum[a[x] ^ k];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n ; i++) {
scanf("%d", &a[i]);
a[i] ^= a[i - ];
}
for (int i = ; i <= m ; i++) {
scanf("%d%d", &qu[i].l, &qu[i].r);
qu[i].l--;
qu[i].id = i;
}
sz = (int)sqrt(n);
sort(qu + , qu + m + );
L = , R = ;
for (int i = ; i <= m ; i++) {
while(L > qu[i].l) add(--L);
while(R < qu[i].r) add(++R);
while(L < qu[i].l) del(L++);
while(R > qu[i].r) del(R--);
ANS[qu[i].id] = ans;
}
for (int i = ; i <= m ; i++)
printf("%lld\n", ANS[i]);
return ;
}
XOR and Favorite Number (莫对算法)的更多相关文章
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 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 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 ...
- 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莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- 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 ...
- E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)
一直都说学莫队,直到现在才学,训练的时候就跪了 T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- Codeforces Round #340 (Div. 2) E XOR and Favorite Number 莫队板子
#include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...
随机推荐
- 【WXS数据类型】Number
Number包括整数与小数. 属性: 名称 返回 说明 [Number].constructor 值为字符串“Number” 返回该类型的结构字符串 方法: 原型:[Number].toString( ...
- JDK源码分析:Integer.java部分源码解析
1)声明部: public final class Integer extends Number implements Comparable<Integer> extends Number ...
- java学习笔记-9.违例差错控制
1.违例规范是告诉程序员这个方法可能抛出哪些类型的异常.他的格式在方法声明中,位于自变量(参数)列表的后面,如void f() throws tooBig, tooSmall, divZero { ...
- 加密SecurityHelper
接下来给大家分享一下我用的加密helper,现在只用的md5加密的方法,网上很多方法找到的时候加密完了会变成乱码,这样对于密码这种字段保存的时候就会出错.其实只需要把加密完的byte字节转化成16位就 ...
- php多进程单例模式下的 MySQL及Redis连接错误修复
前几天写了个php常驻脚本,主要逻辑如下 //跑完数据后休息60秒 $sleepTime = 60; $maxWorker = 10; while (true) { $htmlModel = new ...
- ide的tomcat的部署和配置
关于intellij ide的tomcat的部署和配置 1.下载zip版的Tomcat 7,并解压.下载地址 2.在IDEA中配置Tomcat 7 在idea中的Settings(Ctrl+Alt ...
- 软工2017第六周团队协作——个人PSP
10.20 --10.26本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间 结束时间 中断时间 实际用 ...
- Thunder团队第一周 - Scrum会议2
Scrum会议2 小组名称:Thunder 项目名称:待定 Scrum Master:李传康 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康(M ...
- iOS单利创建的方法
我们在使用单例的时候有两种方法@synchronized,GCD,往往人们使用@synchronized,但是推荐使用GCD: 第一种(@synchronized): + (id)sharedInst ...
- Zigbee安全基础篇Part.1
原文地址: https://www.4hou.com/wireless/14211.html 导语:ZigBee是一种开源无线技术,用于低功耗嵌入式设备(无线电系统).本文探讨了ZigBee协议的可用 ...