Maybe I didn't have much knowledge about computational ry. So far I don't have a clear understanding of the scanning line algorithm. I remember the definition of ZY was as follows:
Each event has a start time and end time. All time points are sorted and scanned in sequence. If the start time is used, the time is added. Otherwise, the event is deleted. The current event can be processed at any time.
Scanning lines are used more frequently in computational ry. I just gave a rough look at the line segment tree list. I can only discuss things and cannot make overall planning. The "rectangle area" is a classic application that combines the scanning line with the line segment tree. It is also a classic example of discretization of the range.
Rectangular area and: There are N rectangles on the plane, each side is parallel to the coordinate axis, and the total area covered by them is calculated (repeated coverage is counted only once ).
Solution: sort the vertical edges of a rectangle by x coordinate. Each rectangle is regarded as an event, and the left vertical edges of a rectangle are regarded as the start and right vertical edges of the event as the end of the event, in this way, we can calculate the area of the rectangle between any two adjacent edges (the x-axis * X-Coordinate Difference currently covered). We can understand it after a single graph...
The next step is the process of interval discretization and statistic maintenance. After discretization, there are m abscissa. If m nodes are created, the length of the Line Segment Tree node cannot be calculated (the simplest is the leaf node, the length is 0, which is obviously incorrect). Therefore, the first leaf node represents I ~ The distance between I + 1 (here we know that the original understanding of Poster is completely unknown, not the problem of semi-open and semi-closed, but the problem of saving information on nodes ). The line segment tree with the rectangle area is strange. Although the range value is updated, the pushdown operation is not required. And pushup () immediately after updating the self-interval, the line segment tree must be flexible in many ways...
Poj1151 AtlantaArea ratio of the floating-point rectangle to be discretization
Import java. text. decimalFormat; import java. util. arrays; import java. util. secret; public class Atlantis1151 {class SegTree {class node {int left, right, key; double sum; void init (int l, int r) {left = l; right = r; sum = 0; key = 0;} int mid () {return (left + right)> 1;} double length () {return hash [right + 1]-hash [left] ;}} node tree []; SegTree (int maxn) {maxn = maxn * 3; tree = new node [maxn]; for (int I = 1; I <maxn; I ++) tree [I] = new node ();} void init (int l, int r, int idx) {tree [idx]. init (l, r); if (l = r) return; int mid = tree [idx]. mid (); init (l, mid, idx <1); init (mid + 1, r, (idx <1) | 1 );} void update (int left, int right, int idx, int v) {if (tree [idx]. left> = left & tree [idx]. right <= right) {tree [idx]. key + = v; pushup (idx); return;} int mid = tree [idx]. mid (); if (left <= mid) update (left, ri Ght, idx <1, v); if (right> mid) update (left, right, (idx <1) | 1, v); pushup (idx );} void pushup (int idx) {if (tree [idx]. key> 0) tree [idx]. sum = tree [idx]. length (); else {if (tree [idx]. left = tree [idx]. right) tree [idx]. sum = 0; elsetree [idx]. sum = tree [idx <1]. sum + tree [(idx <1) | 1]. sum ;}} scanned scan = new partition (System. in); class edge implements Comparable <edge> {int s, t, v; double x; public Int compareTo (edge oth) {if (x <oth. x) return-1; if (x = oth. x & v> oth. v) return-1; // The error found when the rectangle perimeter is located. It seems that the area is correct. It may be because the data is too weak. Write it first, study return 1 ;}} class zone implements Comparable <zone> {int id; double h; public int compareTo (zone o) {if (h <o. h) return-1; return 1 ;}} SegTree st = new SegTree (310); edge [] ed = new edge [310]; zone [] zn = new zone [310]; int n, cnt; double hash [] = new double [310]; void hash () {Arrays. Sort (zn, 1, n * 2 + 1); cnt = 1; for (int I = 1; I <= 2 * n; I ++) {if (I> 1 & zn [I]. h! = Zn [I-1]. h) cnt ++; hash [cnt] = zn [I]. h; int temp = zn [I]. id; if (temp> 0) ed [temp]. s = ed [temp + 1]. s = cnt; elseed [-temp]. t = ed [-temp + 1]. t = cnt ;}} void init () {for (int I = 0; I <= 300; I ++ = 2) {ed [I] = new edge (); ed [I + 1] = new edge (); zn [I] = new zone (); zn [I + 1] = new zone () ;}} void run () {int cas = 0; DecimalFormat df = new DecimalFormat ("0.00"); init (); while (true) {cas ++; n = scan. nextInt (); if (n = 0) break; System. out. println ("Test case #" + cas); for (int I = 1; I <= n * 2; I + = 2) {ed [I]. x = scan. nextDouble (); ed [I]. v = 1; zn [I]. id = I; zn [I]. h = scan. nextDouble (); ed [I + 1]. x = scan. nextDouble (); ed [I + 1]. v =-1; zn [I + 1]. id =-I; zn [I + 1]. h = scan. nextDouble ();} hash (); Arrays. sort (ed, 1, n * 2 + 1); st. init (1, cnt-1, 1); st. update (ed [1]. s, ed [1]. t-1, 1, 1); double ans = 0; for (int I = 2; I <= n * 2; I ++) {ans + = st. tree [1]. sum * (ed [I]. x-ed [I-1]. x); st. update (ed [I]. s, ed [I]. t-1, 1, ed [I]. v);} System. out. println ("Total received area:" + df. format (ans); System. out. println () ;}} public static void main (String [] args) {new Atlantic 1151 (). run ();}}
Ps: many great Gods prefer to use binary for discretization. The specific encoding complexity and event complexity need to be further compared, but I still feel comfortable writing my own method, so I will not change it for the time being.
Rectangular perimeter and:It is a little more complex than the area, and the horizontal and vertical sides must be separately counted. Paste a pushup () and Statistical code, and draw a picture to understand it. (pay attention to the coincidence of the two vertical edges. This data exists in poj1177discuss)
Void pushup (int idx) {if (tree [idx]. cnt> 0) {tree [idx]. sum = tree [idx]. length (); // interval overwrite length tree [idx]. lp = tree [idx]. rp = 1; // whether the Left and Right endpoints are overwritten by tree [idx]. num = 1; // There are several scattered intervals in the interval} else {if (tree [idx]. left = tree [idx]. right) {tree [idx]. num = tree [idx]. sum = 0; tree [idx]. lp = tree [idx]. rp = 0;} else {int l = idx <1; int r = l + 1; tree [idx]. lp = tree [l]. lp; tree [idx]. rp = tree [r]. rp; tree [idx]. sum = tree [l]. sum + tree [r]. sum; tree [idx]. num = tree [l]. num + tree [r]. num-(tree [l]. rp & tree [r]. lp );}}}
For (int I = 1; I <= n * 2; I ++) {if (I> 1) ans + = st. tree [1]. num * (ed [I]. x-ed [I-1]. x) * 2; // The horizontal edge st. update (ed [I]. s, ed [I]. t-1, 1, ed [I]. v); ans + = Math. abs (st. tree [1]. sum-last); // vertical edge last = st. tree [1]. sum ;}