Astequalmers 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 writing a program that will count the amounts of the stars of each level on a given map. inputthe first line of the input 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
Y Coordinate. Stars with equal
Y Coordinates are listed in ascending order
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
Level
N −1. Sample
| Input |
Output |
51 15 17 13 35 5 |
12110 |
This is a variable segment.
I have taken the test on behalf of others. The general method is as follows.
Use level [p] to remember when the first row of the P column of the star does not add one (as a result, the number of incoming rows increases by row, all these data sets are calculated by row). The CNT [level] records are used to calculate the total number of stars at the level.
Insert a star in binary mode.ProgramNote.
Accode:
# Include <cstdio> # include <cstring> # include <cstdlib> # include <bitset> const char fi [] = "ural1028.in"; const char fo [] = "ural1028.out "; const int maxn = 15010; const int maxm = 32010; const int max = 0x3fffff00; const int min =-Max; int level [maxm]; int CNT [maxn]; int N, x, Y, TMP; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W", stdout);} void insert (int x, int L, int R) {int mid = (L + r)> 1; if (x <mid) {++ level [Mid]; // The value of X is in the left half of the MID, so the same half of the MID is + 1. Insert (x, L, mid-1); // returns two-digit search. Return;} If (x = mid) {TMP + = level [Mid]; // you can add the cursor of the first star at the bottom of X. ++ Level [Mid]; // Add x itself.} If (x> mid) {TMP + = level [Mid]; // If the mid value is in the left half of X, add the equal half of the mid value to X. Insert (x, Mid + 1, R); // returns two-digit search.} Void readdata () {scanf ("% d", & N); For (INT I = 0; I <n; ++ I) {scanf ("% d", & X, & Y); TMP = 0; // used to remember the number of stars that have been inserted before. Insert (x, 0, maxm-1); ++ CNT [TMP]; // The operator of the specified vertex.} For (INT I = 0; I <n; ++ I) printf ("% d \ n", CNT [I]);} int main () {init_file (); readdata (); exit (0 );}
Paste the version of a tree array:
# Include <cstdio> # include <cstdlib> # include <algorithm> # include <string> # define lowbit (x) & (-(X ))) const int maxn = 15010; const int Maxx = 32010; int A [Maxx], lev[ maxn], n; inline void ins (int x) {for (INT I = X; I <Maxx; I + = lowbit (I) ++ A [I]; return;} inline int sum (INT X) {int ans = 0; for (INT I = x; I-= lowbit (I) ans + = A [I]; return ans;} inline int getint () {int res = 0; char TMP; whi Le (! Isdigit (TMP = getchar (); Do res = (RES <3) + (RES <1) + TMP-'0 '; while (isdigit (TMP = getchar (); Return res;} int main () {freopen ("stars. in "," r ", stdin); freopen (" stars. out "," W ", stdout); n = getint (); For (INT I = 0; I <n; ++ I) {int x = getint () + 1, y = getint () + 1; + lev[ sum (x)]; ins (x);} For (INT I = 0; I <N; ++ I) printf ("% d \ n", lev[ I]); Return 0 ;}# UNDEF lowbit