0分求调
  • 板块P2574 XOR的艺术
  • 楼主Eous
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/11/28 20:01
  • 上次更新2023/11/28 22:01:47
查看原帖
0分求调
549131
Eous楼主2023/11/28 20:01
#include<bits/stdc++.h>
#define int long long
using namespace std;
int const maxn=2e5+10;
struct node{
	int l,r,val,lazy;
}tree[maxn<<2];
int n,q;
int a[maxn];
char c;
void pushUp(int rt)
{
	tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val;
}
void pushDown(int rt)
{
	if(tree[rt].lazy)
	{
		tree[rt<<1].lazy^=1;
		tree[rt<<1|1].lazy^=1;
		tree[rt<<1].val=tree[rt<<1].r-tree[rt<<1].l+1-tree[rt<<1].val;
		tree[rt<<1|1].val=tree[rt<<1|1].r-tree[rt<<1|1].l+1-tree[rt<<1|1].val;
		tree[rt].lazy=0;
	}
}
void build(int l,int r,int rt)
{
	tree[rt].l=l;
	tree[rt].r=r;
	if(l==r)
	{
		tree[rt].val=a[l];
		return;
	}
	int mid=l+r>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	pushUp(rt);
}
void update(int ql,int qr,int rt)
{
	int l=tree[rt].l;
	int r=tree[rt].r;
	if(ql<=l&&r<=qr)
	{
		tree[rt].val=r-l+1-tree[rt].val;
		tree[rt].lazy^=1;
		return;
	}
	pushDown(rt);
	int mid=l+r>>1; 
	if(ql<=mid)
		update(ql,qr,rt<<1);
	if(qr>mid)
		update(ql,qr,rt<<1|1);
}
int query(int ql,int qr,int rt)
{
	int l=tree[rt].l;
	int r=tree[rt].r;
	if(ql<=l&&r<=qr)
		return tree[rt].val;
	pushDown(rt);
	int mid=l+r>>1;
	int ret=0;
	if(ql<=mid)
		ret+=query(ql,qr,rt<<1);
	if(qr>mid)
		ret+=query(ql,qr,rt<<1|1);
	return ret;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		cin>>c;
		a[i]=(c=='1');
	}
	build(1,n,1);
	while(q--)
	{
		int l,r,op;
		cin>>op>>l>>r;
		if(op==0)
			update(l,r,1);
		else
			cout<<query(l,r,1)<<endl;
	}
	return 0;
}
2023/11/28 20:01
加载中...