10pts 求条
查看原帖
10pts 求条
1110209
Eater_Tree楼主2024/11/25 19:46

AC on #2

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
struct node{
	int l,r,dat,turn;
}tree[801000];
int op,a,b;
void build(int p,int l,int r){
	tree[p].l=l,tree[p].r=r;
	if(l==r)
		return ;
	int mid=(tree[p].l+tree[p].r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	return ;
}
void down(int p){
	if(tree[p].turn%2==0)
		return ;
	tree[p*2].dat=tree[p*2].r-tree[p*2].l+1-tree[p*2].dat;
	tree[p*2+1].dat=tree[p*2+1].r-tree[p*2+1].l+1-tree[p*2+1].dat;
	tree[p*2].turn++;
	tree[p*2+1].turn++;
	tree[p].turn=0;
	return ;
}
void update(int p,int l,int r){
	if(tree[p].l>=l&&tree[p].r<=r){
		tree[p].dat=tree[p].r-tree[p].l+1-tree[p].dat;
		tree[p].turn++;
		return ;
	}
	down(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid)
		update(p*2,l,mid);
	if(r>mid)
		update(p*2+1,mid+1,r);
	tree[p].dat=tree[p*2].dat+tree[p*2+1].dat;
	return ;
}
int ask(int p,int l,int r){
	if(tree[p].l>=l&&tree[p].r<=r)
		return tree[p].dat;
	down(p);
	int ans=0;
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid)
		ans+=ask(p*2,l,mid);
	if(r>mid)
		ans+=ask(p*2+1,mid+1,r);
	return ans;
}
signed main(){
	scanf("%lld%lld",&n,&m);
	build(1,1,n);
	while(m--){
		scanf("%lld%lld%lld",&op,&a,&b);
		if(op==0)
			update(1,a,b);
		else
			printf("%lld\n",ask(1,a,b));
	}
	return 0;
}
2024/11/25 19:46
加载中...