Same as poj 2299, it is also an application that uses a tree array to calculate the number of reverse orders.
Sort X from large to small. If X is the same, sort y from large to small. Create a tree array for Y. Based on the definition of the number of reverse orders, it is clear to draw a graph algorithm once.
# Include < Stdio. h >
# Include < Stdlib. h >
# Include < String . H >
# Define Maxn 1001
Struct Pair {
Int X, Y;
} P [ 1000010 ];
Int N, m, K;
Int C [maxn];
Int CMP ( Const Void * A, Const Void * B ){
If (Pair * )) -> X = (Pair * ) B) -> X)
Return (Pair * ) B) -> Y - (Pair * )) -> Y;
Return (Pair * ) B) -> X - (Pair * )) -> X;
}
Inline Int Lowbit ( Int T ){
Return T & ( - T );
}
Void Update ( Int I ){
While (I <= M ){
C [I] + = 1 ; // Increment is 1
I + = Lowbit (I );
}
}
Int Getsum ( Int I ){
Int Sum = 0 ;
While (I > 0 ){
Sum + = C [I];
I -= Lowbit (I );
}
Return SUM;
}
Int Main (){
Int T, T = 0 ;
Scanf ( " % D " , & T );
While (T -- ){
Scanf ( " % D " , & N, & M, & K );
For ( Int I = 1 ; I <= K; ++ I ){
Scanf ( " % D " , & P [I]. X, & P [I]. y );
}
Qsort (P + 1 , K, Sizeof (P [ 0 ]), CMP );
Long Long Ans = 0 ;
For ( Int I = 1 ; I <= K; ++ I ){
Ans + = Getsum (P [I]. Y - 1 );
Update (P [I]. y );
}
Printf ( " Test Case % d: % LLD \ n " , ++ T, ANS );
Memset (C, 0 , Sizeof (C ));
}
Return 0 ;< BR >}