The so-called discretization (one-dimensional discretization is discussed here) is a point set on a straight line. We ignore the distance between them and only consider their relative positions. This is discretization.
For example, a point set 1, 5, 8, and 3 can be discretely converted into 1, 3, and 4, so that their relative positions remain unchanged, but the highest bit points are greatly reduced, meeting the spatial requirements of the Line Segment tree.
How to Implement discretization: We can open an array a [1] = 1, a [3] = 4. A [4] = 8, a [2] = 3. In this way, the point-to-origin ing after Discretization can be completed. In turn, we can use the 2-point lookup function, the lgn complexity is used to find the ing between the origin point and the discrete point.
It is easy to solve the space problem of the Line Segment tree. We need to merge the Rectangles and the bottom ends of all the rectangles are aligned. Therefore, we do not need to consider the low end, we only need to consider the height and horizontal distance, which exactly fits the one-dimensional line segment tree. Every time we insert a rectangle, We can insert this horizontal edge and record its height, finally, we can easily calculate the area.
# Include <algorithm>
# Include <iostream>
Using namespace STD;
Const int n= 40010;
Struct tree
{
_ Int64 L, R; // The number of discrete points
_ Int64 high;
} T [8 * n];
Struct point
{
Int beg, end;
Int Hi;
} Dia [N];
Int node [2 * n];
Int all;
Void maketree (int c, int L, int R)
{
T [C]. L = L;
T [C]. r = R;
T [C]. High = 0;
If (L + 1 = r)
{
Return;
}
Int mid = (L + r)> 1;
Maketree (C * 2, L, mid );
Maketree (2 * C + 1, mid, R );
}
Void Update (int c, int L, int R, int num)
{
If (T [C]. L = L & T [C]. r = r)
{
T [C]. High = num;
Return;
}
If (T [C]. High! = 0)
{
T [2 * C]. High = T [2 * C + 1]. High = T [C]. High;
T [C]. High = 0;
}
Int mid = (T [C]. L + T [C]. R)> 1;
If (r <= mid)
Update (2 * C, L, R, num );
Else if (L> = mid)
Update (2 * C + 1, L, R, num );
Else
{
Update (2 * C, L, mid, num );
Update (2 * C + 1, mid, R, num );
}
}
Long long findans (int c)
{
_ Int64 sum1, sum2;
If (T [C]. High! = 0 | T [C]. L + 1 = T [C]. R)
{
Return (_ int64) (T [C]. High * (node [T [C]. R]-node [T [C]. l]);
}
Else
{
Sum1 = findans (C * 2 );
Sum2 = findans (C * 2 + 1 );
Return sum1 + sum2;
}
}
Int F (INT num)
{
Int L = 1, R = all;
While (L <= r)
{
Int mid = (L + r)> 1;
If (num = node [Mid])
Return mid;
If (Num <node [Mid])
R = mid-1;
Else
L = Mid + 1;
}
}
Int CMP (const void * a, const void * B)
{
If (* (point *) a). Hi = (* (point *) B). Hi)
Return (* (point *) a). Beg-(* (point *) B). Beg;
Else
Return (* (point *) a). Hi-(* (point *) B). Hi;
}
Int main ()
{
Int N, I;
_ Int64 ans = 0;
Int CNT = 1;
Scanf ("% d", & N );
For (I = 0; I <n; I ++)
{
Scanf ("% i64d % i64d % i64d", & Dia [I]. Beg, & Dia [I]. End, & Dia [I]. Hi );
Node [CNT ++] = Dia [I]. Beg;
Node [CNT ++] = Dia [I]. end;
}
Sort (node + 1, node + CNT );
All = 1;
For (I = 2; I <CNT; I ++)
{
While (I <CNT & node [I] = node [I-1]) I ++;
If (I! = CNT)
Node [++ all] = node [I];
}
Qsort (Dia, N, sizeof (DIA [0]), CMP );
Maketree (1, 1, all );
For (I = 0; I <n; I ++)
{
Int B = f (DIA [I]. Beg );
Int e = f (DIA [I]. End );
// Cout <B <"<e <Endl;
Update (1, B, E, DIA [I]. Hi );
}
Printf ("% i64d/N", findans (1 ));
Return 0;
} // PS: The PKU data is disgusting!