题解报告:hdu 1541 Stars(经典BIT)
Problem Description

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
Output
Sample Input
Sample Output
#include<string.h>
#include<cstdio>
const int maxn=;
int n,a,b,aa[maxn],tmp[maxn];
int lowbit(int x){
return x & -x;
}
void update(int x,int val){
while(x<maxn){//注意:因为输入的横坐标大小不是在n的范围内,所以这里要小于或等于32000,对整个区间进行统计
aa[x]+=val;
x+=lowbit(x);
}
}
int get_sum(int x){
int ret=;
while(x>){
ret+=aa[x];
x-=lowbit(x);
}
return ret;
}
int main(){
while(~scanf("%d",&n)&&n){
memset(tmp,,sizeof(tmp));//tmp[i]表示等级level i有tmp[i]个星星
memset(aa,,sizeof(aa));//注意清0
for(int i=;i<n;++i){
scanf("%d%d",&a,&b);//题目中的输入已排好序,直接对横坐标进行处理,get_sum(a+1)表示当前坐标点的左下角有这么多个星星,对应为level大小,其个数加1,即tmp[get_sum(a+1)]++;
tmp[get_sum(a+)]++;//每个横坐标要加1,因为输入的横坐标可能为0,而在树状数组中下标最小为1,如果不加1,在单点修改的时候,由于lowbit(0)==0,那么x就会一直小于maxn,即陷入死循环
update(a+,);//当前坐标点对应level包含星星的个数先加1,因为不包括本身,所以再更新标记当前坐标点为1,即已经出现过
}
for(int i=;i<n;++i)//输出每个level包含星星的个数
printf("%d\n",tmp[i]);
}
return ;
}
题解报告:hdu 1541 Stars(经典BIT)的更多相关文章
- hdu 1541 Stars 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有 ...
- hdu 1541 Stars
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 思路:要求求出不同等级的星星的个数,开始怎么也想不到用树状数组,看完某些大神的博客之后才用树状数 ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- HDU - 1541 Stars 【树状数组】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
- hdu 1541 Stars 统计<=x的数有几个
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- POJ 2352 Stars(HDU 1541 Stars)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41521 Accepted: 18100 Descripti ...
- HDU 1541 Stars (树状数组)
Problem Description Astronomers often examine star maps where stars are represented by points on a p ...
- HDU 1541 Stars (线段树)
Problem Description Astronomers often examine star maps where stars are represented by points on ...
- hdu 1541 Stars(线段树单点更新,区间查询)
题意:求坐标0到x间的点的个数 思路:线段树,主要是转化,根据题意的输入顺序,保证了等级的升序,可以直接求出和即当前等级的点的个数,然后在把这个点加入即可. 注意:线段树下标从1开始,所以把所有的x加 ...
随机推荐
- 使用MySQL Workbench进行数据库设计——MySQL Workbench用法总结
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/48318877 本文出自[我是干勾鱼的博客] 1 简单介绍 MySQL Workb ...
- Mongodb for PHP教程之入门安装
简介: MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据 ...
- C#&.NET高级面试题
转自http://chaoyouzhuo.blog.163.com/blog/static/1263760012011109114131316/ 1. DateTime.Parse(myString) ...
- 在CentOS上把Nginx从1.2.4升级到1.6.0
在CentOS上升级把Nginx从1.2.4升级到1.6.0 摘要:本文记录了在CentOS 6.3上,把Nginx从1.2.4升级到1.6.0的过程. 1. 概述 在我做的一个项目中,最近我对生产服 ...
- PL/SQL Developer导入导出Oracle数据库方法
前一篇博客介绍了Navicat工具备份Oracle的方法.这篇博客介绍一下使用PL/SQL Developer工具导入导出Oracle数据库的方法. PL/SQL Developer是Oracle数据 ...
- Fluently NHibernate 插入CLOB字段
ORA-01461: can bind a LONG value only for insert into a LONG column 插入oracle某表时报的错. 查来查去,是插入的某个字段值超长 ...
- Hbase权限配置以及使用手册
1.Hbase权限控制简介 Hbase的权限控制是通过AccessController Coprocessor协处理器框架实现的,可实现对用户的RWXCA的权限控制. 2.配置 配置hbase-sit ...
- Ubuntu grub2的启动配置文件grub.cfg,为了修改另人生厌的时间
文章转自http://hi.baidu.com/detax/blog/item/90f18b54a8ef5253d00906e4.html 升级到Ubuntu 9.10后,就要接触grub2了,它和以 ...
- Nyquist–Shannon sampling theorem 采样定理
Nyquist–Shannon sampling theorem - Wikipedia https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_s ...
- SAP事务码 一
SE80 -- edit source code. SE24 -- class create or display. SFP -- created and maintained independent ...