同志们,哪里有问题啊!!!
查看原帖
同志们,哪里有问题啊!!!
482102
史蒂夫的憨憨楼主2022/12/11 11:50
#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;
int n,m,op,l,r;
char tt[MAX]; 
int a[MAX];
struct node{
	int l,r;
	int data;
	int add;	
}t[MAX*4];
void pushup(int p){
	t[p].data=t[p*2].data+t[p*2+1].data; 
}
void pushdown(int p){
	if(t[p].add){
		t[2*p].data+=t[2*p].r-t[2*p].l+1-t[2*p].data;
		t[2*p+1].data+=t[2*p+1].r-t[2*p+1].l+1-t[2*p+1].data;
		t[2*p].add^=1;
		t[2*p+1].add^=1;
		t[p].add=0;
	}
}
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r;
	if(l==r){
		t[p].data=a[l];
		return;
	}
	int mid=(l+r)/2;
	build(2*p,l,mid);
	build(2*p+1,mid+1,r);
	pushup(p);
}
void change(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r){
		t[p].data=t[p].r-t[p].l+1-t[p].data;
		t[p].add^=1;
		return;
	}
	pushdown(p);
	int mid=(t[p].l+t[p].r)/2;
	if(l<=mid) change(2*p,l,r);
	if(r>mid) change(2*p+1,l,r);
	pushup(p);
}
int query(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r) return t[p].data;
	pushdown(p);
	int mid=(t[p].l+t[p].r)/2,sum=0;
	if(l<=mid) sum+=query(2*p,l,r);
	if(r>mid) sum+=query(2*p+1,l,r);
	return sum;
}
int main(){
	scanf("%d%d",&n,&m);
	cin>>tt;
	for(int i=0;i<n;i++)
		if(tt[i]=='1')
			a[i+1]=1;
	build(1,1,n);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&op,&l,&r);
		if(op==0) change(1,l,r);
		else printf("%d\n",query(1,l,r));
	}
	return 0;
}
/*
10 6
1011101001
0 2 4
1 1 5
0 3 7
1 1 10
0 1 4
1 2 6

0000010001
*/

2022/12/11 11:50
加载中...