首页 > 学院 > 开发设计 > 正文

codeforces 617E. XOR and Favorite Number (莫队)

2019-11-08 00:58:22
字体:
来源:转载
供稿:网友

E. XOR and Favorite Numbertime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

Examplesinput
6 2 31 2 1 1 0 31 63 5output
70input
5 3 11 1 1 1 11 52 41 3output
944Note

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.

题解:莫队

cnt[i]表示异或前缀和为i的数的个数,那么答案就是cnt[i]*cnt[i^k]

k=0的时候需要特判,cnt[i]*(cnt[i]-1)/2

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#define N 1000003#define LL long long using namespace std;struct data{	int l,r,id;}q[N];int n,m,k,a[N],cnt[N*10],c[N],belong[N];LL ans[N],sum;int cmp(data a,data b){	return belong[a.l]<belong[b.l]||belong[a.l]==belong[b.l]&&a.r<b.r;}void change(int x,int val){	int t=c[x];	if (k) sum-=(LL)cnt[t]*cnt[k^t];	else sum-=(LL)cnt[t]*(cnt[k^t]-1)/2;	cnt[t]+=val;	if (k) sum+=(LL)cnt[t]*cnt[k^t];	else sum+=(LL)cnt[t]*(cnt[k^t]-1)/2;}int main(){	freopen("a.in","r",stdin);	scanf("%d%d%d",&n,&m,&k);	for (int i=1;i<=n;i++) 	 scanf("%d",&a[i]),c[i]=c[i-1]^a[i];	for (int i=1;i<=m;i++) {	 scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;	 q[i].l-=1;    }	int block=ceil(sqrt(n));	for (int i=1;i<=n;i++) belong[i]=(i-1)/block+1;	sort(q+1,q+m+1,cmp);	int l=0,r=0; change(0,1);	for (int i=1;i<=m;i++) {		while (q[i].l<l) change(--l,1);		while (q[i].l>l) change(l++,-1);		while (q[i].r>r) change(++r,1);		while (q[i].r<r) change(r--,-1);		ans[q[i].id]=sum;	}	for (int i=1;i<=m;i++)	 printf("%I64d/n",ans[i]); } 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表