Color the balltime limit: 9000/3000 ms (Java/other) memory limit: 32768/32768 K (Java/other) total submission (s): 8 accepted submission (s): 2 Font: times New Roman | verdana | georgiafont size: Batch → problem descriptionn balloons in a row, numbered 1, 2, 3 .... n. given two integers a B (A <= B) each time, Lele colors each balloon one time from balloon a to balloon B, riding his "little pigeon" electric car. But after N times, Lele has forgotten how many times the I-th balloon has been painted. Can you help him figure out how many times each balloon has been painted? Input the first behavior of each test instance is an integer N, (n <= 100000 ). next n rows, each row contains two integers, a B (1 <= A <= B <= N ).
When n = 0, the input ends. Output each test instance outputs a row, which contains N integers. The number of I represents the total number of times that the I balloon is colored. Sample Input
31 12 23 331 11 21 30
Sample output
1 1 13 2 1
/*树状数组应用。 */#include<stdio.h>#include<string.h> int n,tree[100010];int lowbit(int N){return N&(-N);}int add(int i,int t)//求第i个数之后都加上t. {while(i<=n){tree[i]+=t;i+=lowbit(i);}}int sum(int m) {int sum=0;while(m>0){sum+=tree[m];m-=lowbit(m);}return sum;}int main(){int i,a,b;while(scanf("%d",&n),n){memset(tree,0,sizeof(tree));for(i=0;i<n;i++){scanf("%d %d",&a,&b);add(a,1); //将a之后的涂色次数加1. add(b+1,-1);//将b之后的涂色次数减1. }printf("%d",sum(1));for(i=2;i<=n;i++) printf(" %d",sum(i));printf("\n");}return 0;}
Color the ball (hangdian 1556)