10pts!求助
查看原帖
10pts!求助
907430
corner_xiejunqi楼主2024/11/23 10:55
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lc p<<1
#define rc p<<1|1
const int N=1e5+10;
int n,m,a[N],op,l,r;
struct node{
	int l,r,val,add;
}tree[4*N];
void push_up(int p){
	tree[p].val=tree[lc].val+tree[rc].val;
}
void build(int p,int l,int r){
	tree[p]={l,r,0,0};
	if(l==r) return;
	int mid=l+r>>1;
	build(lc,l,mid);
	build(rc,mid+1,r);
	push_up(p);
}
void push_down(int p){
	tree[lc].val=tree[lc].r-tree[lc].l+1-tree[lc].val;
	tree[rc].val=tree[rc].r-tree[rc].l+1-tree[rc].val;
	tree[lc].add=!tree[lc].add;
	tree[rc].add=!tree[rc].add;
	tree[p].add=0;
}
void update(int p,int l,int r){
	if(tree[p].l>=l && tree[p].r<=r){
		tree[p].add=tree[p].r-tree[p].l+1-tree[p].val;
		tree[p].add=!tree[p].add;
		return;
	}
	push_down(p);
	if(tree[lc].r>=l) update(lc,l,r);
	if(tree[rc].l<=r) update(rc,l,r);
	push_up(p);
}
int query(int p,int l,int r){
	if(tree[p].l>=l && tree[p].r<=r) return tree[p].val;
	push_down(p);
	int sum=0;
	if(tree[lc].r>=l) sum+=query(lc,l,r);
	if(tree[rc].l<=r) sum+=query(rc,l,r);
	return sum;
}
signed main(){
	cin.tie(0);cout.tie(0);
	ios::sync_with_stdio(false);
	cin>>n>>m;
	build(1,1,n);
	while(m--){
		cin>>op>>l>>r;
		if(op==0) update(1,l,r);
		else cout<<query(1,l,r)<<'\n';
	}
	return 0;
}

2024/11/23 10:55
加载中...