HDUOJ -----Color the ball
阿新 • • 發佈:2022-05-05
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7497 Accepted Submission(s): 3865
Problem Description
N個氣球排成一排,從左到右依次編號為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
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
Sample Output
1 1 1 3 2 1
Author
8600
Source
HDU 2006-12 Programming Contest
簡單的樹狀陣列。
依舊樹狀陣列的原理,如果直接採用ope(a,b),顯然會超時,而且若是在樹狀陣列中劃定界限來統計,圖畫的個數,這樣這樣明顯是統計不到的,
我們知道,樹狀陣列是一個向上修改的的過程,然後向下統計求sum , 若果我們要夾出這個數值,那麼我們必須要進行兩次的修改,來最終確定有多少
程式碼:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define maxn 100000 5 int aa[maxn+5]; 6 int nn; 7 int lowbit(int x) 8 { 9 return x&(-x); 10 } 11 void ope(int x,int val) 12 { 13 while(x<=nn) 14 { 15 aa[x]+=val; 16 x+=lowbit(x); 17 } 18 } 19 int sum(int x) 20 { 21 int ans=0; 22 while(x>0) 23 { 24 ans+=aa[x]; 25 x-=lowbit(x); 26 } 27 return ans; 28 } 29 int main() 30 { 31 int i,a,b; 32 while(scanf("%d",&nn),nn) 33 { 34 memset(aa,0,sizeof(aa)); 35 for(i=0;i<nn;i++) 36 { 37 scanf("%d%d",&a,&b); 38 ope(a,1); 39 ope(b+1,-1); 40 } 41 printf("%d",sum(1)); 42 for(i=2;i<=nn;i++) 43 printf(" %d",sum(i)); 44 putchar(10); 45 } 46 return 0; 47 }