求助!!!实在看不出来了
查看原帖
求助!!!实在看不出来了
787452
zqx1108楼主2023/9/22 22:59

代码看了几天了,样例都过不了。。。

#include<bits/stdc++.h>
using namespace std;
#define N 200005 
int m,n;
int b,c,d;
string a;
int num[N];
struct node{
	int l,r;
	int sum,lazy;
}t[N*4+10];

void build(int p,int ll,int rr){
	t[p].l=ll;
	t[p].r=rr;
	if(ll==rr){
		t[p].sum=num[p];
		return ;
	}
	int mid=(ll+rr)/2;
	build(p*2,ll,mid);
	build(p*2+1,mid+1,rr);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}

void spread(int p){
	if(t[p].lazy){
		t[p*2].sum=t[p*2].r-t[p*2].l+1-t[p*2].sum;
		t[p*2+1].sum=t[p*2+1].r-t[p*2+1].l+1-t[p*2+1].sum;
		t[p*2].lazy^=1;
		t[p*2+1].lazy^=1;
		t[p].lazy=0;
	}
}

int ask(int p,int x,int y){
	if(x<=t[p].l&&y>=t[p].r){
		return t[p].sum;
	}
	int mid=(t[p].l+t[p].r)>>1;
	int ans=0;
	spread(p);
	if(x<=mid) ans+=ask(p*2,x,y);
	if(y>mid) ans+=ask(p*2+1,x,y);
	return ans;
}
void change(int p,int x,int y){
	if(x<=t[p].l&&y>=t[p].r){
		t[p].sum=t[p].r-t[p].l+1-t[p].sum;
		t[p].lazy^=1;
		return ;
	}
	int mid=(t[p].l+t[p].r)>>1;
	spread(p);
	if(x<=mid) change(p*2,x,y);
	if(y>mid) change(p*2+1,x,y);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
int main(){
	cin>>n>>m;
	cin>>a;
	for(int i=0;i<a.size();i++){
		if(a[i]=='1') num[i+1]=1;
		else num[i+1]=0;
	}
	build(1,1,n);
	for(int i=1;i<=m;i++){
		cin>>b>>c>>d;
		if(b==0){
			change(1,c,d);	
		}
		else {
			cout<<ask(1,c,d)<<'\n';
		}
	} 
	
	return 0;
}
2023/9/22 22:59
加载中...