Remove the longitudinal edges in x-coordinate order and create a segment tree in the y direction.
Current effective length of each query Len,ans + = len* (x[i]-x[i-1]); where Len is T[rt].len;
After the query is finished, update the line tree in the y direction, enter the Edge +1, and exit-1.
#include <bits/stdc++.h>using namespacestd;#defineLson L, M, rt<<1#defineRson m+1, R, rt<<1|1typedefLong Longll;structl{Doublex, y1, y2; intD; L () {} l (DoubleXDoubleY1,DoubleY2,intd): X (x), y1 (y1), y2 (y2), d (d) {}}; L line[ -];BOOLCMP (l A, L B) {returnA.x <b.x;}Doubley[ -];structnode{intD; DoubleLen;}; Node t[ -<<3];voidinit () {memset (T,0,sizeof(T));}voidPushup (intRtintLintR) { if(T[RT].D) T[rt].len= y[r]-y[l-1]; ElseT[rt].len= L = = r?0: t[rt<<1].len+t[rt<<1|1].len;}voidUpdateintLintRintDintLintRintRT) { if(L <= L&&r <=R) {T[RT].D+=D; Pushup (RT, L, R); return ; } intm = (l+r) >>1; if(L <=m) Update (L, R, D, Lson); if(R >m) Update (L, R, D, Rson); Pushup (RT, L, R);}intMain () {intN, CA =1; Doublex1, y1, x2, y2; while(SCANF ("%d", &N), N) { for(inti =0; I < n; i++) {scanf ("%LF%LF%LF%LF", &x1, &y1, &X2, &y2); Line[i*2] = L (x1, y1, Y2,1); Line[i*2+1] = L (x2, y1, Y2,-1); Y[i*2] = y1, y[i*2+1] =Y2; } sort (line, line+2*N, CMP); Sort (y, y+2*N); Init (); DoubleAns =0, Lastx = line[0].x; for(inti =1; I <2*n; i++){ if(i&&line[i].x! = line[i-1].x) ans+ = (line[i].x-line[i-1].x) *t[1].len; intL = Lower_bound (Y, y+2*n, Line[i].y1)-y+1, r = Lower_bound (Y, y+2*n, Line[i].y2)-y; if(L <=r) Update (L, R, LINE[I].D,1,2*n,1); } printf ("Test Case #%d\n", ca++); printf ("Total explored area:%.2f\n\n", ans); } return 0;}View Code
No pushdown () function, each segment is saved one time. D indicates the number of times to be overwritten, and Len indicates the legal length at least once. See Pushup () function.
Ans + = t[1].len* (x[i]-x[i-1]);
For the area of intersection : The method of the same area and, plus len2 represents at least two times the legal length, ans + = t[1].len2* (x[i]-x[i-1]);
The circumference and : The method is the same area and, scan two times, respectively along the x direction and the y direction, each time plus the update before and after t[1].len the absolute value of the difference.
HDU1542 Rectangular Area and