Zoj 3129 | poj 3067 Japan

Source: Internet
Author: User

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 >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.