题目描述
Janez 喜欢橙子!他制造了一个橙子扫描仪,但是这个扫描仪对于扫描的每个橙子的图像只能输出一个 3232 位整数。
他一共扫描了 nn 个橙子,但有时他也会重新扫描一个橙子,导致这个橙子的 3232 位整数发生更新。
Janez 想要分析这些橙子,他觉得异或操作非常有趣,他每次选取一个区间从 ll 至 uu,他想要得到这个区间内所有子区间的异或和的异或和。
例如 l=2,u=4l=2,u=4 的情况,记橙子序列 AA 中第 ii 个橙子的整数是 ,那么他要求的就是:
a_2 \oplus a_3 \oplus a_4 \oplus (a_2\oplus a_3)\oplus(a_3\oplus a_4)\oplus(a_2\oplus a_3 \oplus a_4)a2⊕a3⊕a4⊕(a2⊕a3)⊕(a3⊕a4)⊕(a2⊕a3⊕a4)
注:式子中的 \oplus⊕ 代表按位异或运算。异或的运算规则如下。
对于两个数的第 ii 位,记为 x,yx,y,那么:
xx | yy | x\oplus yx⊕y |
---|---|---|
00 | 11 | 11 |
11 | 00 | 11 |
00 | 00 | 00 |
11 | 11 | 00 |
例:13\oplus 23=2613⊕23=26
13=13= | 0\cdots 0011010⋯001101 |
---|---|
23=23= | 0\cdots 0101110⋯010111 |
13\oplus 23=13⊕23= | 0\cdots 0110100⋯011010 |
输入格式
第一行输入两个正整数 n,qn,q,表示橙子数量和操作次数。
接下来一行 nn 个非负整数,表示每个橙子扫描得到的数值 ,从 11 开始编号。
接下来 qq 行,每行三个数:
-
如果第一个数是 11,接下来输入一个正整数 ii 与非负整数 jj,表示将第 ii 个橙子的扫描值 a_iai 修改为 jj。
-
如果第一个数是 22,接下来输入两个正整数 u,lu,l 表示询问这个区间的答案。
输出格式
对于每组询问,输出一行一个非负整数,表示所求的总异或和。
输入输出样例
输入 #1复制
3 3 1 2 3 2 1 3 1 1 3 2 1 3
输出 #1复制
2 0
输入 #2复制
5 6 1 2 3 4 5 2 1 3 1 1 3 2 1 5 2 4 4 1 1 1 2 4 4
输出 #2复制
2 5 4 4
说明/提示
输入输出样例 1 解释
-
最初,A=[1,2,3]A=[1,2,3],询问结果为 1\oplus 2\oplus 3\oplus(1\oplus 2)\oplus (2\oplus 3)\oplus(1\oplus 2\oplus 3)=21⊕2⊕3⊕(1⊕2)⊕(2⊕3)⊕(1⊕2⊕3)=2
-
修改后,第一个位置被修改为 33 ,询问的结果是 3\oplus 2\oplus 3\oplus(3\oplus 2)\oplus (2\oplus 3)\oplus(3\oplus 2\oplus 3)=03⊕2⊕3⊕(3⊕2)⊕(2⊕3)⊕(3⊕2⊕3)=0。
数据规模与约定:
本题采用多测试点捆绑测试,共有 5 个子任务。
- Subtask 1(12 points):1\le n,q\le 10^21≤n,q≤102,无特殊限制
- Subtask 2(18 points):1\le n,q\le 5\times 10^21≤n,q≤5×102,且没有修改操作。
- Subtask 3(25 points):1\le n,q\le 5\times 10^31≤n,q≤5×103,无特殊限制
- Subtask 4(20 points):1\le n,q\le 2\times 10^51≤n,q≤2×105,且没有修改操作。
- Subtask 5(25 points):1\le n,q\le 2\times 10^51≤n,q≤2×105,无特殊限制
对于所有数据,0\le a_i\le 10^9,1\le n,q\le 2\times 10^50≤ai≤109,1≤n,q≤2×105
说明
原题来自:eJOI2019 Problem A. XORanges
思路:首先我们要观察一下区间和子区间的异或值的关系:
如果是2~4,那么所有的子区间异或的柿子就是
2^3^4^(2^3)^(3^4)^(2^3^4)
2用了3次,3用了4次,4用了3次
根据异或的性质:a^a==0
那么只有2和4对最后的值的贡献是1
再换一个区间比如:2~5
异或的柿子是2^3^4^5^(2^3)^(3^4)^(4^5)^(2^3^4)^(3^4^5)^(2^3^4^5)
所有的数都用了偶数次
所以异或的结果是0
那么我们就可以得出结论:当区间的两端的数奇偶值不相同的时候异或就是0
相同的话只有奇偶性和区间两端的端点的奇偶性相同的数才有贡献
那么我们就创建一个结构体存一个奇数,一个偶数的异或和的树状数组
更新的时候,把之前常用的求和运算改为异或运算(因为求的是异或和)
把添加操作改为异或,异或的参数是i a[i]^x(因为异或自己会抵消自己的贡献),然后把a[i]改为x
/*
.----------------. .----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. || .--------------. |
| | ________ | || | _________ | || | ____ ____ | || | ____ | |
| | |_ ___ `. | || | |_ ___ | | || ||_ \ / _|| || | .' `. | |
| | | | `. \ | || | | |_ \_| | || | | \/ | | || | / .--. \ | |
| | | | | | | || | | _| _ | || | | |\ /| | | || | | | | | | |
| | _| |___.' / | || | _| |___/ | | || | _| |_\/_| |_ | || | \ `--' / | |
| | |________.' | || | |_________| | || ||_____||_____|| || | `.____.' | |
| | | || | | || | | || | | |
| '--------------' || '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '----------------' '----------------'
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b) {
return b? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
*/
const int N=2e5+10;
int m,n;
int a[N];
//
struct name{
int c[4*N];
void add(int x,int y){
for(int i=x;i<=n;i+=lowbit(i)){
c[i]^=y;
}
}
int sum(int x){
int res=0;
for(int i=x;i;i-=lowbit(i))res^=c[i];
return res;
}
}b[2];
void sove(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i&1].add(i,a[i]);
}
while(m--){
int op,i,j;
cin>>op>>i>>j;
if(op==1){
b[i&1].add(i,a[i]^j);
a[i]=j;
}else{
if((i&1)!=(j&1)){
cout<<0<<endl;
}else{
int op=b[i&1].sum(j)^b[i&1].sum(i-1);
cout<<op<<endl;
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie() ,cout.tie() ;
int t=1;
// cin>>t;
while(t--){
sove();
}
return 0;
}