Japan
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:14795 |
|
Accepted:3968 |
Description
Japan plans to welcome the acm icpc world finals and a lot of roads must be built for the venue. japan is tall island with N cities on the East Coast and M cities on the West Coast (M <= 1000, n <= 1000 ). k superhighways will be build. cities on each coast are numbered 1, 2 ,... from north to south. each superhighway is straight line and connects city on the East Coast with city of the West Coast. the funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. at most two superhighways cross at one location. write a program that calculates the number of the crossings between superhighways.
Input
The input file starts with t-the number of test cases. each test case starts with three numbers-n, m, K. each of the next K lines contains two numbers-the numbers of cities connected by the superhighway. the first one is the number of the city on the East Coast and second one is the number of the City of the West Coast.
Output
For each test case write one line on the standard output:
Test Case (case number): (number of crossings)
Sample Input
13 4 41 42 33 23 1
Sample output
Test Case 1: 5
Source
Southeastern Europe 2006 meaning: the east and west coast of Japan have n, m cities, and want to build roads together. Returns the number of intersections between cities. sorts the intersections and returns the reverse order. You can sort by merging, or use a tree array. The tree array is used here. Note that the edge range is relatively large, 1000*1000. The result is greater than Int. Use long
/* Poj 3067 Japan is sorted by the first number from small to large. If the first number is the same, it is sorted by the second number from small to large. Then the tree array is used to find the reverse order. The side is relatively large, and the result must be long. */ # Include <Stdio. h> # Include <Algorithm> # Include < String . H> # Include <Iostream> Using Namespace STD; Const Int Maxn =1010 ; Int C [maxn]; Struct Node { Int A, B;} node [ 1000 * 1000 ]; Int N, m, K; Bool CMP (node A, Node B ){ If (A. A = B.)Return A. B < B. B; Return A. A < B. ;} Int Lowbit ( Int X ){ Return X &(- X );} Void Add ( Int I, Int Val ){ While (I <=M) {C [I] + = Val; I + = Lowbit (I );}} Int Sum ( Int I ){ Int S = 0 ; While (I> 0 ) {S + = C [I]; I -= Lowbit (I );} Return S ;} Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int Icase = 0 ; Int T; scanf ( " % D " ,&T ); While (T -- ) {Icase ++ ; Scanf ( " % D " , & N, & M ,& K ); For ( Int I = 1 ; I <= K; I ++ ) {Scanf ( " % D " , & Node [I]. ,& Node [I]. B);} Sort (node + 1 , Node + 1 + K, CMP); memset (C, 0 , Sizeof (C )); Long Long Ans = 0 ; For ( Int I =1 ; I <= K; I ++ ) {Ans + = Sum (M )- Sum (node [I]. B); add (node [I]. B, 1 );} Printf ( " Test Case % d: % i64d \ n " , Icase, ANS );} Return 0 ;}