Problem descriptionastpolicmers 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. astpolicmers 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.
Inputthe 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.
Outputthe output shoshould 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 15 17 13 35 5
Sample output
12110
Sourceural Collegiate Programming Contest 1999
Meaning: give you n coordinates, and then any of the points, if the X and Y coordinates of s points are not greater than this point, then the value of this point is S,
Finally, let you output value from 1 ~ N-1, number of vertices
A team made this question during the competition and soon made it. I had a long struggle (too much food % >_<%) and kept thinking about sorting X and then sorting y,
If you want to obtain the rule, and finally think of sorting X, to judge the value of this point, you need to quickly query the front point (sort by X) the number of vertices that are no larger than him (the data is big and time-out). Finally, I thought of a line segment tree (⊙ o ⊙ )..., Use a line segment tree to save Le ~ Number of existing Ri instances
The specific idea is finished and the code is finished.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define L (x) (x <1) # define R (X) (x <1 | 1) # define mid (x, y) (x + y)> 1) Using namespace STD; # define n 32005 struct stud1 {int le, ri; int Va;} f [N * 4]; // At the beginning, there was no multiplication 4, and the answer was never correct. struct stud {int X, Y ;} s [N]; int ans [N]; int CMP (stud a, stud B) {if (. X = B. x) return. Y <B. y; // remember that X occupies y return a at the same time. x <B. x;} void build (INT POs, int le, int RI) {f [POS]. le = Le; F [POS ]. Ri = Ri; F [POS]. va = 0; If (Le = RI) return; int mid = mid (Le, RI); Build (L (POS), Le, mid ); build (R (POS), Mid + 1, RI);} void Update (INT POs, int le) {f [POS]. va ++; If (F [POS]. le = Le & Le = f [POS]. RI) return; int mid = mid (F [POS]. le, F [POS]. RI); If (mid> = Le) Update (L (POS), Le); else Update (R (POS), Le);} int query (INT POs, int le, int RI) {If (F [POS]. le = Le & F [POS]. ri = RI) return f [POS]. va; int mid = mid (F [POS]. le, F [POS]. RI ); If (mid> = RI) return query (L (POS), Le, RI); else if (mid <le) return query (R (POS), Le, RI ); return query (L (POS), Le, mid) + query (R (POS), Mid + 1, RI);} int main () {int I, N; while (~ Scanf ("% d", & N) {for (I = 0; I <n; I ++) scanf ("% d ", & S [I]. x, & S [I]. y); memset (ANS, 0, sizeof (ANS); sort (S, S + N, CMP); Build (, N-1); for (I = 0; I <n; I ++) {int x = query (1, 0, s [I]. y); ans [x] ++; update (1, s [I]. y);} for (I = 0; I <n; I ++) printf ("% d \ n", ANS [I]);} return 0 ;}
HDU 1541 stars (line segment tree)