luogu P2574 XOR的艺术

题目传送门:https://www.luogu.org/problemnew/show/P2574



题意:

有一个长度为n的01序列,现在有两种操作。

[1]:将x~y区间的数都异或1。

[2]:求x^y的区间和。



思路:

改一改updata,就是模板题了。



代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
	int n,m,len=0;
	struct node{int d,n,lazy,l,r,lc,rc;} tr[400010];
	char s[200010];
void build(int l,int r)
{
	int now=++len,mid=(l+r)>>1;
	tr[now].l=l;
	tr[now].r=r;
	tr[now].n=r-l+1;
	tr[now].lazy=0;
	tr[now].lc=tr[now].rc=-1;
	if(l<r)
	{
		tr[now].lc=len+1; build(l,mid);
		tr[now].rc=len+1; build(mid+1,r);
	}
}
void updata(int now)
{
	if(tr[now].lazy)
	{
		int lc=tr[now].lc,rc=tr[now].rc;
		if(lc!=-1) tr[lc].d=tr[lc].n-tr[lc].d,tr[lc].lazy^=1;
		if(rc!=-1) tr[rc].d=tr[rc].n-tr[rc].d,tr[rc].lazy^=1;
		tr[now].lazy=0;
	}
}
void change(int now,int l,int r,int k)
{
	updata(now);
	if(l==tr[now].l&&r==tr[now].r)
	{
		tr[now].d=k==-1?tr[now].n-tr[now].d:k;
		tr[now].lazy^=1;
		return;
	}
	int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)>>1;
	if(r<=mid) change(lc,l,r,k);
	else if(l>mid) change(rc,l,r,k);
	else change(lc,l,mid,k),change(rc,mid+1,r,k);
	tr[now].d=tr[lc].d+tr[rc].d;
}
int findsum(int now,int l,int r)
{
	updata(now);
	if(l==tr[now].l&&r==tr[now].r) return tr[now].d;
	int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)>>1;
	if(r<=mid) return findsum(lc,l,r);
	else if(l>mid) return findsum(rc,l,r);
	else return findsum(lc,l,mid)+findsum(rc,mid+1,r);
}
int main()
{
	int t,x,y;
	scanf("%d %d",&n,&m);
	scanf("%s",s+1);
	build(1,n);
	for(int i=1;i<=n;i++)
		change(1,i,i,s[i]-48);
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %d",&t,&x,&y);
		if(!t) change(1,x,y,-1); else printf("%d\n",findsum(1,x,y));
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值