Uva_11983
This question can be converted into finding the area of the rectangle that is covered K times. Simply add X2 and Y2 to the area of 1, which is equivalent to seeing every square unit as a point.
# Include <stdio. h> # Include < String . H> # Include <Stdlib. h> # Define Maxk 11 # Define Maxd 60010 Int N, K, M, Len [ 4 * Maxd] [maxk], CNT [ 4 *Maxd], Ty [maxd]; Struct SEG { Int X, Y1, Y2, Col;} seg [maxd]; Int Cmpint ( Const Void * _ P, Const Void * _ Q ){ Int * P = ( Int *) _ P, * q = ( Int *) _ Q; Return * P <* Q? - 1 : 1 ;} Int CMPS ( Const Void * _ P, Const Void * _ Q) {seg * P = (SEG *) _ p, * q = (SEG * ) _ Q; Return P-> x <q-> X? - 1 :1 ;} Void Build ( Int Cur, Int X, Int Y ){ Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1 ; Memset (LEN [cur], 0 ,Sizeof (LEN [cur]); Len [cur] [ 0 ] = Ty [Y + 1 ]- Ty [X]; CNT [cur] = 0 ; If (X = Y) Return ; Build (LS, X, mid); Build (RS, mid + 1 , Y );} Void Init (){ Int I, J, K, X1, Y1, X2, Y2; scanf ( " % D " , & N ,& K ); For (I = 0 ; I <n; I ++ ) {J = I < 1 , K = (I < 1 ) | 1 ; Scanf ( " % D " , & X1, & Y1, & X2 ,& Y2 ); ++ X2, ++ Y2; seg [J]. x = X1, SEG [K]. x = X2; seg [J]. Y1 = Seg [K]. Y1 = Y1, SEG [J]. y2 = seg [K]. y2 = Y2; seg [J]. Col = 1 , SEG [K]. Col =- 1 ; Ty [J] = Y1, Ty [k] = Y2;} qsort (TY, n < 1 , Sizeof (TY [ 0 ]), Cmpint); m =- 1 ; For (I = 0 ; I <(n < 1 ); I ++ ) If (I = 0 | Ty [I]! = Ty [I- 1 ]) Ty [ ++ M] =Ty [I]; build ( 1 , 0 , M- 1 );} Int BS ( Int X ){ Int Mid, min = 0 , Max = m + 1 ; For (;) {Mid = (Min + max)> 1 ; If (Mid = Min) Break ; If (TY [Mid] <= X) min = Mid; Else Max = Mid ;} Return Mid ;} Void Update ( Int Cur, Int X, Int Y ){ Int Ls = cur < 1 , RS = (cur < 1 ) | 1 ; Memset (LEN [cur], 0 , Sizeof (LEN [cur]); If (CNT [cur]> = K) Len [cur] [k] = Ty [Y +1 ]- Ty [x]; Else If (X = Y) Len [cur] [CNT [cur] = Ty [Y + 1 ]- Ty [x]; Else { Int I; For (I = CNT [cur]; I <= K; I ++ ) Len [cur] [I] + = Len [ls] [I-CNT [cur] + Len [RS] [I-CNT [cur]; For (I = k-CNT [cur] + 1 ; I <= K; I ++ ) Len [cur] [k] + = Len [ls] [I] + Len [RS] [I] ;}} Void Refresh ( Int Cur, Int X, Int Y, Int S, Int T, Int C ){ Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1 ; If (X> = S & Y <= T) {CNT [cur] + = C; Update (cur, x, y ); Return ;} If (Mid> =S) Refresh (LS, X, mid, S, T, C ); If (Mid + 1 <= T) Refresh (RS, mid + 1 , Y, S, T, C); Update (cur, x, y );} Void Solve (){ Int I, J, K; Long Long Int Ans = 0 ; Qsort (SEG, n < 1 , Sizeof (SEG [ 0 ]), CMPs); seg [n < 1 ]. X = seg [(n < 1 )- 1 ]. X; For (I = 0 ; I <(n < 1 ); I ++ ) {J = BS (SEG [I]. Y1), k =BS (SEG [I]. Y2); refresh ( 1 , 0 , M- 1 , J, k- 1 , SEG [I]. col); ans + = ( Long Long Int ) Len [ 1 ] [K] * (SEG [I + 1 ]. X- SEG [I]. X);} printf ( " % LLD \ n " , ANS );} Int Main (){ Int T, TT; scanf ( " % D " ,& T ); For (Tt = 0 ; TT <t; TT ++ ) {Init (); printf ( " Case % d: " , Tt + 1 ); Solve ();} Return 0 ;}