Stars
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
Ural Collegiate Programming Contest 1999
题目类型:线段树
算法分析:由于题目中已经给出的坐标是已经按照y坐标递增排序好的,所以以某点(x,y)和原点为对角线构造的矩形中的点的个数可以直接求在[0,x]范围内点的横坐标的个数,然后判断完level之后,直接更新x坐标处的点的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 32000 + 66; int cnt[maxn<<2], sum[maxn<<2]; void PushUp (int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void UpDate (int rt, int l, int r, int val, int num) { if (l == r) { sum[rt] += num; return ; } int m = (l + r) >> 1; if (val <= m) UpDate (lson, val, num); else UpDate (rson, val, num); PushUp (rt); } int Query (int rt, int l, int r, int L, int R) { if (L <= l && r <= R) { return sum[rt]; } int m = (l + r) >> 1; int ans = 0; if (L <= m) ans += Query (lson, L, R); if (R > m) ans += Query (rson, L, R); return ans; } int main() { // freopen ("aaa.txt", "r", stdin); int n; while (scanf ("%d", &n) != EOF) { memset (cnt, 0, sizeof (cnt)); memset (sum, 0, sizeof (sum)); int i, val_x, val_y; for (i = 0; i < n; i++) { scanf ("%d%d", &val_x, &val_y); cnt[Query(1, 0, maxn, 0, val_x)]++; UpDate (1, 0, maxn, val_x, 1); } for (i = 0; i < n; i++) printf ("%d\n", cnt[i]); } return 0; } |
- « 上一篇:poj2342
- poj2387:下一篇 »