Give some rectangles to calculate the area of these rectangles. A rectangle is the coordinates in the lower left corner and lower right corner. It is a positive integer.
Idea: a simple line segment tree question is to find the rectangular area. Because they are all integers and the data range is small, discretization is not required.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string. h>
Using namespace std;
Const int N = 50010, M = 1010;
Struct tree {
Int lp, rp, len, num;
Int getmid (){
Return (lp + rp)/2;
}
} Tt [N * 4];
Struct line {
Int lp, rp, cnt, value;
} LL [M * 2];
Void built_tree (int lpos, int rpos, int pos ){
Tt [pos]. lp = lpos;
Tt [pos]. rp = rpos;
Tt [pos]. len = tt [pos]. num = 0;
If (tt [pos]. lp + 1 = tt [pos]. rp)
Return;
Int mid = tt [pos]. getmid ();
Built_tree (lpos, mid, pos * 2 );
Built_tree (mid, rpos, pos * 2 + 1 );
}
Bool cmp (line a, line B ){
Return a. value> B. value;
}
Void getlen (int pos ){
If (tt [pos]. num> 0)
Tt [pos]. len = tt [pos]. rp-tt [pos]. lp;
Else if (tt [pos]. lp + 1 = tt [pos]. rp)
Tt [pos]. len = 0;
Else
Tt [pos]. len = tt [pos * 2]. len + tt [pos * 2 + 1]. len;
}
Void update (int pos, int id ){
If (tt [pos]. lp> = LL [id]. lp & tt [pos]. rp <= LL [id]. rp ){
Tt [pos]. num + = LL [id]. cnt;
Getlen (pos );
Return;
}
If (tt [pos]. lp + 1 = tt [pos]. rp)
Return;
Int mid = tt [pos]. getmid ();
If (LL [id]. lp> = mid ){
Update (pos * 2 + 1, id );
}
Else if (LL [id]. rp <= mid ){
Update (pos * 2, id );
} Www.2cto.com
Else {
Update (pos * 2, id );
Update (pos * 2 + 1, id );
}
Getlen (pos );
}
Int main (){
// Freopen ("1.txt"," r ", stdin );
Int a, B, c, d, tot = 0;
While (scanf ("% d", & a, & B, & c, & d) & (a + B + c + d )! =-4 ){
Tot = 0;
LL [tot]. lp = a; LL [tot]. rp = c; LL [tot]. value = d; LL [tot ++]. cnt = 1;
LL [tot]. lp = a; LL [tot]. rp = c; LL [tot]. value = B; LL [tot ++]. cnt =-1;
Int x1, y1, x2, y2;
While (1 ){
Scanf ("% d", & x1, & y1, & x2, & y2 );
If (x1 + y1 + x2 + y2 =-4)
Break;
LL [tot]. lp = x1; LL [tot]. rp = x2; LL [tot]. value = y2; LL [tot ++]. cnt = 1;
LL [tot]. lp = x1; LL [tot]. rp = x2; LL [tot]. value = y1; LL [tot ++]. cnt =-1;
}
Built_tree (0, N, 1 );
Sort (LL, LL + tot, cmp );
Update (1, 0 );
Long ans = 0;
For (int I = 1; I <tot; ++ I ){
Ans + = tt [1]. len * (LL [I-1]. value-LL [I]. value );
Update (1, I );
}
Printf ("% lld \ n", ans );
}
Return 0;
}