HDU1828 求周長並 線段樹+離散化

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 415    Accepted Submission(s): 237

Problem Description

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.



The corresponding boundary is the whole set of line segments drawn in Figure 2.



The vertices of all rectangles have integer coordinates.

 

 

Input

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.

 

 

Output

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

 

 

Sample Input

7

-15 0 5 10

-5 8 20 25

15 -4 24 14

0 -6 16 4

2 15 10 22

30 10 36 20

34 0 40 16

 

 

Sample Output

228

 

 

離散化 + 線段樹 .

讀入數據時記錄豎着的邊的兩端座標 x,y1,y2 Y 軸上出現過的座標 tempy[], tempy 從小到大排序 , 去掉重複的 y 軸座標轉存到數組 y[], 利用 y[] 的數據對 Y 軸進行離散化建立線段樹 , 然後對豎着的邊排序 , 從左到右掃描 , 根據掃描到的豎邊的 y 軸覆蓋區間對線段樹進行維護更新 , 統計覆蓋的總長度 , 每更新一次都記錄一下 y 軸被覆蓋的總長度 , 每次更新之後 y 軸覆蓋的總長度的變化量就是輪廓的周長增加的長度 . 同時加上 x 軸上增加的長度 ( 如果 y 軸有被覆蓋的話 , 這個用線段樹的根節點可以判斷 ), 直到掃描結束 .

 

代碼如下 :

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章