HDU 1542 "Atlantis" (scan line segment tree +)

 

Portal

 

• the meaning of problems

  You n rectangles, each rectangle gives you $ (x_1, y_1), (x_2, y_2) $ of this rectangle, respectively The corners;

  Allows you to find the n and the rectangular area;

  Where $ x \ leq 10 ^ {5} \, \ y \ leq 10 ^ {5} $;

•answer

  Such practice solutions need to use a more important algorithm - scanning line algorithm;

  In fact, we do not need to scan line algorithm how thorough study of such questions only used the idea of ​​a scan line algorithm;

  Here we talk about how to deal with such problems begin scanning line;

  Suppose you have two rectangles, as shown in FIG;

    

 

  The rectangular ① The corners are: $ (1.2 \, \ 1.4), (4.5 \, \ 3.4) $

  ② The corners of the rectangle are: $ (3.5 \, \ 2.6), (7.6 \, \ 4.8) $

  Now, there is a virtual scan line, from top to bottom scan the entire polygon;

    

  Start scanning from the scanning line position, scanning line 1 calculates the length of the rectangle is scanned $ line_ {1} = 4.5-1.2 = 3.3 $;

  Scan line from position 1 up to position 2, calculated from the scanning lines 1, 2 to scan the rectangular area $ area_ {1} = line_ {1} \ times (h_2-h_1) $;

  And calculate the scanning line 2 to the scanning length of the rectangle $ line_ {2} = 7.6-1.2 = 6.4 $;

  Scan line from the second position upwardly to position 3, the scanning lines 2 and 3 is calculated to scan a rectangular area of ​​$ area_ {2} = line_ {2} \ times (h_ {3} -h_ {2}) $;

  3 and calculates a scan line length of the rectangle is scanned $ line_ {3} = 7.6-3.5 = 4.1 $;

  Scan line from the third position upwardly to position 4, a scan line is calculated to scan a rectangular area of ​​3,4 $ area_ {3} = line_ {3} \ times (h_ {4} -h_ {3}) $;

  With this, it is determined by the scan lines and the area of ​​the rectangle $ area = area_ {1} + area_ {2} + area_ {3} $;

  Through the above simulation, can know in order to use the scanning line, then you need to do the following preparations:

    (1) Keep the rectangle on the bottom

    (2) ordering information in accordance with the height will be saved

  So how do you record it?

  We can use the structure to hold all of the lower structure is defined as follows:

. 1  struct the Data
 2  {
 . 3      Double L, R & lt; /// Save information about side 
. 4      Double H; /// Save edge height 
. 5      int F; /// determines that the upper edge of the rectangle is located under and above it assigning -1, assigned to the lower side. 1 
. 6      BOOL  operator <( const the Data & obj) const /// arranged in ascending order of height of the edge 
. 7      {
 . 8          return H < obj.h;
 . 9      }
 10 } a [MAXN << . 1 ];

  Then, both sides of the rectangular information is stored in a sequence of side by side as well:

    $a_{1}:\{l=1.2\ ,\ r=4.5\ ,\ h=1.4\ ,\ f=1 \}$

    $a_{2}:\{l=3.5\ ,\ r=7.6\ ,\ h=2.6\ ,\ f=1 \}$

    $a_{3}:\{l=1.2\ ,\ r=4.5\ ,\ h=3.4\ ,\ f=-1 \}$

    $a_{4}:\{l=3.5\ ,\ r=7.6\ ,\ h=4.8\ ,\ f=-1 \}$

  Because the coordinate is large, the abscissa needs to be discrete;

•Code

  HDU1542.cpp

Guess you like

Origin www.cnblogs.com/violet-acmer/p/11459504.html