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

题目:逃票的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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <string>
using namespace std;
#define For(i,a) for(i=0;i<a;i++)
#define Foru(i,a,b) for(i=a;i<=b;i++)
#define Ford(i,a,b) for(i=a;i>=b;i--)
#define clr(ar,vel) memset(ar,vel,sizeof(ar))
#define PB push_back
#define maxint 0x7fffffff

#define maxn 500010


template <int SZ>
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 <maxn> 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值