UESTC 360 Another LCIS 线段树

题目

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=67377

题目来源:某线段树合集(ACM大牛总结的线段树专辑,超经典的)练习题

简要题意:可以对区间所有数进行加,然后询问某区间内最长的上升子段。

题解

这题还是比较难的,需要维护比较多的数据。

一个是要维护区间左右端点的值。

要维护区间最长的上升前缀和上升后缀的长度。

然后最后要维护一个区间的答案。

需要特殊注意的是穿过中间的情况,这个的话就是要通过左边右端点和右边左端点去更新答案了。

然后还有就是左边或者右边全上升的时候lmax和rmax可能还要加。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 1e5+5;

struct Node {
    int lmax, rmax, lv, rv, mx, lazy;

    void reset() {
        lmax = rmax = lv = rv = mx = lazy = 0;
    }

    void add(int a) {
        lv += a;
        rv += a;
        lazy += a;
    }
};

struct SegmentTree {
#define lson (rt<<1)
#define rson ((rt<<1)|1)
#define MID ((L+R)>>1)
#define lsonPara lson, L, MID
#define rsonPara rson, MID+1, R

    const static int TN = N << 2;

    Node t[TN];

    void pushUp(int rt, int L, int R) {
        t[rt].lv = t[lson].lv, t[rt].rv = t[rson].rv;
        t[rt].lmax = t[lson].lmax, t[rt].rmax = t[rson].rmax;
        t[rt].mx = max(t[lson].mx, t[rson].mx);

        if (t[lson].rv < t[rson].lv) {
            t[rt].mx = max(t[rt].mx, t[lson].rmax + t[rson].lmax);
            if (t[lson].lmax == (MID - L + 1)) {
                t[rt].lmax = t[lson].lmax + t[rson].lmax;
            }
            if (t[rson].rmax == (R - MID)) {
                t[rt].rmax = t[lson].rmax + t[rson].rmax;
            }
        }
    }

    void pushDown(int rt, int L, int R) {
        if (t[rt].lazy) {
            t[lson].add(t[rt].lazy);
            t[rson].add(t[rt].lazy);
            t[rt].lazy = 0;
        }
    }

    void build(int rt, int L, int R) {
        t[rt].reset();
        if (L == R) {
            int x;
            scanf("%d", &x);
            t[rt].lv = t[rt].rv = x;
            t[rt].mx = t[rt].lmax = t[rt].rmax = 1;
        } else {
            build(lsonPara);
            build(rsonPara);
            pushUp(rt, L, R);
        }
    }

    void modify(int rt, int L, int R, int l, int r, int v) {
        if (l > R || r < L) return;
        if (l <= L && r >= R) {
            t[rt].add(v);
        } else {
            pushDown(rt, L, R);
            modify(lsonPara, l, r, v);
            modify(rsonPara, l, r, v);
            pushUp(rt, L, R);
        }
    }

    int query(int rt, int L, int R, int l, int r) {
        if (l > R || r < L) return 0;
        if (l <= L && r >= R) return t[rt].mx;
        pushDown(rt, L, R);
        int ans = 0;
        ans = max(query(lsonPara, l, r), query(rsonPara, l, r));
        if (t[lson].rv < t[rson].lv) {
            int ll = max(MID - t[lson].rmax + 1, l);
            int rr = min(MID + t[rson].lmax, r);
            ans = max(ans, rr - ll + 1);
        }
        return ans;
    }
};

SegmentTree st;

char s[5];
int main() {
    int t, n, m, l, r, v, cas = 1;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        st.build(1, 1, n);

        printf("Case #%d:\n", cas++);
        while (m--) {
            scanf("%s%d%d", s, &l, &r);
            if (s[0] == 'q') {
                printf("%d\n", st.query(1, 1, n, l, r));
            } else {
                scanf("%d", &v);
                st.modify(1, 1, n, l, r, v);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值