標籤: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;}