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

题解:首先要知道 a^b=c 等价于 a=c^b;   我们用a[i]记录前I个数的异或和,然后离线处理所有区间(对于所有区间我们按L所在块为第一排序,该询问的r为第二排序,对所有询问区间排序);

对于新增加的一个数我们加上前区间异或等于k^s的数的个数,然后更新一下异或为s的数量,对于一个需要去掉的数,我需要先新更新这个数的数量,然后减去k^s的数量即可;

离线跑一遍,

参考代码为:

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int Max = ;
const int MAXM = <<;
struct Point
{
int L ,R,Id;
} a[Max]; LL sum[Max],ans[Max],k;
int n,m,L,R;
LL cnt[MAXM],ant;
bool cmp(Point b,Point c)
{
return b.L/==c.L/? b.R<c.R:b.L<c.L;
}
void Dec(LL s)
{
--cnt[s];
ant-=cnt[s^k];
}
void Inc(LL s)
{
ant += cnt[s^k];
cnt[s]++;
}
int main()
{
scanf("%d %d %lld",&n,&m,&k);
LL data;
for(int i=;i<=n;i++) scanf("%lld",&sum[i]),sum[i]^=sum[i-];
for(int i=;i<=m;i++) scanf("%d %d",&a[i].L,&a[i].R),a[i].Id = i,a[i].L--;
sort(a+,a+m+,cmp);
L=,R=,cnt[]=,ant=;
for(int i=;i<=m;i++)
{
while(R<a[i].R) Inc(sum[++R]);
while(R>a[i].R) Dec(sum[R--]);
while(L<a[i].L) Dec(sum[L++]);
while(L>a[i].L) Inc(sum[--L]);
ans[a[i].Id]=ant;
}
for(int i=;i<=m;i++) printf("%lld\n",ans[i]);
return ;
}

CodeForces-617E XOR And Favorite Numbers(莫队)的更多相关文章

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

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

  2. Codeforces 617E XOR and Favorite Number莫队

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

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

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

  4. XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)

    题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...

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

  6. Codeforces - 617E 年轻人的第一道莫队

    我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧 cnt[i]表示i出现的次数 maxn开2e6比较保险 /*H E A D*/ struct Query{ int l,r,id; ...

  7. Codeforces - 617E 年轻人的第一道莫队·改

    题意:给出\(n,m,k,a[1...n]\),对于每次询问,求\([l,r]\)中\(a[i] \ xor \ a[i+1] \ xor \ ...a[j],l<=i<=j<=r\ ...

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

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

随机推荐

  1. element - ui tree

    一行代码两行泪,没有外网真可怕!好久没写博客了,更新一把. <template> <div> <el-tree :data="data" :props ...

  2. springboot封装JsonUtil,CookieUtil工具类

    springboot封装JsonUtil,CookieUtil工具类 yls 2019-9-23 JsonUtil public class JsonUtil { private static Obj ...

  3. Python3.7.1学习(七)mysql中pymysql模块详解(一)

    pymysql是纯用Python操作MySQL的模块,其使用方法和MySQLdb几乎相同.此次介绍mysql以及在python中如何用pymysql操作数据库, 以及在mysql中存储过程, 触发器以 ...

  4. 插入订单并且输出订单号的sql存储过程

    --插入订单-- create proc InsertOrders ( @OrderNumber varchar(300), @OrderState varchar(30), @OrderType v ...

  5. ArcGIS API For Javascript :如何制作地图切换器

    大部分情况下我们开发会使用原生的地图切换器,由于每个项目的页面风格不同,业务场景不同,因此需要做一些样式不同的地图切换器. 首先可以照猫画虎,自己照着地图切换器的样式抄一个,或者看看主流的地图切换器都 ...

  6. [springboot 开发单体web shop] 8. 商品详情&评价展示

    上文回顾 上节 我们实现了根据搜索关键词查询商品列表和根据商品分类查询,并且使用到了mybatis-pagehelper插件,讲解了如何使用插件来帮助我们快速实现分页数据查询.本文我们将继续开发商品详 ...

  7. FIddler+Proxifer工具对windows PC客户端进行抓包

    python的大火,带动了python爬虫. 爬虫就必定绕不开抓包. 目前最常见的就是网页抓包了,可以使用chrome进行,或者配合其他抓包软件 fiddler. 小程序有些兴起是,如跳一跳之类的,也 ...

  8. 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势

    文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...

  9. 程序员用于机器学习编程的Python 数据处理库 pandas 入门教程

    入门介绍 pandas适合于许多不同类型的数据,包括: · 具有异构类型列的表格数据,例如SQL表格或Excel数据 · 有序和无序(不一定是固定频率)时间序列数据. · 具有行列标签的任意矩阵数据( ...

  10. Nginx+SpringBoot实现负载均衡

    前言 在上一篇中介绍了Nginx的安装,本篇文章主要介绍的是Nginx如何实现负载均衡. 负载均衡介绍 介绍 在介绍Nginx的负载均衡实现之前,先简单的说下负载均衡的分类,主要分为硬件负载均衡和软件 ...