hdoj 1556 Color the ball 【樹狀數組】,hdoj1556

來源:互聯網
上載者:User

hdoj 1556 Color the ball 【樹狀數組】,hdoj1556

Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9769    Accepted Submission(s): 5028

Problem DescriptionN個氣球排成一排,從左至右依次編號為1,2,3....N.每次給定2個整數a b(a <= b),lele便為騎上他的“小飛鴿"牌電動車從氣球a開始到氣球b依次給每個氣球塗一次顏色。但是N次以後lele已經忘記了第I個氣球已經塗過幾次顏色了,你能幫他算出每個氣球被塗過幾次顏色嗎? 
Input每個測試執行個體第一行為一個整數N,(N <= 100000).接下來的N行,每行包括2個整數a b(1 <= a <= b <= N)。
當N = 0,輸入結束。 
Output每個測試執行個體輸出一行,包括N個整數,第I個數代表第I個氣球總共被塗色的次數。 
Sample Input
31 12 23 331 11 21 30
 
Sample Output
1 1 13 2 1
 簡單的樹狀數組的應用,

代碼:

#include <stdio.h>#include <string.h>#define M 100005int c[M], n;int lowbit(int x){return x&(-x);}int getsum(int x){int sum = 0;while(x){sum += c[x];x -= lowbit(x);}return sum;}void add(int x, int val){while(x<=n){c[x] += val;x += lowbit(x);}}int main(){while(scanf("%d", &n), n){memset(c, 0, sizeof(c));int a, b, m = n;//n += 1;while(m --){scanf("%d%d", &a, &b);add(a, 1);add(b+1, -1);}printf("%d", getsum(1));for(int i = 2; i <= n; i ++){printf(" %d", getsum(i));}printf("\n");}return 0;}







聯繫我們

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