hdu 3308 LCIS (线段树单点更新,区间合并)

题目链接

题意:长度为n的序列,单点更新,或者询问某一个区间中最长连续严格递增序列的长度是多少。(此处的连续为位置连续,并非数值连续,也就是3,5,7,9,这样的就是满足题意的长度为4的序列)

思路:线段树区间合并。维护三个域。

mx:区间中最长连续严格递增序列的长度

lm:包含区间左端点的最长连续严格递增序列的长度。

rm:包含区间右端点的最长连续严格递增序列的长度。

PushUp的时候,一个区间的答案显然可以从左右两个子区间的最大值得到。

还有一种可能是左右区间各取一部分,此时必须满足左区间的右端点值严格小于右区间的左端点值。

需要注意的是,如果某区间的最长连续严格递增子序列的长度等于区间长度,那么该区间可以向相应方向延伸。

查询的时候也是如此,要记得查询的时候,某一段区间对答案贡献不会超过区间长度。。

hdu有点坑。。。函数名不能命名为update...update2也不行2333

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年11月26日 星期六 18时12分49秒
  4File Name :code/hdu/3308.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31const int N=1E5+7;
 32int n,m;
 33int a[N];
 34struct Tree
 35{
 36    int mx,lm,rm;
 37}tree[N<<2];
 38void PushUp(int rt,int l,int r)
 39{
 40    tree[rt].lm = tree[rt<<1].lm;
 41    tree[rt].rm = tree[rt<<1|1].rm;
 42    tree[rt].mx = max(tree[rt<<1].mx,tree[rt<<1|1].mx);
 43    int m = (l+r)>>1;
 44    if (a[m]<a[m+1])
 45    {
 46	if (tree[rt<<1].lm==m-l+1) tree[rt].lm+=tree[rt<<1|1].lm;
 47	if (tree[rt<<1|1].rm == r-m) tree[rt].rm += tree[rt<<1].rm;
 48	tree[rt].mx = max(tree[rt].mx,tree[rt<<1].rm + tree[rt<<1|1].lm);
 49    }
 50}
 51void build( int l,int r,int rt)
 52{
 53    if (l==r)
 54    {
 55	tree[rt].mx = tree[rt].lm = tree[rt].rm = 1;
 56	return;
 57    }
 58    int m = (l+r)>>1;
 59    build(lson);
 60    build(rson);
 61    PushUp(rt,l,r);
 62}
 63void upd( int p,int sc,int l,int r,int rt)
 64{
 65    if (l==r)
 66    {
 67	a[p] = sc;
 68	return ;
 69    }
 70    int m = (l+r)>>1;
 71    if (p<=m) upd(p,sc,lson);
 72    else upd(p,sc,rson);
 73    PushUp(rt,l,r);
 74}
 75int query(int L,int R,int l,int r,int rt)
 76{
 77    if (L<=l&&r<=R) return tree[rt].mx;
 78    int m = (l+r)>>1;
 79    int ret = 0 ;
 80    if (L<=m) ret = max(ret,query(L,R,lson)); 
 81    if (R>=m+1) ret = max(ret,query(L,R,rson));
 82    if (a[m]<a[m+1])
 83    {
 84	ret = max(ret,min(tree[rt<<1|1].lm,R-m)+min(tree[rt<<1].rm,m-L+1)); 
 85    }
 86    return ret;
 87}
 88int main()
 89{
 90	int T;
 91	cin>>T;
 92	while (T--)
 93	{
 94	    scanf("%d %d",&n,&m);
 95	    for ( int i = 1; i <= n ; i++) scanf("%d",&a[i]);
 96	    ms(tree,0);
 97	    build(1,n,1);
 98	    while (m--)
 99	    {
100		char opt[3];
101		int x,y;
102		scanf("%s %d %d",opt,&x,&y);
103		if (opt[0]=='Q') printf("%d\n",query(x+1,y+1,1,n,1));
104		else upd(x+1,y,1,n,1);
105	    }
106	}
107    return 0;
108}