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. linux 查找目录下的文件内容并替换(批量)

    2.sed和grep配合 命令:sed -i s/yyyy/xxxx/g `grep yyyy -rl --include="*.txt" ./` 作用:将当前目录(包括子目录)中 ...

  2. [转]图解eclipse 查看原始类出现The jar file rt.jar has no source attachment

    原文:http://blog.csdn.net/u011514810/article/details/53196371 ---------------------------------------- ...

  3. URL传参中文乱码的一种解决方法

    中文乱码是由于,发送和接收方使用的编码解码格式不一致导致,以下是关于url传参解决中文乱码的一种方法,最后根据各种编码格式尝试解码,发现正确的解码格式 string strQueryString = ...

  4. 2016/4/17 去除 ul ol 前标记 list-style:none list-style-type:none

    对于很多人用div来做网站时,总会用到,但在显示效果时前面总是会有一个小黑点,这个令很多人头痛,但又找不到要源,其它我们可以用以下方法来清除. 1.在CSS中写入代码.找到相关性的CSS,在..li和 ...

  5. os 2大功能

    支撑功能 中断管理 时钟管理 原语操作  primitive 资源管理功能 进程管理 存储器管理 设备管理

  6. hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 BestCoder Sequence Time Limit: 2000/1000 MS (Jav ...

  7. js实现加密(?!)

    <script src="yourUrl/md5.min.js"></script> 或者: <script src="http://cdn ...

  8. SprigMVC基础测试

    创建POJO package org.entity; public class User { private int id; private String userName; private Stri ...

  9. HDU1083 Courses —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-1083 Courses Time Limit: 20000/10000 MS (Java/Others)    Memory ...

  10. Android开发之自定义对话框

    由于系统自带的对话框不好看,于是本人就自定义了一个对话框,以后有类似的就可以直接使用啦.先上效果图: 1,布局文件dialog_clear_normal.xml <?xml version=&q ...