UOJ #164. 【清华集训2015】V(神奇的线duang树)

这篇博客介绍了UOJ #164的题目解析和解决方案,涉及区间操作与单点查询问题。利用线段树维护max(x+a, b)函数的状态,并处理区间加、减、赋值和查询操作。博主分享了北大小姐姐的PPT中关于线段树的标记合并方法,特别提示在区间加减操作时注意避免INF溢出。代码实现中,博主以喵声结尾,暗示代码已隐藏或省略。" 108050444,8630400,PyTorch损失函数详解:从交叉熵到CTC损失,"['pytorch', '深度学习', '神经网络', '损失函数', '模型训练']

传送门

http://uoj.ac/problem/164


题解

题目大意
n个数,m次操作
1.区间加、减一个数并对0取max
2.区间赋值
3.单点查询当前值
4.单点查询 历史最大值
n,m≤5*10^5

根据北大小姐姐的PPT,应该是这样的:

线段树维护:f(x)=max(x+a,b)函数的当前值和历史最大值(一定也是一个同样形式的函数),即一个标记为(a,b,ma,mb)

标记合并:
f1(f(x)):(a,b,ma,mb)+(a1,b1,ma1,mb1)->(a+a1,max(b1,b+a1),max(ma,a+ma1),max(mb,mb1,b+ma1))

区间加:
(a,b,ma,mb)->(a,b,ma,mb)+(x,0,x,0)

区间减:
(a,b,ma,mb)->(a,b,ma,mb)+(-x,0,-x,0)

区间覆盖:
(a,b,ma,mb)->(a,b,ma,mb)+(-inf,x,-inf,x)

最后返回f(x),其中x+a是加的,b是覆盖的,带m的表示历史最大值。一开始赋值(val, 0)和没有的操作填0就保证了不会取到0以下。每次传标记都保证a,b至少有一个是“有效”的,答案取其中的max。单点查询所以非叶子信息是不用维护的。

另外,比较坑的一点是INF不要减爆了,所以还要在a+a1的时候与-INF取个max。

时间复杂度O(m log n)


代码

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define maxn 500100
#define INF 1000000000000000LL

using namespace std;

typedef long long LL; 

struct Tnode{
    LL a, b;
}ever[maxn<<2], now[maxn<<2];

int n, m;

LL A[maxn];

void Build(int root, int L, int R){
    if(L == R){
        ever[root].a = A[L];
        ever[root].b = 0LL;
        now[root].a = A[L];
        now[root].b = 0LL;
        return;
    }

    int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;

    Build(Lson, L, mid);
    Build(Rson, mid+1, R);
}

void Down(int root, int Lson, int Rson){

    if(!now[root].a && !now[root].b && !ever[root].a && !ever[root].b)  return;

    ever[Lson].a = max(ever[Lson].a, now[Lson].a+ever[root].a);
    ever[Lson].b = max(ever[Lson].b, max(ever[root].b, now[Lson].b+ever[root].a));
    now[Lson].a = max(now[Lson].a+now[root].a, -INF);
    now[Lson].b = max(now[Lson].b+now[root].a, now[root].b);

    ever[Rson].a = max(ever[Rson].a, now[Rson].a+ever[root].a);
    ever[Rson].b = max(ever[Rson].b, max(ever[root].b, now[Rson].b+ever[root].a));
    now[Rson].a = max(now[Rson].a+now[root].a, -INF);
    now[Rson].b = max(now[Rson].b+now[root].a, now[root].b);

    ever[root].a = ever[root].b = now[root].a = now[root].b = 0LL;
}

void Update(int root, int L, int R, int x, int y, LL u, LL v){
    if(x > R || y < L)  return;
    if(x <= L && y >= R){
        ever[root].a = max(ever[root].a, now[root].a+u);
        ever[root].b = max(ever[root].b, max(v, now[root].b+u));
        now[root].a = max(now[root].a+u, -INF);
        now[root].b = max(now[root].b+u, v);
        return;
    }

    int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;

    Down(root, Lson, Rson);

    Update(Lson, L, mid, x, y, u, v);
    Update(Rson, mid+1, R, x, y, u, v);
}

LL Query(int root, int L, int R, int x, bool sign){

    if(L == R){
        if(!sign)  return max(now[root].a, now[root].b);
        return max(ever[root].a, ever[root].b);
    }

    int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1;

    Down(root, Lson, Rson);

    if(x <= mid)  return Query(Lson, L, mid, x, sign);
    return Query(Rson, mid+1, R, x, sign);
}

int main(){

    scanf("%d%d", &n, &m);

    for(int i = 1; i <= n; i++)
        scanf("%lld", &A[i]);

    Build(1, 1, n);

    int op, l, r;
    LL x;

    for(int i = 1; i <= m; i++){
        scanf("%d", &op);
        if(op == 1){
            scanf("%d%d%lld", &l, &r, &x);
            Update(1, 1, n, l, r, x, 0LL);
        }
        else if(op == 2){
            scanf("%d%d%lld", &l, &r, &x);
            Update(1, 1, n, l, r, -x, 0LL);
        }
        else if(op == 3){
            scanf("%d%d%lld", &l, &r, &x);
            Update(1, 1, n, l, r, -INF, x);
        }
        else if(op == 4){
            scanf("%d", &r);
            printf("%lld\n", Query(1, 1, n, r, 0));
        }
        else{
            scanf("%d", &r);
            printf("%lld\n", Query(1, 1, n, r, 1));
        }
    }

    return 0;
}

这里写图片描述

喵~喵~喵~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值