HDU 3308 最長上升連續子序列 (線段樹)

來源:互聯網
上載者:User
題意不用說了,此題是傳說中的  “傻仔哥”  出的,雖然不知教主真名,但是還是看著他的線段樹長大的....Code:#include<iostream>#include<cstdio>#include<string>#include<cmath>#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define manx 100050using namespace std;int root[manx<<2],lm[manx<<2],rm[manx<<2],a[manx];void up(int l,int r,int rt){    lm[rt] = lm[rt<<1];    rm[rt] = rm[rt<<1|1];    root[rt] = max(root[rt<<1],root[rt<<1|1]);    int mid = (l+r)>>1;    if(a[mid+1]>a[mid]){        if(lm[rt<<1] == mid-l+1) lm[rt] += lm[rt<<1|1];        if(rm[rt<<1|1] == r-mid) rm[rt] += rm[rt<<1];        root[rt] = max(root[rt],lm[rt<<1|1]+rm[rt<<1]);    }}void make(int l,int r,int rt){    if(l==r){        lm[rt] = rm[rt] = root[rt] = 1;        return ;    }    int mid = (l+r)>>1;    make(lson);    make(rson);    up(l,r,rt); //   cout<<l<<" "<<r<<" "<<rt<<" "<<root[rt]<<endl;}int query(int l,int r,int rt,int ll,int rr){    if(ll<=l && rr>=r){        return root[rt];    }    int mid=(l+r)>>1;    if(rr<=mid) return query(lson,ll,rr);    else if(ll>mid) return query(rson,ll,rr);    else {        int t1 = query(lson,ll,mid);        int t2 = query(rson,mid+1,rr);        int t = max(t1,t2);        if(a[mid] < a[mid+1]){            t1 = min(rm[rt<<1],mid-ll+1);  ////  防止越界的情況發生             t2 = min(lm[rt<<1|1],rr-mid);            t1 += t2;        }        return max(t,t1);    }}void update(int l,int r,int rt,int p,int q){    if(l==r && r==p){        a[l] = q;        return ;    }    int mid = (l+r)>>1;    if(p<=mid) update(lson,p,q);    else  update(rson,p,q);    up(l,r,rt);}int main(){    int t,n,m;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        make(1,n,1);        int p,q;        char ch[5];        for(int i=1;i<=m;i++){            scanf("%s%d%d",ch,&p,&q);            if(ch[0]=='Q'){                p++,q++;                int flag = query(1,n,1,p,q);                printf("%d\n",flag);            }            if(ch[0]=='U'){                p++;                update(1,n,1,p,q);            }        }        }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.