码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-17 23:39:04      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
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].
 

Input
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).
 

Output
For each Q, output the answer.
 

Sample Input
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
 

Sample Output
1 1 4 2 3 1 2 5
 
比较普通的区间合并题型吧。记录左右段的,然后合并时根据端点情况即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e5+100;
int lsum[maxn<<2],rsum[maxn<<2];
int mm[maxn<<2],sum[maxn];
int t,n,m,x;
void pushup(int rs,int l,int r)
{
    lsum[rs]=lsum[rs<<1];
    rsum[rs]=rsum[rs<<1|1];
    mm[rs]=max(mm[rs<<1],mm[rs<<1|1]);
    int mid=(l+r)>>1;
    if(sum[mid]<sum[mid+1])
    {
        if(lsum[rs<<1]==mid-l+1)
            lsum[rs]+=lsum[rs<<1|1];
        if(rsum[rs<<1|1]==r-mid)
            rsum[rs]+=rsum[rs<<1];
        mm[rs]=max(mm[rs],rsum[rs<<1]+lsum[rs<<1|1]);
    }
}
void build(int rs,int l,int r)
{
    if(l==r)
    {
        lsum[rs]=rsum[rs]=mm[rs]=1;
        return  ;
    }
    int mid=(l+r)>>1;
    build(rs<<1,l,mid);
    build(rs<<1|1,mid+1,r);
    pushup(rs,l,r);
}
void update(int x,int c,int l,int r,int rs)
{
    if(l==r)
    {
        sum[x]=c;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid)  update(x,c,l,mid,rs<<1);
    else  update(x,c,mid+1,r,rs<<1|1);
    pushup(rs,l,r);
}
int query(int x,int y,int l,int r,int rs)
{
    if(l>=x&&r<=y)
        return  mm[rs];
    int mid=(l+r)>>1;int res=0;
    if(x<=mid)  res=max(res,query(x,y,l,mid,rs<<1));
    if(y>mid)   res=max(res,query(x,y,mid+1,r,rs<<1|1));
    if(sum[mid]<sum[mid+1]&&x<=mid&&y>mid)
        res=max(res,min(mid-x+1,rsum[rs<<1])+min(y-mid,lsum[rs<<1|1]));
    return res;
}
int main()
{
    int x,y;char str[10];
    scanf("%d",&t);
    while(t--)
    {
        CLEAR(mm,0);CLEAR(lsum,0);CLEAR(rsum,0);
        scanf("%d%d",&n,&m);
        REPF(i,1,n) scanf("%d",&sum[i]);
        build(1,1,n);
        while(m--)
        {
            scanf("%s%d%d",str,&x,&y);
            if(str[0]=='U')
               update(x+1,y,1,n,1);
            else
            {
                int ans=query(x+1,y+1,1,n,1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}


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

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/44359015

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!