hdu1166 線段樹

來源:互聯網
上載者:User

標籤:

雖然沒有大神們寫的飄逸,但是思路很清晰......

#include<cstdio>#include<cstring>#define maxn 50000using namespace std;struct seg_tree{    int l,r,value;}st[3*maxn];void build(int l,int r,int rt){    st[rt].l=l;    st[rt].r=r;    if(l!=r)    {        build(l,(l+r)/2,2*rt);        build((l+r)/2+1,r,2*rt+1);        st[rt].value=st[rt*2].value+st[rt*2+1].value;    }    else    {        int temp;        scanf("%d",&temp);        st[rt].value=temp;    }}void update(int x,int rt,int v){    if(st[rt].l==st[rt].r)    {        st[rt].value+=v;        return;    }    int m=(st[rt].l+st[rt].r)/2;    if(x>=st[rt].l&&x<=st[rt].r)      st[rt].value+=v;    if(x<=m)        update(x,rt*2,v);    else        update(x,rt*2+1,v);}int query(int l,int r,int rt){    if(st[rt].l==l&&st[rt].r==r)        return st[rt].value;    else    {        int m=(st[rt].l+st[rt].r)/2;        if(r<=m)            return query(l,r,rt*2);        else if(l>m)            return query(l,r,rt*2+1);        else            return query(l,m,rt*2)+query(m+1,r,rt*2+1);    }}
int main(){ int t,n,i,j; char cmd[6]; scanf("%d",&t); for(int k=1;k<=t;k++) { printf("Case %d:\n",k); scanf("%d",&n); build(1,n,1); while(~scanf("%s",cmd),strcmp(cmd,"End")) { scanf("%d%d",&i,&j); if(strcmp(cmd,"Query")==0) printf("%d\n",query(i,j,1)); else if(strcmp(cmd,"Add")==0) update(i,1,j); else if(strcmp(cmd,"Sub")==0) update(i,1,-j); } }}

 

hdu1166 線段樹

聯繫我們

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