树状数组扩展(异或求和)

题目:逃票的chanming

题意:操作0: 读入p,q,v,并且a[p] ^= v, a[p + 1] ^= v, .. ,a[q] ^= v; 操作1:  读入p,q,输出s = a[p] ^ a[p + 1] ^ a[p + 2]..^a[q]的结果;

思路:

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define For(i,a) for(i=0;i=b;i--)
#define clr(ar,vel) memset(ar,vel,sizeof(ar))
#define PB push_back
#define maxint 0x7fffffff

#define maxn 500010


template 
class BIT{
    long long c[SZ+10];
public :
    void clear(){clr(c,0);}
    long long getsum(int x){
        long long s=0;
        while(x>0)
            s^=c[x], x-=x&-x;
        return s;
    }
    void update(int x, int n){
        while(x<=SZ)
            c[x]^=n,x+=x&-x;
    }
};

BIT  sum, sumi;

int main(){
	int n, m;
	int act, s, t, v;
//	for(int i = 0; i < 10; i ++) cout << (i&1) << endl;
	scanf("%d%d",&n,&m);
	for(int i = 0; i < m; i ++) {
		scanf("%d%d%d",&act,&s,&t);
		if( !act ) {
			scanf("%d",&v);
			sum.update(s,v);
			sumi.update(s,v*(s&1));
			sum.update(t+1,v);
			sumi.update(t+1,v*((t+1)&1));
			
		}
		else{
//			cout << sum.getsum(t) << ' ' <<  sumi.getsum(t) << endl; 
			int w = ((t+1)&1)*sum.getsum(t)^sumi.getsum(t);
//			cout << w << endl;
			w ^= (s&1)*sum.getsum(s-1)^sumi.getsum(s-1);
			printf("%d\n",w);
		}
	}
	return 0;
}


你可能感兴趣的:(树状数组,线段树)