fhq WA 0 求助,样例最后两行输出错误
查看原帖
fhq WA 0 求助,样例最后两行输出错误
467089
xlsfyc楼主2022/3/26 19:27
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,a[500001]={0},use[500001]={0},value[500001]={0},heap[500001]={0},size[500001]={0},lmax[500001]={0},rmax[500001]={0},sum[500001]={0},maxn[500001]={0},lc[500001]={0},rc[500001]={0},lazy1[500001]={0},lazy2[500001]={0};
int top=0,root=0,x=0,y=0,z=0,f=0,cnt=0;
int new_node(int x){
	int now=(top>=1)?use[top--]:++cnt;
    size[now]=1;
    value[now]=sum[now]=maxn[now]=x;
    heap[now]=rand();
    lazy1[now]=0;
    lazy2[now]=INF;
    lc[now]=rc[now]=0;
    lmax[now]=rmax[now]=max(x,0);
    return now;
}
void pushup(int now){
    size[now]=size[lc[now]]+size[rc[now]]+1;
    sum[now]=sum[lc[now]]+sum[rc[now]]+value[now];
    lmax[now]=max(max(lmax[lc[now]],sum[lc[now]]+value[now]+lmax[rc[now]]),0);
    rmax[now]=max(max(rmax[rc[now]],sum[rc[now]]+value[now]+rmax[lc[now]]),0);
    maxn[now]=value[now]+max(rmax[lc[now]]+lmax[rc[now]],0);
    if(lc[now]>0) maxn[now]=max(maxn[now],maxn[lc[now]]);
    if(rc[now]>0) maxn[now]=max(maxn[now],maxn[rc[now]]);
}
void push(int now,int x){
    value[now]=lazy2[now]=x;
    sum[now]=size[now]*x;
    lmax[now]=rmax[now]=max(sum[now],0);
    maxn[now]=max(sum[now],x);
}
void pushdown(int now){
    if(lazy2[now]!=INF){
        if(lc[now]!=0) push(lc[now],lazy2[now]);
        if(rc[now]!=0) push(rc[now],lazy2[now]);
        lazy2[now]=INF;
    }
    if(lazy1[now]!=0){
        swap(lc[now],rc[now]);
        swap(lmax[lc[now]],rmax[lc[now]]);
        swap(lmax[rc[now]],rmax[rc[now]]);
        if(lc[now]!=0) lazy1[lc[now]]^=1;
        if(rc[now]!=0) lazy1[rc[now]]^=1;
        lazy1[now]=0;
    }
}
void split(int now,int k,int &x,int &y){
    if(now==0) x=y=0;
    else{
        pushdown(now);
        if(size[lc[now]]+1<=k){
            x=now;
            split(rc[now],k-size[lc[now]]-1,rc[now],y);
        }else{
            y=now;
            split(lc[now],k,x,lc[now]);
        }
        pushup(now);
    }
}
int merge(int x,int y){
    if(x==0 || y==0) return x+y;
    if(heap[x]<heap[y]){
        pushdown(x);
        rc[x]=merge(rc[x],y);
        pushup(x);
        return x;
    }else{
        pushdown(y);
        lc[y]=merge(x,lc[y]);
        pushup(y);
        return y;
    }
}
void del(int now){
	use[++top]=now;
	if(lc[now]>0) del(lc[now]);
	if(rc[now]>0) del(rc[now]);
}
int build(int l,int r){
	if(l==r) return new_node(a[l]);
	int mid=(l+r)/2;
	return merge(build(l,mid),build(mid+1,r));
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    root=merge(root,build(1,n));
    while(m--){
        string s;
        cin>>s;
        if(s=="INSERT"){
        	int t,k;
        	scanf("%d%d",&t,&k);
        	if(k<=0) continue;
        	memset(a,0,sizeof(a));
        	for(int i=1;i<=k;i++) scanf("%d",&a[i]);
			split(root,t,x,y);
			root=merge(merge(x,build(1,k)),y);
		}else if(s=="DELETE"){
			int l,r;
			scanf("%d%d",&l,&r);
			if(r<=0) continue;
			r=l+r-1;
			split(root,r,x,y);
			split(x,l-1,x,z);
			del(z);
			root=merge(x,y);
		}else if(s=="MAKE-SAME"){
			int l,r,k;
			scanf("%d%d%d",&l,&r,&k);
			if(r<=0) continue;
			split(root,r,x,y);
			split(x,l-1,x,z);
			push(z,k);
			root=merge(merge(x,z),y);
		}else if(s=="REVERSE"){
			int l,r;
			scanf("%d%d",&l,&r);
			if(r<=0) continue;
			r=l+r-1;
			split(root,r,x,y);
			split(x,l-1,x,z);
			lazy1[z]^=1;
			swap(lmax[z],rmax[z]);
			root=merge(merge(x,z),y);
		}else if(s=="GET-SUM"){
			int l,r;
			scanf("%d%d",&l,&r);
			if(r<=0) continue;
			r=l+r-1;
			split(root,r,x,y);
			split(x,l-1,x,z);
			printf("%d\n",sum[z]);
			root=merge(merge(x,z),y);
		}else if(s=="MAX-SUM"){
			printf("%d\n",maxn[root]);
		}
    }
    return 0;
} 
2022/3/26 19:27
加载中...