线段树愣是只A了#2(赏1关)
查看原帖
线段树愣是只A了#2(赏1关)
846398
jomy楼主2023/8/13 20:32

求 dalao⁡\operatorname{dalao} 指点

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int s[N],tr[N<<2],tag[N<<2];
#define lson (root<<1)
#define rson ((root<<1)+1)
#define mid ((l+r)>>1)
void down(int root,int l,int r){
	if(tag[root]){
		tr[root]^=tag[root]*(r-l+1);
		tag[lson]^=tag[root];
		tag[rson]^=tag[root];
		tag[root]=0;
	}
}
void update(int root){
	tr[root]=tr[lson]+tr[rson];
}
int qurey(int root,int l,int r,int L,int R){
	down(root,l,r);
	int ans;
	if(l==L&&r==R)  return tr[root];
	else if(R<=mid) ans=qurey(lson,l,mid,L,R);
	else if(L>mid) ans=qurey(rson,mid+1,r,L,R);
	else ans=qurey(lson,l,mid,L,mid)+qurey(rson,mid+1,r,mid+1,R);
	down(lson,l,mid);
	down(rson,mid+1,r);
	update(root);
	return ans;
}
void add(int root,int l,int r,int L,int R){
	down(root,l,r);
	if(l==L&&R==r) tag[root]^=1;
	else{
		if(R<=mid) add(lson,l,mid,L,R);
		else if(L>mid) add(rson,mid+1,r,L,R);
		else{
			add(lson,l,mid,L,mid);
			add(rson,mid+1,r,mid+1,R);
		}
		down(lson,l,mid);
		down(rson,mid+1,r);
		update(root);
	}
}
int main(){
	int n,m;
	cin>>n>>m;
	while(m--){
		int a,b,c;
		cin>>a>>b>>c;
		if(a==0){
			add(1,1,n,b,c);
		}
		else{
			cout<<qurey(1,1,n,b,c)<<'\n';
		}
	}
	return 0;
}
2023/8/13 20:32
加载中...