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

题意: 查询区间连续最长上升子串的长度 有单点修改操作

分析: 之前的风格草这种题太容易了 - - 随手写完就A了

代码:

//
//  Created by TaoSama on 2015-09-29
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, q, a[N];

typedef pair<int, int> Interval;

struct Node {
    Node() {}
    Node(int l, int r): l(l), r(r) {sz = r - l + 1;}
    int l, r, sz;
    int preR, sufL;
    Interval sub;
} dat[N << 2];

Interval getMax(Interval x, Interval y) {
    return x.second - x.first > y.second - y.first ? x : y;
}

Node push_up(Node x, Node y) {
    Node ret(x.l, y.r);
    ret.preR = x.preR;
    ret.sufL = y.sufL;
    ret.sub = getMax(x.sub, y.sub);
    if(a[x.r] < a[x.r + 1]) {
        if(ret.preR - ret.l + 1 == x.sz) ret.preR = y.preR;
        if(ret.r - ret.sufL + 1 == y.sz) ret.sufL = x.sufL;
        ret.sub = getMax(ret.sub, Interval(x.sufL, y.preR));
    }
    return ret;
}

void build(int l, int r, int rt) {
    if(l == r) {
        dat[rt] = Node(l, r);
        dat[rt].preR = dat[rt].sufL = l;
        dat[rt].sub = Interval(l, l);
        scanf("%d", a + l);
        return;
    }
    int m = l + r >> 1;
    build(l, m, rt << 1);
    build(m + 1, r, rt << 1 | 1);
    dat[rt] = push_up(dat[rt << 1], dat[rt << 1 | 1]);
}

void update(int o, int v, int rt) {
    if(dat[rt].l == dat[rt].r) {
        a[dat[rt].l] = v;
        return;
    }
    int m = dat[rt].l + dat[rt].r >> 1;
    if(o <= m) update(o, v, rt << 1);
    else update(o, v, rt << 1 | 1);
    dat[rt] = push_up(dat[rt << 1], dat[rt << 1 | 1]);
}

Node query(int L, int R, int rt) {
    if(L <= dat[rt].l && dat[rt].r <= R) return dat[rt];
    int m = dat[rt].l + dat[rt].r >> 1;

    if(L <= m && R > m) return push_up(query(L, R, rt << 1), query(L, R,
                                           rt << 1 | 1));
    else if(L <= m) return query(L, R, rt << 1);
    else return query(L, R, rt << 1 | 1);
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &q);
        build(0, n - 1, 1);
        while(q--) {
            char op[2]; int x, y;
            scanf("%s%d%d", op, &x, &y);
            if(*op == 'U') update(x, y, 1);
            else {
                Interval ans = query(x, y, 1).sub;
                printf("%d\n", ans.second - ans.first + 1);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值