Codeforces 242 E XOR on Segment 異或線段樹
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
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 a
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);
}
}
}