Calculate the star level. The number of other stars in the lower left of the star is the level of the Star (which can be in the same horizontal or vertical line). Because y and X are given progressively, increasing the number of stars at Layer y has no effect on the Y-1 layer. Every time you add a star (x, y), you only need to count the number of stars in the [] range to get its level value. In addition, you need to update the array after X. The algorithm is implemented using a tree array or line segment tree. The Code is as follows:
1 # include <iostream> 2 # include <cstdio> 3 using namespace STD; 4 # define Max 32010 5 Int star [Max]; 6 int level [Max]; 7 int lowbit (int x); 8 int sum (int K); 9 void Update (int K); 10 int main () 11 {12 // freopen ("D: \ acm.txt "," r ", stdin); 13 int xnum = 0, ynum = 0; 14 int linenum = 0; 15 scanf (" % d ", & linenum ); 16 For (INT I = 0; I <linenum; ++ I) {17 scanf ("% d", & xnum, & ynum ); 18 level [sum (xnum + 1)] ++; // Add 1 or a positive integer to xnum to avoid being 0. Otherwise, 19 Update (xnum + 1) is reported ); 20} 21 for (INT I = 0; I <linenum; ++ I) 22 printf ("% d \ n", level [I]); 23 return 0; 24} 25 int lowbit (int x) {26 return X & (-x); // return the start Number of the last 1 position 27} 28 int sum (int K) {// The first K items and 29 int judge = 0; 30 While (k) {31 judge = judge + star [k]; 32 K = k-lowbit (k ); // 33} 34 return judge; 35} 36 void Update (int K) {// modify the K entry and the updated content 37 while (k <max) {38 star [k] ++; 39 K = K + lowbit (k); 40} 41}
Attach the line segment tree solution (not written by me) for future study. Now, I can't think of myself as I can understand the code and understand my thoughts. Continue to work hard!
Http://blog.chinaunix.net/uid-22263887-id-1778936.html
Poj 2352 star