题解报告: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加 ...
随机推荐
- 问题解决:FFmpeg视频编解码库,无法解析的外部信号
在编译FFmpeg相关项目时.可能会出现: error LNK2019: 无法解析的外部符号 "int __cdecl avpicture_fill(struct AVPicture *,u ...
- Office EXCEL 如何设置最大行高
对于单个单元格行来说,行高必须在0-409之间 但是如果合并了两个单元格,则行高就扩展了一倍,不止409,而是两倍的409.
- 非计算机专业的伟伯是怎样拿到阿里Offer的。求职励志!!!
写在前面: 2015 年 7 月初.參加阿里巴巴校招内推, 8 月 15 日拿到研发project师 JAVA 的 offer .我的专业并不是计算机,也没有在互联网公司实习过,仅仅有一些学习和面试心 ...
- HDU 1272: 小希的迷宫(并查集)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Android使用adb获得activity堆栈信息
很实用的命令: adb shell dumpsys activity 该命令能够看到当前执行的是哪个activity,执行的一些进程等 首先能够看到执行的进程: ACTIVITY MANAGER RU ...
- c++代码赏析之类对象传參
#include <iostream> using namespace std; class A { private: int x; public: A():x(0) { x = 0; c ...
- qemu -net tap配置上网
1 该选项的用途 让qemu所在的宿主机器的tap网络接口和qemu的vlan n连接起来,从而进一步配置宿主机后,可以让qemu里面的操作系统可以通过vlan n里面的网卡上网. 2 真个系统的架构 ...
- Java内部类用法
内部类可以是静态(static)的,可以使用 public.protected 和 private 访问控制符,而外部类只能使用 public,或者默认. 成员式内部类 在外部类内部直接定义(不在方法 ...
- ELF和a.out文件格式的比较
本文讨论了 UNIX/LINUX 平台下三种主要的可执行文件格式:a.out(assembler and link editor output 汇编器和链接编辑器的输出).COFF(Common Ob ...
- YTU 2904: B--Faultfinding
2904: B--Faultfinding 时间限制: 1 Sec 内存限制: 128 MB 提交: 64 解决: 33 题目描述 Do you remember the game in whic ...