求调,悬4关
  • 板块灌水区
  • 楼主focus_costan1
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/4/25 22:42
  • 上次更新2024/4/26 15:32:05
查看原帖
求调,悬4关
1148600
focus_costan1楼主2024/4/25 22:42

P2574 XOR的艺术

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
	int lazy,sum;
}tree[800005];
int a[200005];
char s[200005];
void pushup(int x){
	tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
}
void pushdown(int x,int l,int r){
	int mid=(l+r)>>1;
	tree[x<<1].lazy^=1;
	tree[x<<1|1].lazy^=1;
	tree[x<<1].sum+=tree[x].lazy*(mid-l+1);
	tree[x<<1|1].sum+=tree[x].lazy*(r-mid);
	tree[x].lazy=0;	
}
void build(int l,int r,int x){
	if(l==r){
		tree[x].sum=a[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,x<<1);
	build(mid+1,r,x<<1|1);
	pushup(x);
} 
void update(int x,int l,int r,int from,int to){
	if(l>=from&&r<=to){
		tree[x].lazy^=1;
		tree[x].sum+=1*(r-l+1);
		return ;
	}
	pushdown(x,l,r);
	int mid=(l+r)>>1;
	if(from<=mid){
		update(x<<1,l,mid,from,to);
	}
	if(to>mid){
		update(x<<1|1,mid+1,r,from,to);
	}
	pushup(x);
}
int query(int x,int l,int r,int from,int to){
	if(l>=from&&r<=to){
		return tree[x].sum;
	}
	pushdown(x,l,r);
	int mid=(l+r)>>1;
	int ans=0;
	if(from<=mid){
		ans+=query(x<<1,l,mid,from,to);
	}
	if(to>mid){
		ans+=query(x<<1|1,mid+1,r,from,to);
	}
	return ans;
}
signed main(){
	int n,f;
	scanf("%lld%lld",&n,&f);
	for(int i=1;i<=n;i++){
		cin>>s[i];
		a[i]=s[i]-'0';
	}
	build(1,n,1);
	while(f--){
		int opt,l,r;
		scanf("%lld%lld%lld",&opt,&l,&r);
		if(opt==0){
			update(1,1,n,l,r);
		}
		if(opt==1){
			printf("%lld\n",query(1,1,n,l,r));
		}
	}
	return 0;
}

回复请@我的大号focus_costan

2024/4/25 22:42
加载中...