HDOJ 1556 Color the ball
来源:互联网 发布:mac中毒会有什么反应 编辑:程序博客网 时间:2024/05/19 18:43
http://acm.hdu.edu.cn/showproblem.php?pid=1556
树状数组解法:
题意:给N段连续的球上色,求上色后每个球被涂色的次数。
思路:假设有一个数组d,令其的初值为0。若涂色 [ L,R ] 的区间,则令d [ L ] +=1;d [R] - =1;C [ i ] 为对应数组d的前i项和,这样C是 [ L,R ] 的区间就记录了球的涂色次数。因为实际操作时d数组用不到,故可以略去。通过这个技巧就将断更新转化为了点更新,求第i个球涂几次只需求d的先i项和(即C [ i ] )即可。这样就变成一个简单的树状数组求和问题了~
关于C数组和d数组的关系举个例子吧:
第一次涂 [ L,R ] 区间:
下标 1 2 3 4 5 6 7 8 9 ……
C : 0 0 1 1 1 1 0 0 0 ……
d : 0 0 1 0 0 0 -1 0 0 ……
L R R+1
#include<stdio.h>#include<string.h>#define maxn 111111int n,C[maxn];int Lowbit(int x){return x&(-x);}void updata(int i,int x){while(i<=n){C[i]+=x;i+=Lowbit(i);}}int sum(int end){int sum=0;while(end>0){sum+=C[end];end-=Lowbit(end);}return sum;}int main(){while(scanf("%d",&n)&&n){int i,m=n;memset(C,0,sizeof(C));while(m--){int a,b;scanf("%d %d",&a,&b);updata(a,1);updata(b+1,-1);}printf("%d",sum(1));for(i=2;i<=n;i++)printf(" %d",sum(i));printf("\n");}return 0;}线段树解法:用线段树做就很简单啦~(代码有时间再补一下啦)
- HDOJ 1556 Color the ball
- HDOJ 1556 Color the ball
- HDOJ 1556 Color the ball
- Color the ball HDOJ--1556
- hdoj 1556 Color the ball
- HDOJ 1556 Color the ball
- HDOJ-1556 Color the ball
- HDOJ 1556 Color the ball
- hdoj-1556-Color the ball
- HDOJ Color the ball
- hdoj Color the ball
- HDoj-1556-Color the ball-树状数组
- hdoj 1556 Color the ball 【树状数组】
- hdoj 1556 Color the ball(树状数组)
- HDOJ Color the ball 1556【树状数组】
- hdoj 1556 Color the ball 【线段树】
- hdoj 1556 Color the ball <区间问题,>
- [HDOJ 1556] Color the ball(线段树成段更新入门)
- POJ 1654 Area
- iOS开发中的NSBundle对象
- Writing Smart Clients by Using Windows Communication Foundation
- Linux 内核编程总结
- 高精度加法模板(string类,char)
- HDOJ 1556 Color the ball
- 10个常见的Android新手误区
- 寄居蟹与海葵
- 对xib/nib, file's owner, first responder的理解
- This Handler class should be static or leaks ....
- 在DB2中根据原表复制新表
- VC++程序的编译链接过程
- XManager Shell乱码解决
- fsck命令修复受损文件系统