線段樹 SP1716 GSS3 - Can you answer these queries III

來源:互聯網
上載者:User

標籤:using   比較   --   val   ios   return   lin   query   iostream   

SP1716 GSS3 - Can you answer these queries III
題意翻譯

n 個數,q 次操作

操作0 x y把A_xAx 修改為yy

操作1 l r詢問區間[l, r] 的最大子段和

依舊是維護最大子段和,還是再敲一遍比較好。

code:

#include<iostream>#include<cstdio>#define ls(o) o<<1#define rs(o) o<<1|1using namespace std;const int wx=100017;inline int read(){    int sum=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}    return sum*f;}struct val_tree{    int l,r,lsum,rsum,sum,tot;    #define sum(o) t[o].sum    #define lsum(o) t[o].lsum    #define rsum(o) t[o].rsum    #define tot(o) t[o].tot}t[wx*4];int a[wx];int n,m;void up(int o){    tot(o)=tot(ls(o))+tot(rs(o));    lsum(o)=max(lsum(ls(o)),tot(ls(o))+lsum(rs(o)));    rsum(o)=max(rsum(rs(o)),tot(rs(o))+rsum(ls(o)));    sum(o)=max(rsum(ls(o))+lsum(rs(o)),max(sum(ls(o)),sum(rs(o))));}void build(int o,int l,int r){    t[o].l=l;t[o].r=r;    if(l==r){sum(o)=lsum(o)=rsum(o)=tot(o)=a[l];return ;}    int mid=t[o].l+t[o].r>>1;    if(l<=mid)build(ls(o),l,mid);    if(r>mid)build(rs(o),mid+1,r);    up(o);}void update(int o,int l,int r,int k){    if(l<=t[o].l&&t[o].r<=r){        sum(o)=lsum(o)=rsum(o)=tot(o)=k;        return ;    }    int mid=t[o].l+t[o].r>>1;    if(l<=mid)update(ls(o),l,r,k);    if(r>mid)update(rs(o),l,r,k);    up(o);}val_tree query(int o,int l,int r){    if(l<=t[o].l&&t[o].r<=r){        return t[o];    }    int mid=t[o].l+t[o].r>>1;    if(r<=mid)return query(ls(o),l,r);    if(l>mid)return query(rs(o),l,r);    val_tree tmp,tmp1,tmp2;    tmp1=query(ls(o),l,r);    tmp2=query(rs(o),l,r);    tmp.tot=tmp1.tot+tmp2.tot;    tmp.lsum=max(tmp1.lsum,tmp1.tot+tmp2.lsum);    tmp.rsum=max(tmp2.rsum,tmp2.tot+tmp1.rsum);    tmp.sum=max(tmp1.rsum+tmp2.lsum,max(tmp1.sum,tmp2.sum));    return tmp;}int main(){    n=read();    for(int i=1;i<=n;i++)a[i]=read();    build(1,1,n);    m=read();    for(int i=1,x,y,opt;i<=m;i++){        opt=read();x=read();y=read();        if(opt)printf("%d\n",query(1,x,y).sum);        else update(1,x,x,y);    }    return 0;}

線段樹 SP1716 GSS3 - Can you answer these queries III

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.