首页 > ACM题库 > HDU-杭电 > HDU 3308-LCIS-线段树-[解题报告]HOJ
2014
03-16

HDU 3308-LCIS-线段树-[解题报告]HOJ

LCIS

问题描述 :

Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].

输入:

T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
OR
Q A B(0<=A<=B< n).

输出:

T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
OR
Q A B(0<=A<=B< n).

样例输入:

1
10 10
7 7 3 3 5 9 9 8 1 8 
Q 6 6
U 3 4
Q 0 1
Q 0 5
Q 4 7
Q 3 5
Q 0 2
Q 4 6
U 6 10
Q 0 9

样例输出:

1
1
4
2
3
1
2
5

~~

#include <iostream>
#include <cstring>
#include <cstdio>
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
const int MAXN=100005;
struct node
{
    int l,r;
    int msum;
    int lsum,rsum;
    int mid()
    {
        return (l+r)>>1;
    }
};
node tree[MAXN*4];
int num[MAXN];
inline void pushup(int pos)
{
    tree[pos].msum=max(tree[lson].msum,tree[rson].msum);
    tree[pos].lsum=tree[lson].lsum;
    tree[pos].rsum=tree[rson].rsum;
    if(num[tree[lson].r]<num[tree[rson].l])
    {
        tree[pos].msum=max(tree[pos].msum,tree[lson].rsum+tree[rson].lsum);
        if(tree[lson].lsum==tree[lson].r-tree[lson].l+1)
            tree[pos].lsum+=tree[rson].lsum;
        if(tree[rson].rsum==tree[rson].r-tree[rson].l+1)
            tree[pos].rsum+=tree[lson].rsum;
    }
}
void build(int l,int r,int pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    if(l==r)
    {
        tree[pos].lsum=tree[pos].rsum=tree[pos].msum=1;
        return ;
    }
    int mid=tree[pos].mid();
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(pos);
}
void update(int a,int b,int pos)
{
    if(tree[pos].l==tree[pos].r)
    {
        num[a]=b;
        return ;
    }
    int mid=tree[pos].mid();
    if(a<=mid)
        update(a,b,lson);
    else
        update(a,b,rson);
    pushup(pos);
}
int queryL(int l,int r,int pos)
{
    if(r-l+1>=tree[pos].lsum)
        return tree[pos].lsum;
    else
        return r-l+1;
}
int queryR(int l,int r,int pos)
{
    if(r-l+1>=tree[pos].rsum)
        return tree[pos].rsum;
    else
        return r-l+1;
}
int query(int l,int r,int pos)
{
    if(l==tree[pos].l&&r==tree[pos].r)
        return tree[pos].msum;
    int mid=tree[pos].mid();
    if(r<=mid)
        return query(l,r,lson);
    else if(l>mid)
        return query(l,r,rson);
    else
    {
        int res=0;
        if(num[tree[lson].r]<num[tree[rson].l])
            res=max(res,queryR(l,mid,lson)+queryL(mid+1,r,rson));
        res=max(res,query(l,mid,lson));
        res=max(res,query(mid+1,r,rson));
        return res;
    }
}

int main()
{
    int n,m,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int i;
        for(i=1;i<=n;i++)
            scanf("%d",&num[i]);
        build(1,n,1);
        char word[2];
        for(i=1;i<=m;i++)
        {
            int a,b;
            scanf("%s%d%d",word,&a,&b);
            if(word[0]=='U')
                update(a+1,b,1);
            else
            {
                int x=query(a+1,b+1,1);
                printf("%d\n",x);
            }
        }
    }

    return 0;
}

参考:http://blog.csdn.net/juststeps/article/details/9531411


  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。