这题的推导请看 其实就是网络赛的那题的弱化版
(感谢这位大佬的博客)
也可以打个表看看,虽然打表的话有点难看出规律
然后很明显, 查询的区间长度为偶数的时候,答案永远是0,如果是奇数,那么查询的区间左端点和右端点一定是奇偶性相同。
答案就是 下标间隔2的数进行异或。
那么我们就可以维护两个树状数组,分别表示下标为奇数的数和下标为偶数的数的前缀异或和。
然后注意一下细节
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x) x&-x;
#define debugint(name,x) printf("%s: %d\n",name,x);
#define debugstring(name,x) printf("%s: %s\n",name,x);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int _,n,m,a[maxn],b[maxn];
int c1[maxn],c2[maxn];
int t1,t2;
void update(int x,int val,int c[]){
while(x < maxn){
c[x] ^= val;
x += lowbit(x);
}
}
int query(int x,int c[]){
int res = 0;
while(x > 0){
res ^= c[x];
x -= lowbit(x);
}
return res;
}
int main() {
int ca = 1;
for(scanf("%d",&_);_;_--){
scanf("%d%d",&n,&m);
int t1 = 0,t2 = 0,x;
for(int i = 1; i <= n; i++){
scanf("%d",&x);
if(i&1) a[++t1] = x,update(t1,x,c1);
else b[++t2] = x,update(t2,x,c2);
}
/* build1(1,t1,1);
for(int i = 1; i <= t1; i++)
printf("%d ",sum1[i]);
build2(1,t2,1);*/
printf("Case #%d:\n",ca++);
while(m--){
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op == 0){
if(l&1){
l = (l+1)/2;
r = query(l,c1)^query(l-1,c1)^r;
update(l,r,c1);
}
else{
l = l/2;
r = query(l,c2)^query(l-1,c2)^r;
update(l,r,c2);
}
}else{
if((r-l+1)%2==0) puts("0");
else{
if(l&1)
printf("%d\n",query((l+1)/2-1,c1)^query((r+1)/2,c1));
else
printf("%d\n",query(l/2-1,c2)^query(r/2,c2));
}
}
}
}
}