题目链接:

E. XOR and Favorite Number

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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 that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.

Input

The first line of the input contains integers nm 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.

Output

Print m lines, answer the queries in the order they appear in the input.

Examples
input
6 2 3
1 2 1 1 0 3
1 6
3 5
output
7
0
input
5 3 1
1 1 1 1 1
1 5
2 4
1 3
output
9
4
4
Note

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]之间有多少对i和j满足a[i]^a[i+1]^...^a[j]=k;

思路:暴力绝对绝对绝对是不行的,所有就要用复杂度更低的算法啦,所以我就去学了莫队算法,莫队算法处理的是一种离线算法,是对询问进行分块排序来优化查询的算法;

这题还要对异或运算要了解;我要去写个位运算的小总结;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
struct node
{
friend bool operator< (node x,node y)
{
if(x.pos==y.pos)return x.r<y.r;
return x.l<y.l;
}
int l,r,id;
int pos;
};
node qu[N];
int n,m,k;
int b[N],num[*N];
long long ans[N];
void solve()
{
memset(num,,sizeof(num));
int le=,ri=;
long long temp=;
for(int i=;i<=m;i++)
{
while(ri<qu[i].r)
{
ri++;
temp+=num[b[ri]^k];//num[]数组是当前le到ri内每个b[i](le<=i<=ri)异或一个要添加的数==k的数目;所以num[b[ri]^k]就是在已有的区间上再加一个
num[b[ri]]++;//ri前能和ri异或==k的数目;后面这些操作一样
}
while(ri>qu[i].r)
{
num[b[ri]]--;
temp-=num[b[ri]^k];
ri--;
}
while(le>qu[i].l-)
{
le--;
temp+=num[b[le]^k];
num[b[le]]++;
}
while(le<qu[i].l-)
{
num[b[le]]--;
temp-=num[b[le]^k];
le++;
}
ans[qu[i].id]=temp;
}
}
int main()
{
b[]=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&b[i]);
b[i]^=b[i-];
}
int s=sqrt(n);
for(int i=;i<=m;i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
qu[i].pos=qu[i].l/s;
}
sort(qu+,qu+m+);
solve();
for(int i=;i<=m;i++)
{
cout<<ans[i]<<"\n";
}
return ;
}

codeforces 617E E. XOR and Favorite Number(莫队算法)的更多相关文章

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

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

  3. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  4. CodeForces - 617E XOR and Favorite Number 莫队算法

    https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry,  问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...

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

    http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...

  6. Codeforces 617E XOR and Favorite Number莫队

    http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...

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

  8. E. XOR and Favorite Number 莫队 2038: [2009国家集训队]小Z的袜子(hose)

    一直都说学莫队,直到现在才学,训练的时候就跪了   T_T,其实挺简单的感觉.其实训练的时候也看懂了,一知半解,就想着先敲.(其实这样是不好的,应该弄懂再敲,以后要养成这个习惯) 前缀异或也很快想出来 ...

  9. codeforces 617E. XOR and Favorite Number 莫队

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

随机推荐

  1. jvm 调优参数

    -server -Xms6000M -Xmx6000M -Xmn500M -XX:PermSize=500M -XX:MaxPermSize=500M -XX:SurvivorRatio=65536 ...

  2. android Notification 的使用(锁定通知栏)

    近期一直在研究 android .并一边研究一边做应用.当中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ一样)的问题.经过研究实现了其功能 ...

  3. C语言基础知识【循环】

    C 循环1.有的时候,我们可能需要多次执行同一块代码.一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推.编程语言提供了更为复杂执行路径的多种控制结构.循环语句允许我 ...

  4. lucas定理证明

    Lucas 定理(证明) A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]. 则组合数C(A,B)与C(a[n],b[n])* ...

  5. poj1845(二分快速求等比数列模M和)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17039   Accepted: 4280 Descripti ...

  6. ubuntu16.04下hive安装与配置

    Hive是什么? 由Facebook开源用于解决海量 结构化日志的数据统计: Hive是基于Hadoop的一个 数据仓库工具,可以将结构化的数据文件映射 成一张表,并提供类SQL查询功能: 构建在Ha ...

  7. 算法调参 weight_ratio, weight_seqratio

    from openpyxl import Workbook import xlrd import time import Levenshtein as Le target_city_list = [' ...

  8. Java 迭代器

    1.快速报错 public class FailFast { public static void main(String[] args) { List<String> c = new A ...

  9. JDBC【菜鸟学JAVA】

    1:首先下载sqljdbc.jar,然后配置ClassPath,然后再在工程文件中把这个(sqljdbc.jar)架包引用上,就可以开始JAVA操作之旅了 打开Eclipse,“文件”→“新建”→“项 ...

  10. python3函数内全局变量使用global

    def p_num(): global num num = 10 print (num) num = 5 p_num() print(num)