hdu1556 Color the ball (树状数组)
2013-07-13 21:02
288 查看
#include <stdio.h> #include <string.h> #define MAX 1000005 int tree[MAX],n; int lowBit(int num) { return (num&(-num)); } void Add(int index,int val)//前n项每项增加val { while(index>0) { tree[index]+=val; index-=lowBit(index); } } int get(int x) { int sum=0; while(x<=n) { sum+=tree[x]; x+=lowBit(x); } return sum; } int main() { int k,i,j; while(scanf("%d",&n)&&n) { memset(tree,0,sizeof(tree)); for(k=1;k<=n;k++) { scanf("%d %d",&i,&j); Add(j,1); Add(i-1,-1); } for(i=1;i<n;i++) printf("%d ",get(i)); printf("%d\n",get(n)); } return 0; }
#include <stdio.h> #include <string.h> #define MAX 1000005 int arr[MAX],n; //树状数组 int lowBit(int x)//二进制后面为0的个数 { return (x&(-x)); } void add(int index,int val)//将index位置的值+val { while(index<=n) { arr[index]+=val; index+=lowBit(index); } } int get(int num) { int sum=0; while(num>0) { sum+=arr[num]; num-=lowBit(num);//找到num前的所有最大子树 } return sum; } int main() { int k,i,j; while(scanf("%d",&n)&&n) { memset(arr,0,sizeof(arr)); for(k=1;k<=n;k++) { scanf("%d %d",&i,&j); add(j+1,-1); add(i,1); } for(i=1;i<n;i++) printf("%d ",get(i)); printf("%d\n",get(n)); } return 0; }
相关文章推荐
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- Hdu1556 Color the ball [分块][树状数组]
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- hdu1556 Color the ball(树状数组)
- HDU1556 Color the ball【树状数组】【区间更新】
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- HDU1556 color the ball (树状数组)
- 【HDU1556】Color the ball(树状数组)
- HDU1556 Color the ball(树状数组BIT 区间修改单点询问)
- [树状数组]HDU1556 Color the ball
- 【树状数组-区间更新】hdu1556 Color the ball
- hdu1556 Color the ball【树状数组 || 线段树 || 技巧*区间修改】
- HDU1556 Color the ball (树状数组)
- HDU1556 color the ball【树状数组】解题报告
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- HDU1556 color the ball(树状数组)向下查询,向上统计
- HDU 1556 Color the ball(树状数组区间更新)
- hdu 1556 Color the ball(树状数组)
- HDOJ 1556 Color the ball(树状数组 & 线段树)
- POJ 2352 Stars + HDU 1556 Color the ball(树状数组单点更新及伪区间更新)