Hdu3308LCIS线段树

 区间合并,和hotel那题差不多,只不过在合并区间的时候加和条件,变成左右儿子相连处是否上升,并且,对父亲的更新取得是最大值。

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <climits>

#include <string>

#include <iostream>

#include <map>

#include <cstdlib>

#include <list>

#include <set>

#include <queue>

#include <stack>

#include<math.h>

using namespace std;



#define lson l,mid,rt<<1

#define rson mid+1,r,rt<<1|1

const int maxn = 111111; int sum[maxn << 2], lsum[maxn << 2], rsum[maxn << 2]; int num[maxn]; void up(int l, int r, int rt) {

    lsum[rt] = lsum[rt << 1];

    rsum[rt] = rsum[rt << 1 | 1];

    sum[rt] = max(sum[rt << 1], sum[rt << 1 | 1]); int mid = (l + r) >> 1; int m = r - l + 1; if (num[mid] < num[mid + 1]){ if (lsum[rt] == (m - (m >> 1))) lsum[rt] += lsum[rt << 1 | 1]; if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt << 1];

        sum[rt] = max(sum[rt], lsum[rt << 1 | 1] + rsum[rt << 1]); } } void build(int l, int r, int rt) { if (l == r){

        sum[rt] = lsum[rt] = rsum[rt] = 1;

        scanf("%d", &num[l]); return; } int mid = (l + r) >> 1;

    build(lson);

    build(rson);

    up(l, r, rt); } void update(int key, int add, int l, int r, int rt) { if (l == r){

        num[l] = add; return; } int mid = (l + r) >> 1; if (key <= mid) update(key, add, lson); else update(key, add, rson);

    up(l, r, rt); } int ask(int L, int R, int l, int r, int rt) { if (L <= l&&r <= R) return sum[rt]; int mid = (l + r) >> 1; int ans = 0; if (L <= mid) ans = max(ans, ask(L, R, lson)); if (R > mid) ans = max(ans, ask(L, R, rson)); if (num[mid] < num[mid + 1]){ int a = min(mid - L + 1, rsum[rt << 1]) + min(R - mid, lsum[rt << 1 | 1]);

        ans = max(ans, a); } return ans; } int main() { char str[100]; int Icase; int n, m, a, b;

    scanf("%d", &Icase); while (Icase--){

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

        build(1, n, 1); for (int i = 0; i < m; i++){

            scanf("%s%d%d", str, &a, &b); if (str[0] == 'U'){

                a++;

                update(a, b, 1, n, 1); } else{

                a++; b++;

                cout << ask(a, b, 1, n, 1) << endl; } } } return 0; }

 

你可能感兴趣的:(HDU)