线段树求调
查看原帖
线段树求调
973480
封禁用户楼主2024/11/28 13:17
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e5+10;
int n,m,a[maxn],tree[maxn*4];
int lzy[maxn*4]; 
void pushup(int u){
	tree[u]=tree[u*2]+tree[u*2+1];
}
void build(int u,int left,int right){
	if(left==right){
		tree[u]=a[left];
		return;
	}
	int mid=(left+right)/2;
	build(u*2,left,mid);
	build(u*2+1,mid+1,right);
	pushup(u);
	return;
}
bool inrange(int L,int R,int l,int r){
	return (l<=L)&&(r>=R);
}
bool outrange(int L,int R,int l,int r){
	return (L>r)||(R<l);
}
void make(int u,int l,int r){
	tree[u]=(r-l+1)-tree[u];
	lzy[u]^=1;
	return;
}
void pushdown(int u,int L,int R){
	int M=(L+R)/2;
	make(u*2,L,M);
	make(u*2+1,M+1,R);
	lzy[u]=0;
	return;
}
int sum(int u,int L,int R,int l,int r){
	if(inrange(L,R,l,r)) return tree[u];
	else if(!outrange(L,R,l,r)){
		int M=(L+R)/2;
		pushdown(u,L,R); 
		return sum(u*2,L,M,l,r)+sum(u*2+1,M+1,R,l,r);
	}
	return 0;
}
void update(int u,int L,int R,int l,int r){
	if(inrange(L,R,l,r)){
		make(u,L,R);
		return;
	}else if(!outrange(L,R,l,r)){
		int M=(L+R)/2;
		pushdown(u,L,R);
		update(u*2,L,M,l,r);
		update(u*2+1,M+1,R,l,r);
		pushup(u);
	}else return;
}
void printt(int u,int L,int R){
	cout << tree[u] << " " << L << " " << R << endl;
	if(L==R) return;
	int M=(L+R)/2;
	printt(u*2,L,M);
	printt(u*2+1,M+1,R);
	return;
}
main(){
	cin >> n >> m;
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int opt,x,y;
		cin >> opt >> x >> y;
		if(opt==0){
			update(1,1,n,x,y);
		}else{
		    printf("%lld\n",sum(1,1,n,x,y));
		}
	}
	return 0;
}
2024/11/28 13:17
加载中...