poj-2352 && HDU-1541 --Stars(树状数组)
2016-01-26 18:54
691 查看
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
51 1
5 1
7 1
3 3
5 5
Sample Output
12
1
1
0
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
树状数组
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #define N 15010 #define M 32010 #define ll long long using namespace std; int c[M], num , n; void add(int x, int val) { while(x <= M) { c[x] += val; x += x&-x; } } int sum(int x) { int ret = 0; while(x > 0) { ret += c[x]; x -= x&-x; } return ret; } int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif int i, j, a, b, t; while(~scanf("%d", &n)) { memset(c, 0, sizeof(c)); memset(num, 0, sizeof(num)); for (i = 0; i < n; i++) { scanf("%d%d", &a, &b); num[sum(a+1)]++; add(a+1, 1);//如果a是0的话,在这个函数里面会死循环,所以需要加1 } for (i = 0; i < n; i++) printf("%d\n", num[i]); } return 0; }
相关文章推荐
- 当执行gradle命令时出现JAVA_HOME设置无效的解决办法
- android 不能在子线程中更新ui的讨论和分析
- AngularJS系列:6、指令
- UE4 main classes explanation
- Codeforces Round #326 (Div. 2) D. Duff in Beach(LIS)
- linux使用select实现精确定时器详解
- zoj 3741 Eternal Reality dp
- 关于Mysql5.6中使用group_concat()函数返回的数据总数不完整的坑
- 使用JFinal-weixin配置微信开发
- 开发一个App要100万?太扯淡
- ruby中sequel多进程插入数据主键冲突
- 从Android运行时出发,打造我们的脱壳神器
- HDU3555 Bomb(数位dp)
- uva 484 - The Department of Redundancy Department
- android View事件传递
- Python中有效的字符串合并方法
- AFNetworking 支持cookies的解决方案
- 怎么解决tomcat占用8080端口问题图文教程
- 使用SQLiteOpenHelper对数据库进行版本管理
- ubuntu下JAVA开发环境搭建及实例