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. 解谜谷歌 DevOps:什么特质可以打造世界级可靠系统?

    [编者按]本文是 Gene Kim 总结自对 Randy Shoup 两个小时的采访,主要关注谷歌 DevOps 的提升之道.本文系 OneAPM联合高效运维编译整理. Randy Shoup 曾协助 ...

  2. HDU 3397 Sequence operation (区间合并,操作比较多)

    费了我一天半的时间,到处debug,后来才发现,主要是建树的时候只在叶子节点对lazy1和lazy2进行初始化了,父节点都没初始化...晕. 具体见代码吧. #include <iostream ...

  3. hdu 4427 Math Magic DP

    思路: dp[i][j][k]表示满足前i个数,和为j,lcm为k的数目. 设a为解的第i+1个数. 那么状态转移就为 dp[i+1][j+a][lcm(a,k)]+=dp[i][j][k]. 但是由 ...

  4. hdu 1133 Buy the Ticket

    首先,记50的为0,100的为1. 当m=4,n=3时,其中的非法序列有0110010; 从不合法的1后面开始,0->1,1->0,得到序列式0111101 也就是说,非法序列变为了n-1 ...

  5. 深入理解JVM—Java 6 JVM参数配置说明

    原文地址:http://yhjhappy234.blog.163.com/blog/static/316328322011119111014657/ 使用说明< xmlnamespace pre ...

  6. lintcode :nth to Last Node In List 链表倒数第n个节点

    题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...

  7. PHP中的多态

    多态的概念一般是强类型语言来谈的,因为强类型语言它必须要声明参数类型,比如一个手电筒对象的打开方法其参数申明了只能是蓝光,就不能传其他光.但可以用父类渲染的方式使其多态,比如声明一个光的父类,让其它颜 ...

  8. React Jest测试

    一. var jest = require('jest'); jest.dontMock('../CheckboxWithLabel.js'); describe('CheckboxWithLabel ...

  9. AsyncTask 与 Thread+Handler

    AsyncTask是封装好的线程池,比起Thread+Handler的方式,AsyncTask在操作UI线程上更方便,因为onPreExecute().onPostExecute()及更新UI方法on ...

  10. JAX-RS 方式的 RESTful Web Service 开发

    JAX-RS 方式的 RESTful Web Service 开发 ——基于 CXF+Spring 的实现 Web Service 目前在风格上有两大类,一个是基于 SOAP 协议,一个是完全遵循 H ...