Problem Description

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

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

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1
0
解题思路:题目的意思就是每一颗星星都有一个等级level,其大小取决于以它为坐标原点,位于第三象限且包括x、y的负半轴(不算本身一点)内星星的个数,要求输出每个等级level(0~n-1)有多少个星星。典型的树状数组裸题!题目输入中已经规定:①不会有重点;②纵坐标先按升序排列;③如果纵坐标相同,按横坐标升序排列。简单在纸上模拟一下题目中的样例可知,我们只需对横坐标进行处理即可。其中有两点需要注意:一、单点修改update函数中while条件要为x<=32000(<maxn),因为输入的横坐标大小不是在n的范围内,所以要对整个大区间进行修改;二、由于输入的横坐标可能为0,而在树状数组中下标最小为1,如果不将输入的每个横坐标加1,那么在单点修改时,由于lowbit(0)==0,那么x小于maxn这个条件就会一直为true,即陷入死循环。
AC代码:
 #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)的更多相关文章

  1. hdu 1541 Stars 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有 ...

  2. hdu 1541 Stars

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 思路:要求求出不同等级的星星的个数,开始怎么也想不到用树状数组,看完某些大神的博客之后才用树状数 ...

  3. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  4. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

  5. hdu 1541 Stars 统计<=x的数有几个

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. POJ 2352 Stars(HDU 1541 Stars)

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41521   Accepted: 18100 Descripti ...

  7. HDU 1541 Stars (树状数组)

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

  8. HDU 1541 Stars (线段树)

     Problem Description Astronomers often examine star maps where stars are represented by points on ...

  9. hdu 1541 Stars(线段树单点更新,区间查询)

    题意:求坐标0到x间的点的个数 思路:线段树,主要是转化,根据题意的输入顺序,保证了等级的升序,可以直接求出和即当前等级的点的个数,然后在把这个点加入即可. 注意:线段树下标从1开始,所以把所有的x加 ...

随机推荐

  1. 问题解决:FFmpeg视频编解码库,无法解析的外部信号

    在编译FFmpeg相关项目时.可能会出现: error LNK2019: 无法解析的外部符号 "int __cdecl avpicture_fill(struct AVPicture *,u ...

  2. Office EXCEL 如何设置最大行高

    对于单个单元格行来说,行高必须在0-409之间   但是如果合并了两个单元格,则行高就扩展了一倍,不止409,而是两倍的409.

  3. 非计算机专业的伟伯是怎样拿到阿里Offer的。求职励志!!!

    写在前面: 2015 年 7 月初.參加阿里巴巴校招内推, 8 月 15 日拿到研发project师 JAVA 的 offer .我的专业并不是计算机,也没有在互联网公司实习过,仅仅有一些学习和面试心 ...

  4. HDU 1272: 小希的迷宫(并查集)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. Android使用adb获得activity堆栈信息

    很实用的命令: adb shell dumpsys activity 该命令能够看到当前执行的是哪个activity,执行的一些进程等 首先能够看到执行的进程: ACTIVITY MANAGER RU ...

  6. c++代码赏析之类对象传參

    #include <iostream> using namespace std; class A { private: int x; public: A():x(0) { x = 0; c ...

  7. qemu -net tap配置上网

    1 该选项的用途 让qemu所在的宿主机器的tap网络接口和qemu的vlan n连接起来,从而进一步配置宿主机后,可以让qemu里面的操作系统可以通过vlan n里面的网卡上网. 2 真个系统的架构 ...

  8. Java内部类用法

    内部类可以是静态(static)的,可以使用 public.protected 和 private 访问控制符,而外部类只能使用 public,或者默认. 成员式内部类 在外部类内部直接定义(不在方法 ...

  9. ELF和a.out文件格式的比较

    本文讨论了 UNIX/LINUX 平台下三种主要的可执行文件格式:a.out(assembler and link editor output 汇编器和链接编辑器的输出).COFF(Common Ob ...

  10. YTU 2904: B--Faultfinding

    2904: B--Faultfinding 时间限制: 1 Sec  内存限制: 128 MB 提交: 64  解决: 33 题目描述 Do you remember the game in whic ...