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, ..., ajis 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 31 2 1 1 0 31 63 5

Output
70

Input
5 3 11 1 1 1 11 52 41 3

Output
944

题意:询问区间内异或和刚好为k的字段个数。思路:莫队+前缀和。这个前缀和比较套路,用的是前缀异或和。字段【l,r】的异或和就是pre[r]^pre[l-1],这种情况下我们在莫队的过程中记录l,r的pre[i]出现的次数,就可以完成更新了。

注意当L<q[i].l时,要先让记录per[L]出现次数的个数减一。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int maxm = 1000086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int num[maxm*2],pre[maxn],a[maxn];struct node{int l,r;int id;
}q[maxn];
ll ans[maxn];
ll anss;
int block;bool cmp(node a,node b){if(a.l/block!=b.l/block){return a.l<b.l;}return a.r<b.r;
}int main()
{int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=n;i++){scanf("%d",&a[i]);pre[i]=pre[i-1]^a[i];}block=sqrt(n);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}sort(q+1,q+1+m,cmp);int L=1,R=0;anss=0;num[0]=1;for(int i=1;i<=m;i++){while(L<q[i].l){int t=k^pre[L-1];num[pre[L-1]]--;///注意语句顺序anss-=num[t];L++;}while(R>q[i].r){int t=k^pre[R];num[pre[R]]--;anss-=num[t];R--;}while(L>q[i].l){L--;int t=k^pre[L-1];anss+=num[t];num[pre[L-1]]++;}while(R<q[i].r){R++;int t=k^pre[R];anss+=num[t];num[pre[R]]++;}ans[q[i].id]=anss;}for(int i=1;i<=m;i++){printf("%lld\n",ans[i]);}return 0;
}

View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/10863851.html

CodeForces - 617E XOR and Favorite Number (莫队+前缀和)相关推荐

  1. codeforces 617E XOR and Favorite Number 莫队

    https://vjudge.net/problem/CodeForces-617E 题目大意:给nnn个数,mmm个询问,以及一个数kkk,每次询问要输出[l,r][l,r][l,r]内满足a[i] ...

  2. CodeForces - 617E XOR and Favorite Number(莫队)

    题目链接:点击查看 题目大意:给出一个由n个数组成的数列,现在给出m组询问,每次询问包含一个l和一个r,要求回答在闭区间[l,r]中有多少组(i,j)满足[i,j]闭区间内的所有数的异或和等于k 题目 ...

  3. CodeForces -617E XOR and Favorite Number(莫队)

    题目链接:点击这里 题目大意: 给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1​,a2​,...,an​ ,再给出一个数字 kkk , mmm 组询问每组询 ...

  4. Codeforces 617E XOR and Favorite Number

    Discription Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Eac ...

  5. 莫队算法(普通莫队、带修莫队、树上莫队、不删除莫队)学习笔记【理解+套路/核心代码+例题及题解】

    一.理解 我的理解就是巧妙的暴力,利用双指针以及分块思想,巧妙的移动双指针,时间复杂度可以达到O(NlogN). 强推博客:写的又好又全.链接 二.套路 1.普通莫队 [1]核心代码 bool cmp ...

  6. Codeforces617E XOR and Favorite Number (普通莫队)

    题目链接: XOR and Favorite Number 2021.11.11更新了解题思路和代码 大致题意 给定一个长度为nnn的序列aaa, 和一个固定的数值kkk. 有mmm次询问, 每次询问 ...

  7. XOR and Favorite Number CF340E 莫队算法

    题目链接 题意:求给定询问区间[L,R]问有多少连续区间异或值等k,多次询问可以离线. a[i]^a[i+1]^a[i+2]^a[n]=(a[1]^a[2]^a[3]^...^a[i-1])^(a[1 ...

  8. CodeForces 617 E. XOR and Favorite Number

    题目链接: CodeForces 617 E. XOR and Favorite Number 题目描述: 给出n个数,m次查询,每次查询在区间[l, r]里面有多少对(i, j),满足ai ^ ai ...

  9. CF 617E 莫队

    CF 617E 题目链接: http://codeforces.com/problemset/problem/617/E 题意: 问[L,R]区间内,有多少个子区间,它们里元素的异或和为k. 思路: ...

最新文章

  1. Eclipse相关问题总结
  2. 打印系列一:(套打解决方案(支持B/S和C/S))
  3. 电信诈骗?一招让骗子血本无归!
  4. android如何不用系统签名,更新Android系统应用程序,带/不带平台签名
  5. Leetcode每日一题:514.freedom-trail(自由之路)
  6. 21天Jmeter打卡Day12 逻辑控制器之循环控制器
  7. linux下默认分区system的id,Linux学习笔记之系统中的分区和文件系统
  8. C语言解决约瑟夫环问题
  9. 如何修改SAO用户密码
  10. Siamese系列跟踪网络之SiamFC、SiamRPN、DaSiamRPN、SiamRPN++、SiamMask
  11. [算法·思想]囚徒困境中Tit For Tat策略为什么就是最好的,之JAVA代码模拟实现
  12. AI 工程师进阶路线
  13. Sparkling Logic SMARTS 实时决策分析模块介绍
  14. Mybatis逆向工程同名表找错数据库的问题
  15. 石墨文档支持的几种markdown格式
  16. BlueHost和SiteGround美国主机商对比评测
  17. 好用的十六进制编辑工具010 Editor
  18. 基于有源钳位三电平的有源电力滤波器(ANPC-APF)MATLAB仿真,包括自建的DSOGI锁相模块和PQ谐波检测模块
  19. while循环+无限循环.py
  20. 灵汐科技携类脑芯片KA200及类脑系列产品亮相国家“十三五”科技创新成就展

热门文章

  1. callbackend entry point - iwfndcl_mgw_request_manager~read_entity
  2. Kubernetes里ingress配置的一些例子
  3. SAP S/4HANA CDS View的访问控制实现:DCL介绍 1
  4. ABAP Development Tools的语法高亮实现原理
  5. 在SAP UI中使用纯JavaScript显示产品主数据的3D模型视图
  6. Netweaver和CloudFoundry里的trace开关
  7. antd 获取table选中行数据_Excel数据匹配:函数法+PQ法+PB法
  8. php网站搭建致命错误203终止,PHP致命错误:无法重新分配$this
  9. oracle一个月内的数据,oracle 查询前一小时、一天、一个月、一年的数据
  10. 线程id 获取线程名称_016 线程及初步网络编程