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. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

  2. Const和ReadOnly区别及其用途--转载

    常量的概念就是一个包含不能修改的值的变量,常量是C#与大多数编程语言共有的.但是,常量不必满足所有的要求.有时可能需要一些变量,其值不应改变,但在运行之前其值是未知的.C#为这种情形提供了另一个类型的 ...

  3. POJ 2253 Frogger(floyd)

    http://poj.org/problem?id=2253 题意 : 题目是说,有这样一只青蛙Freddy,他在一块石头上,他呢注意到青蛙Fiona在另一块石头上,想去拜访,但是两块石头太远了,所以 ...

  4. SUDT2177体检

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2177 题目描述 大家都知道,新生入学的前几周要 ...

  5. fhq_treap 总结

    今天跟着zcg大神学了一发fhq_treap 发现在维护区间问题上fhq_treap不仅思维量小,而且代码量更小 是Splay的不错的替代品,不过至今还是有一些问题不能很好的解决 譬如查询某个数在序列 ...

  6. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

  7. javaWEB邮件测试

    新建一个工具类: Mail.java 该类的主要关键点是:1.设置系统属性.也就是你是用什么协议来进行邮件发送的,邮件协议有很多在种,比如impt,smpt,prop等协议, 我现在测试用的是smpt ...

  8. VirtualUI - Convert your Windows App to HTML5

    http://www.cybelesoft.com/thinfinity/virtualui/

  9. Linux /bin、/sbin、/usr/bin、/usr/sbin目录的区别

    在linux下我们经常用到的四个应用程序的目录是/bin./sbin./usr/bin./usr/sbin .而四者存放的文件一般如下:     bin目录:  bin为binary的简写主要放置一些 ...

  10. org.json.JSONObject与com.google.gson.Gson

    org.json库为JSON创始人编写的解析JSON的java库,Gson为Google为我们提供的解析JSON格式数据的库. Gson里最重要的对象有2个Gson 和GsonBuilder. Gso ...