Hdoj Color the ball
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 11231 Accepted Submission (s): 5595
Problem Description N balloons are arranged 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
Author 8600 tree array Interval Update
#include
#include
#include
using namespace std;const int MAX=100005;int a[MAX],c[MAX];int low(int n){return n&(-n);}void add(int p,int q,int n){while(p<=n){c[p]+=q;p+=low(p);}}int sum(int n){int result=0;while(n!=0){result+=c[n];n-=low(n);}return result;}int main(){int n,m,i,j,a,b;while(scanf("%d",&n),n){memset(c,0,sizeof(c));for(i=1;i<=n;++i){scanf("%d%d",&a,&b);add(a,1,n);add(b+1,-1,n);}printf("%d",sum(1));for(i=2;i<=n;++i){printf(" %d",sum(i));}printf("\n");}return 0;}