求助
  • 板块学术版
  • 楼主hwc2011
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/21 09:14
  • 上次更新2024/8/21 09:15:00
查看原帖
求助
1313003
hwc2011楼主2024/8/21 09:14

这份代码CE了,但是本蒟蒻不知如何修改,希望各位大佬指导一下

#include<bits/stdc++.h>
#define int long long 
#define mid ((l+r)>>1)
#define lt (root<<1)
#define rt (root<<1|1)  
using namespace std;
const int N=1e5+5;
int n,m,a[N];
struct Node{
	int sum1,max1,lmax1,rmax1;
	int sum0,max0,lmax0,rmax0;
	int tag=-1,fan;
}tree[N<<2];
inline int read(){
	int sum=0,d=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar()) d=c=='-'?-d:d;
	for(;isdigit(c);c=getchar()) sum=(sum<<3)+(sum<<1)+(c&15);
	return sum*d;
} 
inline void print(int x,int c){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x/10) print(x/10,2);
	putchar(x%10^48);
	if(c^2) putchar(c?'\n':' ');
}
inline void pushup(Node &rot,Node &ls,Node &rs){
	rot.sum1=ls.sum1+rs.sum1;
	rot.lmax1=(ls.max0?ls.lmax1:ls.lmax1+rs.lmax1);
	rot.rmax1=(rs.max0?rs.rmax1:rs.rmax1+ls.rmax1);
	rot.max1=max(max(ls.max1,rs.max1),ls.rmax1+rs.lmax1);
	rot.sum0=ls.sum0+rs.sum0;
	rot.lmax0=(ls.max1?ls.lmax0:ls.lmax0+rs.lmax0);
	rot.rmax0=(rs.max1?rs.rmax0:rs.rmax0+ls.rmax0);
	rot.max0=max(max(ls.max0,rs.max0),ls.rmax0+rs.lmax0);
}
inline void change(int root,int op,int len){
	Node &rot=tree[root];
	if(op==0) rot={0,0,0,0,len,len,len,len,0,0};
	if(op==1) rot={len,len,len,len,0,0,0,0,1,0};
	if(op==2){
		rot.fan^=1;
		rot.sum1^=rot.sum0^=rot.sum1^=rot.sum0;
		rot.max1^=rot.max0^=rot.max1^=rot.max0;
		rot.lmax1^=rot.lmax0^=rot.lmax1^=rot.lmax0;
		rot.rmax1^=rot.rmax0^=rot.rmax1^=rot.rmax0;
	}
}
inline void pushdown(int root,int l,int r){
	Node &rot=tree[root];
	if(~rot.tag){
		change(lt,rot.tag,mid-l+1);
		change(rt,rot.tag,r-mid);
	}
	if(rot.fan){
		change(lt,2,mid-l+1);
		change(rt,2,r-mid);
	}
	rot.tag=-1;
	rot.fan=0;
}
inline void build(int root,int l,int r){
	if(l==r){
		if(a[l]) tree[root]={1,1,1,1,0,0,0,0,-1,0};
		else tree[root]={0,0,0,0,1,1,1,1,-1,0};
		return;
	}
	tree[root].tag=-1;
	build(lt,l,mid);
	build(rt,mid+1,r);
	pushup(tree[root],tree[lt],tree[rt]);
}
inline void update(int root,int l,int r,int ql,int qr,int op){
	if(qr<l||ql>r) return;
	if(l>=ql&&r<=qr){
		change(root,op,r-l+1);
		return;
	}
	pushdown(root,l,r);
	update(lt,l,mid,ql,qr,op);
	update(rt,mid+1,r,ql,qr,op);
	pushup(tree[root],tree[lt],tree[rt]);
}
inline Node query(int root,int l,int r,int ql,int qr){
	if(qr<l||ql>r) return tree[0];
	if(l>=ql&&r<=qr) return tree[root];
	pushdown(root,l,r);
	Node ans,ls=query(lt,l,mid,ql,qr),rs=query(rt,mid+1,r,ql,qr);
	pushup(ans,ls,rs);
	return ans;
}
signed main(){
	n=read();
	m=read();
	for(int i=1;i<=n;i++) a[i]=read();
	while(m--){
		int op=read(),l=read(),r=read();
		if(op<3) update(1,1,n,l,r,op);
		else{
			Node ans=query(1,1,n,l,r);
			print(op^4?ans.sum1:ans.max1,1);
		}
	}
}
2024/8/21 09:14
加载中...