您的位置:首页 > 其它

HDU1556 Color the ball 树状数组(区间更新单点求值)

2015-10-10 14:06 302 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556

树状数组实现代码如下:

//树状数组纪录每个点的改变值
#include <cstdio>
#include <cstring>
using namespace std;
const int M=100010;
int a[M],n;
int lowbit(int i)
{
return i&(-i);
}
void update(int i,int x)
{
while(i<=n)
{
a[i]+=x;
i+=lowbit(i);
}
}
int query(int n)
{
int sum=0;
while(n>0)
{
sum+=a
;
n-=lowbit(n);
}
return sum;
}
int main()
{
int x,y;
while(scanf("%d",&n)&&n)
{
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
update(x,1);
update(y+1,-1);
}
for(int i=1;i<n;i++)
printf("%d ",query(i));
printf("%d\n",query(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: