POJ2352 Stars(线段树 & 树状数组)
2015-08-31 17:41
337 查看
Stars
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
Sample Output
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
给出n个星星的坐标,星星的级数等于左下角有几个星星,输出n个星星的级数。
简单的线段树,把坐标转化,求和存到level数组即可。
AC代码(线段树):
树状数组思路一样的~
AC代码(树状数组):
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37709 | Accepted: 16423 |
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.
给出n个星星的坐标,星星的级数等于左下角有几个星星,输出n个星星的级数。
简单的线段树,把坐标转化,求和存到level数组即可。
AC代码(线段树):
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int maxn = 32005; #define mid ((left + right) >> 1) #define lson rt << 1, left, mid #define rson rt << 1 | 1, mid + 1, right int sum[maxn << 2], level[maxn << 2]; void update(int rt, int left, int right, int data) { ++sum[rt]; if(left == right) return ; if(data <= mid) update(lson, data); else update(rson, data); } int query(int rt, int left, int right, int l, int r) { if(l == left && right == r) return sum[rt]; int m = mid; if(r <= m) return query(lson, l, r); else if(l > m) return query(rson, l, r); else return query(lson, l, m) + query(rson, m + 1, r); } int main(int argc, char const *argv[]) { /* code */ int n, x, y; while(scanf("%d", &n) != EOF) { memset(sum, 0, sizeof(sum)); memset(level, 0, sizeof(level)); for(int i = 0; i < n; ++i) { scanf("%d%d", &x, &y); ++x; ++level[query(1, 1, maxn, 1, x)]; update(1, 1, maxn, x); } for(int i = 0; i < n; ++i) printf("%d\n", level[i]); } return 0; }
树状数组思路一样的~
AC代码(树状数组):
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int maxn = 32005; int c[maxn], level[maxn], n; int lowbit(int x) { return x & (-x); } int sum(int n) { int sum = 0; while(n > 0) { sum += c ; n -= lowbit(n); } return sum; } void add(int x) { while(x <= maxn) { ++c[x]; x += lowbit(x); } } int main(int argc, char const *argv[]) { int n, x, y; while(scanf("%d", &n) != EOF) { memset(level, 0, sizeof(level)); memset(c, 0, sizeof(c)); for(int i = 0; i < n; ++i) { scanf("%d%d", &x, &y); ++x; level[sum(x)]++; add(x); } for(int i = 0; i < n; ++i) printf("%d\n", level[i]); } return 0; }
相关文章推荐
- URL编码以及GET和POST提交乱码解决方案 (转)
- Android开发相关的Blog推荐
- tesseract使用的一些方法-ocr
- python threading多线程模块
- Android输入法框架分析
- Merge Two Sorted Lists
- 右键菜单功能动态生成子项
- iOS Ad-Hoc测试发布版本应用
- 信号
- Quartz使用
- 多行文本溢出显示省略号(…)全攻略
- wx.ListCtrl简单使用例子
- 通知中心
- Java MVC与封装示例
- spring的生命周期
- phpQuery—基于jQuery的PHP实现
- 内网编译thrift 0.8
- Invalid project description
- RESTEasy入门学习
- Error message when I run sudo: unable to resolve host