用线段树只有10分,求调
  • 板块P2574 XOR的艺术
  • 楼主sllhy7
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/8 11:16
  • 上次更新2025/2/8 13:41:19
查看原帖
用线段树只有10分,求调
1283976
sllhy7楼主2025/2/8 11:16
#include<bits/stdc++.h>
using namespace std;
int n,m,a[200010],op,x,y;
struct st{
	int lc,rc,v,la,le;
}xd[800010];
char c[200010]; 
void build(int root,int l,int r){
	xd[root].lc=l;
	xd[root].rc=r;
	xd[root].le=r-l+1;
	xd[root].la=0;
	if(l==r){
		xd[root].v=a[l];
	}
	else{
		int mid=(l+r)/2;
		build(root*2,l,mid);
		build(root*2+1,mid+1,r);
		xd[root].v=xd[root*2].v+xd[root*2+1].v;
	}
	return ;
}
void pushdown(int root){
	xd[root*2].la=(xd[root*2].la^1); 
	xd[root*2+1].la=(xd[root*2+1].la^1);
	xd[root*2].v=xd[root*2].le-xd[root*2].v;
	xd[root*2+1].v=xd[root*2+1].le-xd[root*2+1].v;
	return ; 
}
void xg(int root,int l,int r){
	if(l<=xd[root].lc&&xd[root].rc<=r){
		xd[root].la=(xd[root].la^1);
		xd[root].v=xd[root].le-xd[root].v;
		return ;
	}
	if(l>xd[root].rc||xd[root].lc>r) return ;
	if(xd[root].la) pushdown(root);
	xg(root*2,l,r);
	xg(root*2+1,l,r);
	xd[root].v=xd[root*2].v+xd[root*2+1].v;
	return ;  
}
int cx(int root,int l,int r){
	if(l<=xd[root].lc&&xd[root].rc<=r){
		return xd[root].v;
	}
	if(l>xd[root].rc||r<xd[root].lc) return 0;
	if(xd[root].la) pushdown(root);
	return cx(root*2,l,r)+cx(root*2+1,l,r);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	cin>>c;
	for(int i=1;i<=n;i++){
		a[i]=c[i-1]-'0';
	}
	build(1,1,n);
	for(int i=1;i<=m;i++){
		cin>>op>>x>>y;
		if(op==0){
			xg(1,x,y);
		}
		else{
			cout<<cx(1,x,y)<<"\n";
		}
	} 
	return 0;
}

2025/2/8 11:16
加载中...