hdu 1166 _ 樹狀數組版

來源:互聯網
上載者:User

#include<stdio.h><br />#include<string.h></p><p>int arr[50005];<br />int c[50005];<br />int n;</p><p>int lowbit(int x)<br />{<br />x = x & (x ^ (x-1));<br />return x;<br />}</p><p>void change(int x,int y)<br />{<br />while(x <= n)<br />{<br />c[x] += y;<br />x += lowbit(x);<br />}<br />}</p><p>int get_sum(int x)<br />{<br />int sum = 0;<br />while(x >= 1){<br />sum += c[x];<br />x -= lowbit(x);<br />}<br />return sum;<br />}</p><p>int main()<br />{<br />int tcase;<br />int i;<br />int a,b;<br />int sum;<br />int walk = 0;<br />char str[10];<br />scanf("%d",&tcase);<br />while(tcase--){<br />arr[0] = 0;<br /> scanf("%d",&n);<br /> for(i = 1;i <= n;i++){<br /> scanf("%d",&arr[i]);<br /> arr[i] += arr[i-1];<br /> c[i] = arr[i]-arr[i-lowbit(i)];<br /> }<br /> printf("Case %d:/n",++walk);<br /> while(1){<br /> scanf("%s",str);<br /> if(!strcmp(str,"Query")){<br /> scanf("%d %d",&a,&b);<br /> printf("%d/n",get_sum(b)-get_sum(a-1));<br /> }<br /> if(!strcmp(str,"Add")){<br /> scanf("%d %d",&a,&b);<br /> change(a,b);<br /> }<br /> if(!strcmp(str,"Sub")){<br /> scanf("%d %d",&a,&b);<br /> change(a,-b);<br /> }<br /> if(!strcmp(str,"End")) break;<br /> }<br />}<br />return 0;<br />}

聯繫我們

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