10pts求条(玄关)
查看原帖
10pts求条(玄关)
950390
AlanFong楼主2024/8/23 16:23

rt
下载数据在本地跑没有问题,到了洛谷上就MLE/TLE

#include<bits/stdc++.h>
using namespace std;
struct node{
	int l,r,laz1,laz2,max;
};
node tree[4000005];
int a[1000005];
int upd(int p)
{
	tree[p].l=tree[2*p].l;
	tree[p].r=tree[2*p+1].r;
	tree[p].max=max(tree[2*p].max,tree[2*p+1].max);
}
void build(int p,int l,int r)
{
	if(l==r)
	{
		tree[p].l=l;
		tree[p].r=r;
		tree[p].max=a[l];
		return ;
	}
	int mid=(l+r)/2;
	build(2*p,l,mid);
	build(2*p+1,mid+1,r);
	upd(p);
}
void pushd(int p)
{
    if(tree[p].laz1!=0)
    {
        tree[2*p].laz1=tree[p].laz1;
        tree[2*p+1].laz1=tree[p].laz1;
        tree[2*p].max=tree[p].laz1;
        tree[2*p+1].max=tree[p].laz1;
    }
    else{
        if(tree[2*p].laz1!=0)
        {
            tree[2*p].laz1+=tree[p].laz2;
            tree[2*p].max+=tree[p].laz2;
        }else{
            tree[2*p].laz2+=tree[p].laz2;
            tree[2*p].max+=tree[p].laz2;
        }
        if(tree[2*p+1].laz1!=0)
        {
            tree[2*p+1].laz1+=tree[p].laz2;
            tree[2*p+1].max+=tree[p].laz2;
        }else{
            tree[2*p+1].laz2+=tree[p].laz2;
            tree[2*p+1].max+=tree[p].laz2;
        }
    }
    tree[p].laz2=0;
    tree[p].laz1=0;
}
void cover(int p,int l,int r,int x)
{
    if(l>r)
    {
        return;
    }
    if(tree[p].l==l&&tree[p].r==r)
    {
        tree[p].laz1=x;
        tree[p].laz2=0;
        tree[p].max=x;
        return;
    }
    if(tree[p].laz1!=0||tree[p].laz2!=0)
    {
        pushd(p);
    }
    if(r<=tree[2*p].r)
    {
        cover(2*p,l,r,x);
    }else if(l>=tree[2*p+1].l)
    {
        cover(2*p+1,l,r,x);
    }else{
        cover(2*p,l,tree[2*p].r,x);
        cover(2*p+1,tree[2*p+1].l,r,x);
    }
    upd(p);
}
void change2(int p,int l,int r,int x)
{
    if(l>r)
    {
        return;
    }
    if(tree[p].l==l&&tree[p].r==r)
    {
        if(tree[p].laz1!=0)
        {
            tree[p].laz1+=x;
        }else{
            tree[p].laz2+=x;
        }
        tree[p].max+=x;
        return;
    }
    if(tree[p].laz1!=0||tree[p].laz2!=0)
    {
        pushd(p);
    }
    if(r<=tree[2*p].r)
    {
        change2(2*p,l,r,x);
    }else if(l>=tree[2*p+1].l)
    {
        change2(2*p+1,l,r,x);
    }else{
        change2(2*p,l,tree[2*p].r,x);
        change2(2*p+1,tree[2*p+1].l,r,x);
    }
    upd(p);
}
int search(int p,int l,int r)
{
    if(l==tree[p].l&&r==tree[p].r)
    {
        return tree[p].max;
    }
    if(tree[p].laz1!=0||tree[p].laz2!=0)
    {
        pushd(p);
    }
    if(r<=tree[2*p].r)
    {
        return search(2*p,l,r);
    }else if(l>=tree[2*p+1].l)
    {
        return search(2*p+1,l,r);
    }else{
        return max(search(2*p,l,tree[2*p].r),search(2*p+1,tree[2*p+1].l,r));
    }
}
signed main()
{
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	build(1,1,n);
	while(q--)
	{
		int op;
		cin>>op;
		if(op==1)
		{
			int l,r,x;
			scanf("%d%d%d",&l,&r,&x);
			cover(1,l,r,x);
		}else if(op==2)
		{
			int l,r,x;
			scanf("%d%d%d",&l,&r,&x);
			change2(1,l,r,x);
		}else{
			int l,r;
			scanf("%d%d",&l,&r);
			cout<<search(1,l,r)<<endl;
		}
	}
	return 0;
}
2024/8/23 16:23
加载中...