为什么RE
查看原帖
为什么RE
128870
chen_qian楼主2021/2/21 17:07

此代码在本地运行结果如下

但是在luogu评测机上显示RE

#include<bits/stdc++.h>
#define N 5000050
#define INF (1<<25) 
using namespace std;
int fa[N],ch[N][2],val[N],tag[N],rev[N],mx[N],lmx[N],rmx[N],size[N],sum[N];
int n,m,root,a[N];
int s[N],top;
void Recycle(int x){
    int &l=ch[x][0],&r=ch[x][1];
    if(l)Recycle(l);
    if(r)Recycle(r);
    s[++top]=x;
    fa[x]=l=r=tag[x]=rev[x]=0;
}
void Push_up(int x){
	int ls=ch[x][0],rs=ch[x][1];
	size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
	sum[x]=sum[ls]+sum[rs]+val[x];
	mx[x]=max(mx[ls],max(mx[rs],lmx[rs]+rmx[ls]+val[x]));
	lmx[x]=max(lmx[ls],sum[ls]+val[x]+lmx[rs]);
    rmx[x]=max(rmx[rs],sum[rs]+val[x]+rmx[ls]);
}
void Push_down(int x){
	int l=ch[x][0],r=ch[x][1];
	if(tag[x]!=N){
		rev[x]=0;
		if(l) tag[l]=tag[x],sum[l]=size[l]*tag[x],val[l]=tag[x];
		if(r) tag[r]=tag[x],sum[r]=size[l]*tag[x],val[r]=tag[x];
		if (tag[x]>=0){
            if(l) lmx[l]=rmx[l]=mx[l]=sum[l];
            if(r) lmx[r]=rmx[r]=mx[r]=sum[r];
        }
		else{
            if(l) lmx[l]=rmx[l]=0,mx[l]=tag[x];
            if(r) lmx[r]=rmx[r]=0,mx[r]=tag[x];
        }
        tag[x]=N;
	}
	if(rev[x]){
		rev[x]=0;rev[l]^=1;rev[r]^=1;
        swap(lmx[l],rmx[l]);swap(lmx[r],rmx[r]);
        swap(ch[l][0],ch[l][1]);swap(ch[r][0],ch[r][1]);
	}
}
int Get(int x){
	return x==ch[fa[x]][1];
}
void Clear(int x){
	s[++top]=x;
	fa[x]=ch[x][1]=ch[x][0]=val[x]=rev[x]=mx[x]=lmx[x]=rmx[x]=size[x]=0;
	tag[x]=N;
	return ;
}
int Build(int l,int r,int f){
	if(l>r) return 0;
	int mid=(l+r)>>1;
	int x=s[top];
	top--;
	val[x]=a[mid];
	if(a[mid]!=INF&&a[mid]!=-INF)mx[x]=a[mid];
	else mx[x]=-INF;
	ch[x][0]=ch[x][1]=0;
	lmx[x]=rmx[x]=max(a[mid],0);
	fa[x]=f;
	tag[x]=N;
	ch[x][0]=Build(l,mid-1,x);
	ch[x][1]=Build(mid+1,r,x);
	Push_up(x);
} 
void Rotate(int x){
	int y=fa[x],z=fa[y],chk=Get(x);
	ch[y][chk]=ch[x][chk^1];
	fa[ch[x][chk^1]]=y;
	ch[x][chk^1]=y;
	fa[y]=x;
	fa[x]=z;
	if(z) ch[z][y==ch[z][1]]=x;
	Push_up(y);
	Push_up(x);
}
void Splay(int x,int goal){
	for(int f=fa[x];f=fa[x],f!=goal;Rotate(x))
		if(fa[f]&&fa[f]!=goal) Rotate(Get(x)==Get(f)?f:x);
	if(goal==0) root=x;
}
int Query_rank(int k){
	int cnr=root;
	while(1){
		if(cnr==0) break;
		Push_down(cnr);
		if(k<=size[ch[cnr][0]]) cnr=ch[cnr][0];
		else{
			k-=size[ch[cnr][0]]+1;
			if(!k) return cnr;
			cnr=ch[cnr][1];
		}
	}
	return 0;
}
int Find(int k,int tot){
	int x=Query_rank(k),y=Query_rank(k+tot+1);
	Splay(x,0);
	Splay(y,x);
	int pos=ch[root][1];
	pos=ch[pos][0];
	return pos;
}
void Query(int k,int tot){
	int x=Find(k,tot);
	printf("%d\n",sum[x]);
}
void Modify(int k,int tot,int v){
	int x=Find(k,tot),y=fa[x];
	val[x]=v;
	tag[x]=v;
	sum[x]=size[x]*v;
	if(v>=0) lmx[x]=rmx[x]=mx[x]=sum[x];
	else lmx[x]=rmx[x]=0,mx[x]=v;
	Push_up(y);
	Push_up(fa[y]); 
}
void Reverse(int k,int tot){
	//cout<<"qwq"<<endl;
	int x=Find(k,tot),y=fa[x];
	rev[x]^=1;
	if(rev[x]){
		swap(ch[x][0],ch[x][1]);
		swap(lmx[x],rmx[x]);
		Push_up(y);
		Push_up(fa[y]);
	}
}
void Erase(int k,int tot){
	int x=Find(k,tot),y=fa[x];
	//cout<<x<<' '<<y<<endl;
	ch[y][0]=0;
	Recycle(x);
	Push_up(y),Push_up(fa[y]);
}
void Insert(int k,int tot){
	for(int i=1;i<=tot;i++) scanf("%d",&a[i]);
	int rt=Build(1,tot,0);
	int x=Query_rank(k+1),y=Query_rank(k+2);
	Splay(x,0);
	Splay(y,x);
	fa[rt]=y;
	ch[y][0]=rt;
	Push_up(y);
	Push_up(x); 
}
void print(int x){
	Push_down(x);
	if(ch[x][0]) print(ch[x][0]);
	if(val[x]!=-INF&&val[x]!=INF) cout<<val[x]<<' ';
	if(ch[x][1]) print(ch[x][1]); 
}
int main(){
	//freopen("1.txt","w",stdout);
	scanf("%d%d",&n,&m);
	mx[0]=a[1]=a[n+2]=-INF;
	for(int i=1;i<=n;i++) scanf("%d",&a[i+1]);
	for(int i=1;i<=N-30;i++) s[++top]=i;
	root=Build(1,n+2,0);
	string op;
	int k,tot,val;
	while(m--){
		cin>>op;
		if(op=="INSERT"){
			scanf("%d%d",&k,&tot);
			Insert(k,tot);
		}
		else if(op=="DELETE"){
			scanf("%d%d",&k,&tot);
			Erase(k,tot);
		}
		else if(op=="MAKE-SAME"){
			scanf("%d%d%d",&k,&tot,&val);
			Modify(k,tot,val);
		}
		else if(op=="REVERSE"){
			scanf("%d%d",&k,&tot);
			Reverse(k,tot);
		}
		else if(op=="GET-SUM"){
			scanf("%d%d",&k,&tot);
			Query(k,tot);
		}
		else{
			printf("%d\n",mx[root]);
		}
		print(root);
		cout<<endl;
	}
}

每一次操作后输出的那一列为操作后当前的序列

2021/2/21 17:07
加载中...