hdu 3308 线段树+区间合并

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100010;
int tlen[maxn<<2];
int llen[maxn<<2],rlen[maxn<<2],lc[maxn<<2],rc[maxn<<2];
void push_up(int rt,int l,int r)
{
    int temp=max(tlen[rt<<1],tlen[rt<<1|1]);
    if(rc[rt<<1]<lc[rt<<1|1])
        temp=max(temp,rlen[rt<<1]+llen[rt<<1|1]);
    tlen[rt]=temp;

    int m=(l+r)/2;
    int lnum=m-l+1;
    int rnum=r-m;

    llen[rt]=llen[rt<<1];
    rlen[rt]=rlen[rt<<1|1];

    if(llen[rt<<1]==lnum && rc[rt<<1]<lc[rt<<1|1])
        llen[rt]+=llen[rt<<1|1];
    if(rlen[rt<<1|1]==rnum && lc[rt<<1|1]>rc[rt<<1])
        rlen[rt]+=rlen[rt<<1];

    lc[rt]=lc[rt<<1];
    rc[rt]=rc[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        int temp;
        scanf("%d",&temp);
        lc[rt]=rc[rt]=temp;
        tlen[rt]=llen[rt]=rlen[rt]=1;
        return;
    }
    int m=(l+r)/2;
    build(lson);
    build(rson);
    push_up(rt,l,r);
}

void update(int pos,int v,int l,int r,int rt)
{
    if(l==r)
    {
        rc[rt]=lc[rt]=v;
        return;
    }
    int m=(l+r)/2;
    if(pos<=m)
        update(pos,v,lson);
    else
        update(pos,v,rson);
    push_up(rt,l,r);
}

int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return tlen[rt];
    }
    int m=(l+r)/2;
    int ret=1;
    if(L<=m)
        ret=max(ret,query(L,R,lson));
    if(R>m)
        ret=max(ret,query(L,R,rson));
    if(rc[rt<<1]<lc[rt<<1|1])//两个子区间相交部分
        ret=max(ret,min(m-L+1,rlen[rt<<1])+min(R-m,llen[rt<<1|1]));
    return ret;
}
int t,n,m;
char op[2];

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--)
        {
            int a,b;
            scanf("%s%d%d",op,&a,&b);
            if(op[0]=='U')
            {
                a++;
                update(a,b,1,n,1);
            }
            else
            {
                a++,b++;
                printf("%d\n",query(a,b,1,n,1));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值