社区讨论

只过hack求条

P2572[SCOI2010] 序列操作参与者 3已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@mjh3eo7v
此快照首次捕获于
2025/12/22 19:48
2 个月前
此快照最后确认于
2025/12/25 13:00
2 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define ls u<<1
#define rs u<<1|1
#define N 100010
using namespace std;
struct tree{int l,r,b,lb,rb,mb,c,lc,rc,mc,len,tag,rev;}tr[N*4];
int a[N];
void pushup(tree& u,tree ll,tree rr){
	u.b = ll.b+rr.b;
	u.lb = ll.c?ll.lb:ll.b+rr.lb;
	u.rb = rr.c?rr.rb:rr.b+ll.rb;
	u.mb = max(max(ll.mb,rr.mb),ll.rb+rr.lb);
	u.c = ll.c+rr.c;
	u.rc = ll.b?ll.lc:ll.c+rr.lc;
	u.rc = rr.b?rr.rc:rr.c+ll.rc;
	u.mc = max(max(ll.mc,rr.mc),ll.rc+rr.lc);
}
void pd(int u,int op){
	tree& t = tr[u];
	if(op==0){
		t.b = t.lb = t.rb = t.mb = t.tag = t.rev = 0;
		t.c = t.lc = t.rc = t.mc = t.len;
	}
	else if(op==1){
		t.b = t.lb = t.rb = t.mb = t.len;
		t.c = t.lc = t.rc = t.mc = t.rev = 0;
		t.tag = 1;
	}
	else{
		swap(t.b,t.c);
		swap(t.lb,t.lc);
		swap(t.rb,t.rc);
		swap(t.mb,t.mc);
		t.rev^=1;
	}
}
void pushdown(int u){
	tree& t = tr[u];
	if(t.tag==0){
		pd(ls,0);
		pd(rs,0);
	}
	if(t.tag==1){
		pd(ls,1);
		pd(rs,1);
	}
	if(t.rev){
		pd(ls,2);
		pd(rs,2);
	}
	t.tag = -1,t.rev = 0;
}
void build(int u,int l,int r){
	int f = a[l];
	tr[u] = {l,r,f,f,f,f,f^1,f^1,f^1,f^1,r-l+1,-1,0};
	if(l==r)return ;
	int mid = (l+r)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	pushup(tr[u],tr[ls],tr[rs]);
}
void change(int u,int x,int y,int op){
	if(x>tr[u].r||y<tr[u].l)return ;
	if(x<=tr[u].l&&tr[u].r<=y){
		pd(u,op);
		return ;
	}
	pushdown(u);
	change(ls,x,y,op);
	change(rs,x,y,op);
	pushup(tr[u],tr[ls],tr[rs]);
}
tree query(int u,int x,int y){
	if(x>tr[u].r||y<tr[u].l)return {};
	if(x<=tr[u].l&&tr[u].r<=y)return tr[u];
	pushdown(u);
	tree t;
	pushup(t,query(ls,x,y),query(rs,x,y));
	return t;
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cout.tie(0);
	int n,m,op,x,y;
	cin>>n>>m;
	for(int i = 1;i<=n;i++)cin>>a[i];
	build(1,1,n);
	for(int i = 1;i<=m;i++){
		cin>>op>>x>>y;
		x++,y++;
		if(op<3)change(1,x,y,op);
		else{
			tree t = query(1,x,y);
			if(op==3)cout<<t.b<<'\n';
			else cout<<t.mb<<'\n';
		}
	}
	return 0;
}

回复

6 条回复,欢迎继续交流。

正在加载回复...