hdu Color the ball

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556

 

树状数组的  update的应用,逆序更新

 

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <limits.h> 
 5 #include <algorithm>
 6 #include <iostream>
 7 #include <ctype.h>
 8 #include <iomanip>
 9 #include <queue>
10 #include <map>
11 #include <stdlib.h>
12 using namespace std;
13 
14 int c[100010],n;
15 
16 int lowbit(int x)
17 {
18     return x&(-x);
19 }
20 
21 void update(int x,int y)
22 {
23     while(x>0){
24         c[x]=c[x]+y;
25         x=x-lowbit(x);
26     }
27 }
28 
29 int sum(int x)
30 {
31     int r=0;
32     while(x<=n){
33         r+=c[x];
34         x+=lowbit(x);
35     }
36     return r;
37 }
38 
39 int main()
40 {
41     int a,b;
42     while(~scanf("%d",&n)&&n!=0){
43         memset(c,0,sizeof(c));
44         for(int i=1;i<=n;i++){
45             scanf("%d%d",&a,&b);
46             update(b,1);
47             update(a-1,-1);
48         }
49         for(int i=1;i<n;i++){
50             printf("%d ",sum(i));
51         }
52         printf("%d\n",sum(n));
53     }
54 }

 

posted @ 2015-11-18 19:12  Vmetrio  阅读(130)  评论(0编辑  收藏  举报