UVALive-2926 Japan "tree-like array"

Source: Internet
Author: User

Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must is built for the venue. Japan is Tall island with N cities on the East coast and m cities on the West coast (m ; N 1000). K superhighways would be build. Cities on each coast is numbered 1; 2;::: from north to south. Each superhighway are straight line and connects city in the East coast with city of the West coast. The funding for the construction are guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At the most of the superhighways cross at the one location. Write A program that calculates the number of the crossings between superhighways.

Input

The input Le 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-numbers | The numbers of cities connected by the superhighway. The RST one is the number of the city on the East coast and second one are the number of the city of the West coast.

Output

For each test case, write one line in the standard output:

Test Case <Case number >: < number of crossings >

Sample Input

1

3 4 4

1 4

2 3

3 2

3 1

Sample Output

Test Case 1:5

Ideas:

Test instructions is to find the total number of intersection points, the east of the city for an, the West for BN, then for the two segments a1b1, A2B2, the condition of the intersection is ((A1 < A2) && (B1>B2)) | | ((A1>A2) && (B1<B2)).

In the Matrix, for (A, b) the segment that intersects with it is in the lower left and upper right two sub-matrices (excluding boundaries).

A tree-shaped array can be used to resolve.

Code:

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN = ++5;5 LL MAT[MAXN][MAXN], N, m;6 intA[MAXN*MAXN], b[maxn*MAXN];7 8 intLowbit (intx) {returnX &-x;}9 Ten voidAddintXintYintd) { One      for(inti = x; I <= N; i + =lowbit (i)) A          for(intj = y; J <= M; J + =Lowbit (j)) -MAT[I][J] + =D; - } the  -LL sum (intXinty) { -     if(x <=0)return 0; -     if(Y <=0)return 0; +LL ret =0; -      for(inti = x; i >0; I-=lowbit (i)) +          for(intj = y; J >0; J-=Lowbit (j)) ARET + =Mat[i][j]; at     returnret; - } -  - intMain () { -Ios::sync_with_stdio (false); -     intT, k, Kase =0; inCIN >>T; -      while(Cin >> n >> M >>k) { toMemset (Mat,0,sizeof(MAT)); +          for(inti =0; I < K; ++i) { -CIN >> A[i] >>B[i]; theAdd (A[i], b[i],1); *         } $LL ans =0;Panax Notoginseng          for(inti =0; I < K; ++i) { -Ans + = SUM (n, b[i]-1) +sum (a[i]-1, m)-sum (A[i], b[i]-1)-sum (a[i]-1, B[i]); the         } +cout <<"Test Case"<< ++kase <<": "; Acout << ans/2<<Endl; the     } +  -     return 0; $}

UVALive-2926 Japan "tree-like array"

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.