10分,求助
查看原帖
10分,求助
638119
封禁用户楼主2023/11/21 20:42
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define SZ(x) (tree[x].r-tree[x].l+1)
const int maxn=200000+100;

int num[maxn];
struct node{
	int l,r,add;
	LL sum;
}tree[maxn<<2];
void PushUp(int rt)
{
	tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum;
}
void build(int l,int r,int rt)
{
	tree[rt].l=l;
	tree[rt].r=r;
	if(l==r)
	{
		tree[rt].sum=num[l];
		return;
	}
	int m=(tree[rt].l+tree[rt].r)>>1;
	build(l,m,L(rt));
	build(m+1,r,R(rt));
	PushUp(rt);
}
void PushDown(int rt)
{
	if(tree[rt].add==1)
	{
		tree[L(rt)].add=1;
		tree[R(rt)].add=1;
		tree[L(rt)].sum=SZ(L(rt))-tree[L(rt)].sum;
		tree[R(rt)].sum=SZ(R(rt))-tree[R(rt)].sum;
		tree[rt].add=0;
	}
}
void update(int l,int r,int rt)
{
	if(l<=tree[rt].l && tree[rt].r<=r)
	{
		tree[rt].add=1;
		tree[rt].sum=SZ(rt)-tree[rt].sum;
		return ;
	}
	PushDown(rt);
	int m=(tree[rt].l+tree[rt].r)>>1;
	if(l<=m) update(l,r,L(rt));
	if(r>m) update(l,r,R(rt));
	PushUp(rt);
}
LL query(int l,int r,int rt)
{
	if(l<=tree[rt].l && tree[rt].r<=r)
		return tree[rt].sum;
	PushDown(rt);
	int m=(tree[rt].l+tree[rt].r)>>1;
	LL ret=0;
	if(l<=m) ret+=query(l,r,L(rt));
	if(m<r) ret+=query(l,r,R(rt));
	return ret;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		char c;
		cin>>c;
		if(c=='1') num[i]=1; 
	}
		
	build(1,n,1);
	int q,x,y,k;
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&q);
		if(q==0)
		{
			scanf("%d %d",&x,&y);
			update(x,y,1);
		}else{
			scanf("%d%d",&x,&y);
			printf("%lld\n",query(x,y,1));
		}
	}
}
2023/11/21 20:42
加载中...