XOR and Favorite Number(CF-617E)
Problem Description
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 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.
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 次詢問,以及一個數 k,每次詢問給出一個區間 [l,r],求這個區間內有多少個子區間的所有數異或為 k
思路:
問題實質是求區間 [l,r] 內異或值等于 k 的個數,可轉為前綴和來做
在輸入過程中令?a[i]^=a[i-1],那么在區間 [l,r] 內,所有數的異或=k 時,有:a[r]^k=a[l-1]
最后再用莫隊處理區間,分塊枚舉即可
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=10007; const int N=2000000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std;struct Node{int l,r;//詢問的左右端點int id;//詢問的編號 }q[N]; int n,m,k,a[N]; int block;//分塊 LL ans,cnt[N]; LL res[N];bool cmp(Node a,Node b){//奇偶性排序return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r); }void add(int x){//統計新的ans+=cnt[a[x]^k];cnt[a[x]]++; } void del(int x){//減去舊的cnt[a[x]]--;ans-=cnt[a[x]^k]; } int main(){while(scanf("%d%d%d",&n,&m,&k)!=EOF){a[0]=0;for(int i=1;i<=n;++i){scanf("%d",&a[i]);a[i]=a[i-1]^a[i];}for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}memset(cnt,0,sizeof(cnt));ans=0;cnt[0]=1;block=sqrt(m*2/3*1.0);//分塊,卡常數sort(q+1,q+m+1,cmp);//對詢問進行排序int l=1,r=0;//左右指針for(int i=1;i<=m;i++){int ql=q[i].l,qr=q[i].r;//詢問的左右端點while(l>ql) {l--;add(l-1);}//[l-1,r]while(l<ql) {del(l-1);l++;}//[l+1,r]while(r<qr) add(++r);//[l,r+1]while(r>qr) del(r--);//[l,r-1]res[q[i].id]=ans;//獲取答案}for(int i=1;i<=m;i++)printf("%lld\n",res[i]);}return 0; }?
總結
以上是生活随笔為你收集整理的XOR and Favorite Number(CF-617E)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 和为k的倍数(51Nod-2522)
- 下一篇: 理论基础 —— 二叉树 —— 三叉链表