线段树求调P2574 RE_80
  • 板块P2574 XOR的艺术
  • 楼主dyyzy
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/10/17 22:38
  • 上次更新2023/10/27 07:03:57
查看原帖
线段树求调P2574 RE_80
461012
dyyzy楼主2022/10/17 22:38
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
inline int read(){
	int x=0,flag=1;char ch=getchar();
	while(ch<'0' || ch>'9'){flag=(ch=='-')?-1:1;ch=getchar();}
	while(ch>='0' && ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*flag;
}
const int N=100005;
struct TREE{
	int l,r,tot;
	int trun;
}t[N<<2];
string s;
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r;
	if(l==r){t[p].tot=s[l-1]=='1';return;}
	int mid=(l+r)>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	t[p].tot=t[p<<1].tot+t[p<<1|1].tot;
}
void spread(int p){
	if(t[p].trun){
		t[p<<1].tot=(t[p<<1].r-t[p<<1].l+1)-t[p<<1].tot;
		t[p<<1|1].tot=(t[p<<1|1].r-t[p<<1|1].l+1)-t[p<<1|1].tot;
		t[p<<1].trun^=1;
		t[p<<1|1].trun^=1;
		t[p].trun=0;
	}
}
void change(int p,int l,int r){
	if(l<=t[p].l && t[p].r<=r){
		t[p].tot=(t[p].r-t[p].l+1)-t[p].tot;
		t[p].trun^=1;
		return;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) change(p<<1,l,r);
	if(mid<r) change(p<<1|1,l,r);
	t[p].tot=t[p<<1].tot+t[p<<1|1].tot;
}
int ask(int p,int l,int r){
	if(l<=t[p].l && t[p].r<=r) return t[p].tot;
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	int ans=0;
	if(l<=mid) ans+=ask(p<<1,l,r);
	if(mid<r) ans+=ask(p<<1|1,l,r);
	return ans;
}
int main(){
	freopen("P2574_2.in","r",stdin);
	freopen("P2574_2.ans","w",stdout);
	int n=read(),m=read();
	cin>>s;
	build(1,1,n);
	for(int i=1;i<=m;++i){
		int pd=read(),l=read(),r=read();
		if(pd==0){
			change(1,l,r);
		}
		if(pd==1){
			cout<<ask(1,l,r)<<endl;
		}
	}
	return 0;
}

我的提交记录

2022/10/17 22:38
加载中...