线段树wa10分求调
查看原帖
线段树wa10分求调
1279263
hzc0829楼主2025/2/5 18:35
#include<bits/stdc++.h>
using namespace std;
int n,m;
struct node{
	int l,r;
	int cnt;
	bool _not;
}t[400010];
void pushup(int p){
	t[p].cnt=t[p<<1].cnt+t[p<<1|1].cnt;
}
void pushdown(int p){
	t[p<<1].cnt=t[p<<1].r-t[p<<1].l+1-t[p<<1].cnt;
	t[p<<1|1].cnt=t[p<<1|1].r-t[p<<1|1].l+1-t[p<<1|1].cnt;
	t[p<<1]._not^=t[p]._not;
	t[p<<1|1]._not^=t[p]._not;
	t[p]._not=0;
}
void build(int rt,int l,int r){
	t[rt].l=l;
	t[rt].r=r;
	if(l==r){
		t[rt].cnt=0;
		t[rt]._not=0;
		return;
	}
	int mid=(l+r)>>1;
	build(rt<<1,l,mid);
	build(rt<<1|1,mid+1,r);
	pushup(rt);
}
void modify(int rt,int l,int r){
	if(l<=t[rt].l&&t[rt].r<=r){
		t[rt]._not^=1;
		return;
	}
	int mid=(t[rt].l+t[rt].r)>>1;
	if(t[rt]._not){
		pushdown(rt);
	}
	if(l<=mid){
		modify(rt<<1,l,mid);
	}
	if(r>mid){
		modify(rt<<1|1,mid+1,r);
	}
	pushup(rt);
}
int query(int rt,int l,int r){
	if(l<=t[rt].l&&t[rt].r<=r){
		return t[rt].cnt;
	}
	int res=0;
	int mid=(t[rt].l+t[rt].r)>>1;
	if(t[rt]._not){
		pushdown(rt);
	}
	if(l<=mid){
		res+=query(rt<<1,l,mid);
	}
	if(r>mid){
		res+=query(rt<<1|1,mid+1,r);
	}
	return res;
}
int main(){
	cin>>n>>m;
	build(1,1,n);
	for(int i=0;i<m;i++){
		int op,a,b;
		cin>>op>>a>>b;
		if(!op){
			modify(1,a,b);
		}else{
			cout<<query(1,a,b)<<endl;
		}
	}
	return 0;
}
2025/2/5 18:35
加载中...