当前位置: 代码迷 >> 综合 >> hdu 3308 LCIS 线段树 区间合并
  详细解决方案

hdu 3308 LCIS 线段树 区间合并

热度:32   发布时间:2024-01-13 18:08:44.0

好题一个,区间合并问题

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

跟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;
}