hdu 3308 LCIS 线段树 区间合并

区间合并问题的高效解决
本文探讨了区间合并问题的解决方法,通过使用lmx、rmx和mx等概念,实现了一个简洁高效的算法来确定区间内最长连续上升序列的长度。通过实例分析,展示了算法的应用与优势。

好题一个,区间合并问题

大意就是问某个区间内最长连续上升序列的长度

跟POJ 3667 Hotel 差不多,貌似还更简单一些,因为只有单点更新

用到了lmx, rmx, mx,表示从左端点往右的连续长度,右端点往左的连续长度,区间内的最大连续长度

/*
ID: sdj22251
PROG: inflate
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 100005
#define INF 1000000000
#define L(x) x<<1
#define R(x) x<<1|1
#define PI acos(-1.0)
#define eps 1e-7
using namespace std;
int a[MAXN];
struct node
{
    int left, right, mid;
    int lmx, rmx, mx;
}tree[4 * MAXN];
void up(int C)
{
    tree[C].lmx = tree[L(C)].lmx;
    tree[C].rmx = tree[R(C)].rmx;
    if(tree[L(C)].lmx == tree[L(C)].right - tree[L(C)].left + 1 && a[tree[R(C)].left] > a[tree[L(C)].right])
    tree[C].lmx += tree[R(C)].lmx;
    if(tree[R(C)].rmx == tree[R(C)].right - tree[R(C)].left + 1 && a[tree[R(C)].left] > a[tree[L(C)].right])
    tree[C].rmx += tree[L(C)].rmx;
    int tmp = 0;
    if(a[tree[R(C)].left] > a[tree[L(C)].right]) tmp = tree[L(C)].rmx + tree[R(C)].lmx;
    tree[C].mx = max(tmp, max(tree[L(C)].mx, tree[R(C)].mx));
    tree[C].mx = max(tree[C].mx, max(tree[C].lmx, tree[C].rmx));
}
void make_tree(int s, int e, int C)
{
    tree[C].left = s;
    tree[C].right = e;
    tree[C].mid = (s + e) >> 1;
    tree[C].lmx = tree[C].rmx = tree[C].mx = 1;
    if(s == e) return;
    make_tree(s, tree[C].mid, L(C));
    make_tree(tree[C].mid + 1, e, R(C));
    up(C);
}
void update(int p,  int C)
{
    if(tree[C].left == tree[C].right)return;
    if(tree[C].mid >= p) update(p, L(C));
    else update(p, R(C));
    up(C);
}
int query(int s, int e, int C)
{
    if(tree[C].left >= s && tree[C].right <= e) return tree[C].mx;
    int s1 = 0, s2 = 0;
    if(tree[C].mid >= s) s1 = query(s, e, L(C));
    if(tree[C].mid < e) s2 = query(s, e, R(C));
    int t = max(s1, s2);
    if(a[tree[C].mid] < a[tree[C].mid + 1])
    t = max(t, min(tree[C].mid - s + 1, tree[L(C)].rmx) + min(e - tree[C].mid, tree[R(C)].lmx));
    return t;
}
int main()
{
    int T, n, m, x, y;
    char s[5];
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
        make_tree(0, n - 1, 1);
        while(m--)
        {
            scanf("%s%d%d", s, &x, &y);
            if(s[0] == 'Q') printf("%d\n", query(x, y, 1));
            else {a[x] = y; update(x, 1);}
        }
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值