Title Address: http://poj.org/problem?id=2352
Analysis:
-Test Instructions Analysis: There are n stars, the number of stars whose lower left (x and Y does not exceed it) is its level, counting the number of stars at level 0 to n-1, respectively. The input is sorted by Y from small to large, and if Y is the same, it will be sorted by X, and no more than 2 stars will occupy the same coordinates.
-Data structure
-Use the original data array A to represent the number of stars in the same x-coordinate. (A[i] is the number of stars with an x-coordinate of i-1). Note the sort when you enter data.
-The tree array s maintains the information of a.
-Level:level[i] The number of stars that record level I.
-Calculation
-Tree Array Construction: Because the input is sorted by Y first and then sorted by x, it is not built with array a (at first A is full 0, S is all 0), but it is updated directly from scratch based on input.
-Calculate level:sumn (int x) calculates the number of stars with an x-coordinate that is not greater than X, and the star with the Y value larger than the current star, as entered, has not been entered. So this number is the current level of the star, and the subsequent input of the Y value will not be smaller than this, so the subsequent input, the current level of the star will not change.
1#include <iostream>2#include <cstring>3 using namespacestd; 4 5 #defineMax_index_range 32010//index is from 1.6 7 //The original data array is hidden, a[i] represents the number of stars with x coordinates of I.8 //S[1:n] Tree array representing the 1th to N star. The subscript starts at 1.9 intS[max_index_range];Ten //Level[i] Indicates the number of stars at level I. One intLevel[max_index_range]; A - intLowbit (intx) { - returnX & (-x); the } - - //calculates the total number of stars whose x-coordinate is not greater than X. Because the tree array is dynamically constructed, the Y value is not entered in the order of entry, and the number is not the current star, so this is the number at the bottom left of the current star . - intSumn (intx) { + intsum =0; - while(X >0){ +Sum + =S[x]; AX-=lowbit (x); at } - returnsum; - } - - //N: The actual number of total stars. - voidModifyintNintDeltaintN) { in //Note: The range of s[i] is the maximum value of the star coordinates, because the original data array a represents the x-coordinate. - while(N <=Max_index_range) { toS[n] + =Delta; +n + =lowbit (n); - } the } * $ intMain () {Panax Notoginseng intn,x,y,i; - while(cin>>N) { thememset (s),0,sizeof(s)); +Memset (Level,0,sizeof(level)); A for(i=0; i<n;++i) { theCin>>x>>y; +x + +;//because the tree-like array coordinates start at 1. -Level[sumn (x)]++; $Modify (x,1, N); $ } - for(i=0; i<n;++i) { -cout<<level[i]<<Endl; the } - }Wuyi return 0; the}
Tree array-2352 Stars