CoderForces-617B
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.
InputThe 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.
Print m lines, answer the queries in the order they appear in the input.
6 2 3 1 2 1 1 0 3 1 6 3 5
7 0
5 3 1 1 1 1 1 1 1 5 2 4 1 3
9 4 4
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.
题意:这道题意思为:给你一行数,让你查找在这行数中有多少子区间满足其内的数亦或为K;
题解:可以让sum[i]表示从1亦或到i的值;sum[i]^=sum[i-1];
sum[r]=k^sum[l-1];(l,r分别表示左右端点)
AC代码为:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 4;
struct node
{
friend bool operator< (node x, node y)
{
if (x.pos == y.pos)return x.r<y.r;
return x.l<y.l;
}
int l, r, id;
int pos;
};
node qu[N];
int n, m, k;
int b[N], num[2 * N];
long long ans[N];
void solve()
{
memset(num, 0, sizeof(num));
int le = 1, ri = 0;
long long temp = 0;
for (int i = 1; i <= m; i++)
{
while (ri<qu[i].r)
{
ri++;
temp += num[b[ri] ^ k];
num[b[ri]]++;
}
while (ri>qu[i].r)
{
num[b[ri]]--;
temp -= num[b[ri] ^ k];
ri--;
}
while (le>qu[i].l - 1)
{
le--;
temp += num[b[le] ^ k];
num[b[le]]++;
}
while (le<qu[i].l - 1)
{
num[b[le]]--;
temp -= num[b[le] ^ k];
le++;
}
ans[qu[i].id] = temp;
}
}
int main()
{
b[0] = 0;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
{
scanf("%d", &b[i]);
b[i] ^= b[i - 1];
}
int s = sqrt(n);
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &qu[i].l, &qu[i].r);
qu[i].id = i;
qu[i].pos = qu[i].l / s;
}
sort(qu + 1, qu + m + 1);
solve();
for (int i = 1; i <= m; i++)
{
cout << ans[i] << "\n";
}
return 0;
}