CF242E XOR on Segment(二进制拆分+线段树)

link

思路:

两种操作,求和与异或。
考虑将数拆成二进制,维护每一位的值。
用线段树进行区间修改和区间求和。
空间卡的很紧,注意求和用long long存

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

inline void out(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) out(x / 10);
	putchar(x % 10 + '0');
}

inline void write(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
	puts("");
}

#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=4e5+100;

struct node{
	int l,r,laz;
	ll sum;
}tr[100010*4][22];

int n,m,a[100010],idx;
ll ans,ans2;
void pushup(ll u,ll k){
	tr[u][k].sum=tr[u<<1][k].sum+tr[u<<1|1][k].sum;
}

void pushdown(ll u,ll k){
	if(tr[u][k].laz){
		ll p=tr[u][k].laz;
		tr[u<<1][k].laz+=p;tr[u<<1|1][k].laz+=p;
		if(p%2){
			tr[u<<1][k].sum=(tr[u<<1][k].r-tr[u<<1][k].l+1)-tr[u<<1][k].sum;
			tr[u<<1|1][k].sum=(tr[u<<1|1][k].r-tr[u<<1|1][k].l+1)-tr[u<<1|1][k].sum;
		}
		tr[u][k].laz=0;
	}
}

void build(ll u,ll l,ll r){
	rep(k,1,21) tr[u][k].l=l,tr[u][k].r=r;
	if(l==r){
		rep(k,1,21)
			if(a[l]&(1<<(k-1))) tr[u][k].sum=1;
		return ;
	}
	ll mid=(l+r)/2;
	build(u<<1,l,mid);build(u<<1|1,mid+1,r);
	rep(k,1,21)
		pushup(u,k);
}

void query(ll u,ll k,ll l,ll r){
	if(tr[u][k].l>=l&&tr[u][k].r<=r){
		ans+=tr[u][k].sum<<(k-1);
		return ;
	}
	ll mid=(tr[u][k].l+tr[u][k].r)/2;
	pushdown(u,k);
	if(mid>=l) query(u<<1,k,l,r);
	if(mid<r) query(u<<1|1,k,l,r);
	pushup(u,k);
}

void update(ll u,ll k,ll l,ll r){
	if(tr[u][k].l>=l&&tr[u][k].r<=r){
		tr[u][k].sum=tr[u][k].r-tr[u][k].l+1-tr[u][k].sum;
		tr[u][k].laz++;
		return ;
	}
	ll mid=(tr[u][k].l+tr[u][k].r)/2;
	pushdown(u,k);
	if(mid>=l) update(u<<1,k,l,r);
	if(mid<r) update(u<<1|1,k,l,r);
	pushup(u,k);
}

int main(){
	n=read;
	rep(i,1,n) a[i]=read;
	build(1,1,n);
	m=read;
	while(m--){
		ll op=read,x=read,y=read;
		if(op==1){
			ans=0;
			for(int k=1;k<=21;k++){
				ans2=0;
				query(1,k,x,y);
				ans+=ans2;
			}
			cout<<ans<<endl;
		}
		else{
			idx=read;
			for(int k=1;1<<(k-1)<=idx;k++)
				if(idx&(1<<(k-1))) update(1,k,x,y);
		}
	}
	return 0;
} 







posted @ 2021-07-21 17:24  OvO1  阅读(60)  评论(0编辑  收藏  举报