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

线段树区间合并LCIShdu3308

时间:2015-08-14 13:52:27      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

#include<bits/stdc++.h>
using namespace std;
#define N 100100
int a[N];
struct {
    int left,right,c;
    int ln,rn;
    int ls,rs,ms;
}b[N*4];
void pushup(int i){
    b[i].ls=b[i*2].ls; b[i].rs=b[2*i+1].rs;
    b[i].ln=b[i*2].ln; b[i].rn=b[2*i+1].rn;
    b[i].ms=max(b[2*i].ms,b[2*i+1].ms);
    if(b[2*i].rn<b[2*i+1].ln){
        if(b[2*i].ls == b[2*i].c) b[i].ls+=b[2*i+1].ls;
        if(b[2*i+1].ls == b[2*i+1].c) b[i].rs+=b[2*i].rs;
        b[i].ms=max(b[i].ms,b[2*i+1].ls+b[i*2].rs);
    }
}
void build(int left,int right,int i){
    int mid;
    b[i].left=left;b[i].right=right;b[i].c=right-left+1;
    if(left==right) {
        b[i].ln=b[i].rn=a[left];
        b[i].ls=b[i].rs=b[i].ms=1;
        return ;
    }
    mid = (left+right)/2;
    build(left,mid,2*i);
    build(mid+1,right,2*i+1);
    pushup(i);
}
void Insert(int i,int t,int m){
    if(b[i].left==b[i].right){
        b[i].ln=b[i].rn=m;
        return ;
    }
    int mid=(b[i].left+b[i].right)>>1;
    if(t<=mid) Insert(2*i,t,m);
    if(t>mid) Insert(2*i+1,t,m);
    pushup(i);
}
int Query(int l,int r,int i){
    if(b[i].left>=l && b[i].right<=r) return b[i].ms;
    int mid=(b[i].right+b[i].left)>>1,ans=0;
    if(l<=mid) ans=max(ans,Query(l,r,2*i));
    if(r>mid) ans=max(ans,Query(l,r,2*i+1));
    if(b[2*i].rn<b[2*i+1].ln) ans = max(ans , min(mid-l+1,b[2*i].rs)+min(r-mid,b[2*i+1].ls));
        return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){

        memset(b,0,sizeof(b));
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        build(1,n,1);
        while(m--){
            char q[10];
            int x,y;
            getchar();
            scanf("%s%d%d",q,&x,&y);
            if(q[0]=='U') Insert(1,x+1,y);
            else printf("%d\n",Query(x+1,y+1,1));
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

线段树区间合并LCIShdu3308

标签:

原文地址:http://blog.csdn.net/a197p/article/details/47659459

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