版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxh759151483/article/details/81530388
已知一个长度为n的整数数列a1,a2,…,an,给定查询参数l、r,问在al,al+1,…,ar区间内,有多少子序列满足异或和等于k。也就是说,对于所有的x,y(l≤x≤y≤r),满足ax⊕ax+1⊕⋯⊕ay=k的x,y有多少组。
输入
输入第一行为3个整数n,m,k。第二行为空格分开的n个整数,即a1,a2,…,an。接下来m行,每行两个整数lj,rj,代表一次查询。
输出
输出共m行,对应每个查询的计算结果。
样例输入
4 5 1 1 2 3 1 1 4 1 3 2 3 2 4 4 4
样例输出
扫描二维码关注公众号,回复:
3477231 查看本文章
4 2 1 2 1
提示
对于30%的数据,1≤n,m≤1000。
对于100%的数据,1≤n,m≤105,0≤k,ai≤105,1≤lj≤rj≤n。
先用前缀数组记录一下前i项的异或,如果sum^k在前面出现过t次代表前面有t个异或和为k的子序列。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
int n, m, a[N], Be[N], ans[N], res, vis[N], k;
struct node{
int l, r, id;
}q[N];
bool cmp(node a, node b){
return Be[a.l] == Be[b.l] ? a.r < b.r : a.l < b.l;
}
void init(){
int unit = sqrt(n);
for(int i = 1; i <= n; i++)
Be[i] = i / unit + 1;
}
void del(int x){
res -= vis[a[x] ^ k];
vis[a[x]]--;
}
void add(int x){
vis[a[x]]++;
res += vis[a[x] ^ k];
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
init();
a[0] = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
a[i] = a[i] ^ a[i - 1];
}
for(int i = 0; i < m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].l--;
q[i].id = i;
}
sort(q, q + m, cmp);
memset(vis, 0, sizeof(vis));
int l = 1, r = 0;
res = 0;
for(int i = 0; i < m; i++){
while (l < q[i].l) del(l++);
while (l > q[i].l) add(--l);
while (r < q[i].r) add(++r);
while (r > q[i].r) del(r--);
ans[q[i].id] = res;
}
for(int i = 0; i < m; i++)
printf("%d\n", ans[i]);
return 0;
}