hdu 3308 LCIS(线段树)

pid=3308" target="_blank" style="">题目链接:hdu 3308 LCIS

题目大意:给定一个序列,两种操作:

  • Q l r:查询区间l,r中的最长连续递增序列长度
  • U p x:将位置p上的数改成x

解题思路:线段树上的区间合并,这是在左右子树合并的时候要推断一下是否满足递增就可以。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e5 + 5;

int N, M, a[maxn];

#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2];
int L[maxn << 2], R[maxn << 2], S[maxn << 2];

void pushup (int u) {
    int mid = (lc[u] + rc[u]) / 2;
    S[u] = max(max(S[lson(u)], S[rson(u)]), (a[mid] < a[mid+1] ? R[lson(u)] + L[rson(u)] : 0));
    L[u] = L[lson(u)] + (L[lson(u)] == rc[lson(u)] - lc[lson(u)] + 1 && a[mid] < a[mid + 1] ?

L[rson(u)] : 0);
    R[u] = R[rson(u)] + (R[rson(u)] == rc[rson(u)] - lc[rson(u)] + 1 && a[mid] < a[mid + 1] ?

R[lson(u)] : 0);
}

void build (int u, int l, int r) {
    lc[u] = l;
    rc[u] = r;

    if (l == r) {
        L[u] = R[u] = S[u] = 1;
        return;
    }

    int mid = (l + r) / 2;
    build(lson(u), l, mid);
    build(rson(u), mid + 1, r);
    pushup(u);
}

void modify (int u, int x, int v) {
    if (lc[u] == x && rc[u] == x) {
        a[x] = v;
        return;
    }

    int mid = (lc[u] + rc[u]) / 2;
    if (x <= mid)
        modify(lson(u), x, v);
    else
        modify(rson(u), x, v);
    pushup(u);
}

int query (int u, int l, int r) {
    if (l <= lc[u] && rc[u] <= r)
        return S[u];

    int mid =(lc[u] + rc[u]) / 2, ret;
    if (r <= mid)
        ret = query(lson(u), l, r);
    else if (l > mid)
        ret = query(rson(u), l, r);
    else {
        int ll = query(lson(u), l, r);
        int rr = query(rson(u), l, r);

        int A = min(R[lson(u)], mid - l + 1);
        int B = min(L[rson(u)], r - mid);

        ret = max(max(ll, rr), a[mid] < a[mid + 1] ?

A + B : 0);
    }
    return ret;
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d", &N, &M);
        for (int i = 0; i < N; i++)
            scanf("%d", &a[i]);

        build(1, 0, N-1);

        int l, r;
        char op[5];
        while (M--) {
            scanf("%s%d%d", op, &l, &r);
            if (op[0] == ‘U‘)
                modify(1, l, r);
            else
                printf("%d\n", query(1, l, r));
        }
    }
    return 0;
}
时间: 2024-08-10 19:03:27

hdu 3308 LCIS(线段树)的相关文章

HDU 3308 LCIS 线段树 区间更新

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目描述: 有两种操作, U x y  , 第xth赋值为y .Q x y , 查询区间x-y的最长连续上升子序列的长度L 解题思路: 对于线段树不好的我依然好难.....有太多细节需要注意了....但是这是一道很好的题, 一段区间的L可能从三个地方来, 一种是中间, 一种是以左起点为开头的, 一种是以右起点结尾的, 这样查询的时候就要注意了: 如果两段的中间值是a[m] < a[m+1]

HDU 3308 LCIS (线段树区间合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[mid + 1] > a[mid],写的细心点就好了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN = 1

HDU 3308 LCIS (线段树&#183;单点更新&#183;区间合并)

题意  给你一个数组  有更新值和查询两种操作  对于每次查询  输出对应区间的最长连续递增子序列的长度 基础的线段树区间合并  线段树维护三个值  对应区间的LCIS长度(lcis)  对应区间以左端点为起点的LCIS长度(lle)  对应区间以右端点为终点的LCIS长度(lri)  然后用val存储数组对应位置的值  当val[mid + 1] > val[mid] 的时候就要进行区间合并操作了 #include <cstdio> #include <algorithm>

HDU 3308 LCIS(线段树区间合并)

Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting from 0) Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b]. Input T in the first line, indicat

hdu 3308(线段树)

线段树求区间最长连续上升 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int maxn=1e5+100; int ll[maxn<<2],rr[maxn<<2];//左边的连续上升子序列长度和右边的连续上升子序列长度 int a[maxn]; int mx[maxn<

HDU 3308 LCIS(线段树)

Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index counting from 0)Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b]. Input T in the first line, indicating

HDU - 3308 - LCIS (线段树 - 区间合并)

题目传送:LCIS 线段树,区间合并,一次过啦,没有纠结,这几天过的最愉快的一个题 思路:求最长连续上升子序列,外带单点更新,经典的线段树题目.具体看代码注释 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack>

HDU 3308 LCIS (端点更新+区间合并)

刚刚做了两道LCIS,碰到这道线段树,脑抽了似的写 线段树+dp(LCIS),贡献一发TLE. 才想到要区间合并,query函数写了好久.下面有详细注释,参见代码吧~~欢迎点赞,欢迎卖萌~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 #include<cstdio> #inc

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid