You've got an array a, consisting of n integers a1, a2, ..., an. You are allowed to perform two operations on this array:
- Calculate the sum of current array elements on the segment [l, r], that is, count value al + al + 1 + ... + ar.
- Apply the xor operation with a given number x to each array element on the segment [l, r], that is, execute
. This operation changes exactly r - l + 1 array elements.
Expression means applying bitwise xor operation to numbers x and y. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as "^", in Pascal — as "xor".
You've got a list of m operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an(0 ≤ ai ≤ 106) — the original array.
The third line contains integer m (1 ≤ m ≤ 5·104) — the number of operations with the array. The i-th of the following m lines first contains an integer ti (1 ≤ ti ≤ 2) — the type of the i-th query. If ti = 1, then this is the query of the sum, if ti = 2, then this is the query to change array elements. If the i-th operation is of type 1, then next follow two integers li, ri (1 ≤ li ≤ ri ≤ n). If the i-th operation is of type 2, then next follow three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106). The numbers on the lines are separated by single spaces.
Output
For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input.
两个操作
1:求[ L,R ] 的和;
2:[ L,R ] 的每一个数a[ i ] ^ x;
关键是加法运算对异或运算不满足分配律,就是a^c+b^c != (a+b)^c;
怎么办了?那就把a二进制化,维护区间每个二进制位的数目;
当x的某一位为1,区间中所有a[ i ]所有为1的要变为0,为0的变为1,然而当其为0时,a[i]对应位是0或者1对答案均无影响 ;
所以维护区间中每个二进制位上1的数目即可,最后按权加和答案就行了;
异或和会爆int;
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int MOD=1e9+7;
const int maxn=1e5+7;
const double ESP=1e-8;
int n;
int a[maxn];
struct node
{
int dig[25];///每一位1的数目
int lazy;
}b[maxn<<2];
void up(int rt)
{
for(int i=0;i<22;++i)
b[rt].dig[i]=b[rt<<1].dig[i]+b[rt<<1|1].dig[i];
}
void down(int l,int r,int rt)
{
if(b[rt].lazy)
{
b[rt<<1].lazy^=b[rt].lazy;
b[rt<<1|1].lazy^=b[rt].lazy;
int mid=(l+r)>>1;
for(int i=0;i<22;++i)
{
if(b[rt].lazy&1<<i)
{
b[rt<<1].dig[i]=mid-l+1-b[rt<<1].dig[i];
b[rt<<1|1].dig[i]=r-mid-b[rt<<1|1].dig[i];
}
}
b[rt].lazy=0;
}
}
void build(int l,int r,int rt)
{
b[rt].lazy=0;
if(l==r)
{
for(int i=0;i<22;++i)
if(a[l]&1<<i) b[rt].dig[i]=1;
else b[rt].dig[i]=0;
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
up(rt);
}
void change(int l,int r,int rt,int ll,int rr,int x)
{
if(ll<=l&&r<=rr)
{
b[rt].lazy^=x;
for(int i=0;i<22;++i)
if(x&1<<i) b[rt].dig[i]=r-l+1-b[rt].dig[i];
return ;
}
down(l,r,rt);
int mid=(l+r)>>1;
if(ll<=mid) change(l,mid,rt<<1,ll,rr,x);
if(mid<rr) change(mid+1,r,rt<<1|1,ll,rr,x);
up(rt);
}
long long query(int l,int r,int rt,int ll,int rr)
{
long long res=0;
if(ll<=l&&r<=rr)
{
for(int i=0;i<22;++i)
res+=(long long)b[rt].dig[i]*(1<<i);
return res;
}
down(l,r,rt);
int mid=(l+r)>>1;
if(ll<=mid) res+=query(l,mid,rt<<1,ll,rr);
if(mid<rr) res+=query(mid+1,r,rt<<1|1,ll,rr);
up(rt);
return res;
}
int q;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
scanf("%d",&q);
int op,l,r,x;
build(1,n,1);
while(q--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d",&l,&r);
cout<<query(1,n,1,l,r)<<endl;
}
else
{
scanf("%d%d%d",&l,&r,&x);
change(1,n,1,l,r,x);
}
}
}