HDU 1556 區間查詢

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   os   io   

Color the ball

Time Limit: 3000 MS Memory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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 <iostream>#include <string.h>#include <stdio.h>using namespace std;#define max 100005int c[max];int n;int lowbit(int x){    return x&-x;}void update(int i,int val){    while(i>0)    {        c[i]+=val;        i-=lowbit(i);    }}int get_sum(int i){    int s=0;    while(i<=n)    {        s+=c[i];   ///上下兩行 不可換位置~~~~~        i+=lowbit(i);    }    return s;}int main(){    while(scanf("%d",&n),n)    {        int x,y;        memset(c,0,sizeof(c));        for(int i=0; i<n; i++)        {            scanf("%d%d",&x,&y);            update(y,1);            update(x-1,-1);        }        for(int i=1; i<n; i++)   ///注意腳標            printf("%d ",get_sum(i));            printf("%d\n",get_sum(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.