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

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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

Source

 
 
 
解析:树状数组(单点更新,区间查询)。根据题意,点的坐标不会重复,且输入时按y从小到大排列,y相同则按x从小到大排列,可以得到结论:如果后面输入的点的横坐标大于或等于之前输入的点的横坐标,那么之前输入的点一定在后面的点的左下角或正下方或正左方。据此可以建立一个树状数组来解答,假设原数组为a[],a[i]表示横坐标为i的点的个数,树状数组为c[],每输入一个点(x, y),查询从a[1]到a[x]的和,即可得到有多少个点的横坐标不大于这个点的横坐标,用ans[]来存储得到的和。每得到一个和,在ans[]的对应位置处加上1。处理完毕后输出即可。但要注意,题目中x的范围为[0, 32000],而树状数组的索引是从1开始的,因此把所有的x加上1,索引区间变为[1, 32001](如果从0开始的话,当i == 0时,执行add(i, 1)会陷入死循环,所以索引是从1开始的。树状数组的结构决定了它的性质)
 
 
 
#include <cstdio>
#include <cstring>
#define lowbit(x) (x)&(-x) int c[32005]; //树状数组
int ans[15005]; //存储结果 void add(int i)
{
while(i <= 32001){ //注意为32001,而不是32000
++c[i];
i += lowbit(i);
}
} int sum(int i)
{
int ret = 0;
while(i > 0){
ret += c[i];
i -= lowbit(i);
}
return ret;
} int main()
{
int n;
while(~scanf("%d", &n)){
int x, y;
//先将两个数组清零
memset(c, 0, sizeof(c));
memset(ans, 0, sizeof(ans));
for(int i = 1; i <= n; ++i){
scanf("%d%d", &x, &y);
++x; //索引加1
++ans[sum(x)]; //ans[]的对应处加上1
add(x); //增加一个横坐标为x的点
}
for(int i = 0; i < n; ++i)
printf("%d\n", ans[i]);
}
return 0;
}

POJ 2352 Stars(HDU 1541 Stars)的更多相关文章

  1. Stars HDU - 1541

    HDU - 1541 思路:二维偏序,一维排序,一维树状数组查询即可. #include<bits/stdc++.h> using namespace std; #define maxn ...

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

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

  3. hdu 1541 Stars

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

  4. HDU 1541 Stars (树状数组)

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

  5. HDU 1541 Stars (线段树)

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

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

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

  7. hdu 1541 Stars 解题报告

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

  8. 题解报告:hdu 1541 Stars(经典BIT)

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

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

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

随机推荐

  1. hdu 1596 find the safest road(最短路,模版题)

    题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...

  2. Java 连接SQLite数据库

    下载jar包: http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz public class TestSQLite { ...

  3. Android中使用HTTP和HttpClient进行通信

    /** * 使用HTTP的Get方式进行数据请求 */ protected void httpGet() { /** * 进行异步请求 */ new AsyncTask<String, Void ...

  4. lintcode: 二叉树的锯齿形层次遍历

    题目 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / \ ...

  5. python自省指南

    深入python中对自省的定义: python的众多强大功能之一,自省,正如你所知道的,python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其他模块和函数,获取它们的信息,并对它们进行 ...

  6. iOS 精确定时器

    Do I need a high precision timer? Don't use a high precision timer unless you really need it. They c ...

  7. oracle 修改表的sql语句

    oracle 修改表的sql语句     1增加一个列:ALTER TABLE 表名 ADD(列名 数据类型);如:ALTER TABLE emp ADD(license varchar2(256)) ...

  8. HDU5099——Comparison of Android versions(简单题)(2014上海邀请赛重现)

    Comparison of Android versionsProblem DescriptionAs an Android developer, itˇs really not easy to fi ...

  9. BeanFactory 和 ApplicationContext

    Spring通过一个配置文件描述Bean及Bean直接的依赖关系,利用Java语言的反射功能实例化Bean并建立Bean之间的依赖关系.Sprig的IoC容器在完成这些底层工作的基础上,还提供了Bea ...

  10. eclipse Juno Indigo Helios Galileo这几种版本的意思(转)

    Galileo Ganymede Europa 这些名字代表eclipse不同的版本  2001年11月7日 ,Eclipse 1.0发布   半年之后,2002年6月27日Eclipse进入了2.0 ...