【JZOJjunior1380】【线段树】最大值(新版)

本文介绍了一种使用Splay树结合懒标记处理区间更新与查询问题的方法。具体包括Splay树的基本操作、懒标记的维护及应用,适用于解决大规模数据的区间修改与最大值查询等问题。

problem

题目描述

在N(1<=N<=100000)个数A1…An组成的序列上进行M(1<=M<=100000)次操作,操作有两种:

(1)1 L R C:表示把A[L]到A[R]增加C(C的绝对值不超过10000);

(2)2 L R:询问A[L]到A[R]之间的最大值。

输入

第一行输入N(1<=N<=100000),表示序列的长度,接下来N行输入原始序列;接下来一行输入M(1<=M<=100000)表示操作的次数,接下来M行,每行为1 L R C或2 L R

输出

对于每个操作(2)输出对应的答案。

样例输入

5
1
2
3
4
5
3
2 1 4
1 1 3 3
2 3 5

样例输出

4
6

数据范围限制

提示

【限制】

保证序列中的所有的数都在longint范围内


analysis

  • splay

  • 这就是最基础的splaysplay操作,不多讲

  • lazytaglazy−tag标记就那么打上去

  • 下传lazytaglazy−tag标记的话,可以不用在rotaterotate的时候下传,可以像LCTLCT一样在整一次splaysplay之前暴力下传

  • 用了打LCTLCT的那种比较好看的简短版可以当板子来CO


code

#include<stdio.h>
#define MAXN 100005

using namespace std;

int t[MAXN][2];
int mx[MAXN],fa[MAXN],lazy[MAXN];
int a[MAXN],st[MAXN];
int n,m;

int max(int x,int y)
{
    return x>y?x:y;
}

int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0' || '9'<ch)
    {
        if (ch=='-')f=-1;
        ch=getchar();   
    }
    while ('0'<=ch && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

void update(int x)
{
    mx[x]=max(a[x],max(mx[t[x][0]],mx[t[x][1]]));
}

void clear(int x)
{
    if (t[x][0])
    {
        a[t[x][0]]+=lazy[x];
        mx[t[x][0]]+=lazy[x];
        lazy[t[x][0]]+=lazy[x];
    }
    if (t[x][1])
    {
        a[t[x][1]]+=lazy[x];
        mx[t[x][1]]+=lazy[x];
        lazy[t[x][1]]+=lazy[x];
    }
    lazy[x]=0;
}

void downdata(int x)
{
    st[0]=0;
    while (x)st[++st[0]]=x,x=fa[x];
    while (st[0])clear(st[st[0]--]);  
}

int lr(int x)
{
    return t[fa[x]][1]==x;
}

void rotate(int x) 
{
    int y=fa[x],k=lr(x);
    t[y][k]=t[x][!k];

    if (t[x][!k])fa[t[x][!k]]=y;
    fa[x]=fa[y];

    if (fa[y])t[fa[y]][lr(y)]=x;    
    t[x][!k]=y;

    fa[y]=x;
    update(y),update(x);
}

void splay(int x,int y) 
{
    downdata(x);
    while (fa[x]!=y)
    {
        if (fa[fa[x]]!=y)
        {
            if (lr(x)==lr(fa[x]))rotate(fa[x]); 
            else rotate(x);
        } 
        rotate(x);
    }
}

int main()
{
    n=read();
    mx[0]=-1e9;
    for (int i=1;i<=n;i++)a[i+1]=read(),fa[i]=i+1,t[i+1][0]=i,update(i+1);
    fa[n+1]=n+2,t[n+2][0]=n+1,update(n+2);
    m=read();
    while (m--)
    {
        int z=read(),x=read()+1,y=read()+1;
        splay(x-1,0),splay(y+1,x-1);
        if (z==1)
        {
            int temp=read();
            a[t[y+1][0]]+=temp;
            mx[t[y+1][0]]+=temp;
            lazy[t[y+1][0]]+=temp;
        }
        else printf("%d\n",mx[t[y+1][0]]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值