POJ 3067 Japan

Source: Internet
Author: User

Japan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25489 Accepted: 6907

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, ... 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 file starts with t-the number of test cases. Each test case is starts with three numbers–n, M, K. Each of the next K lines contains, 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 are the number of the city of the West coast.

Output

For each test case, write one line in the standard output:
Test Case Number: (Number of crossings)

Sample Input

13 4 41 42 33 23 1

Sample Output

Test Case 1:5

Source

Southeastern Europe 2006 Parse: Tree-like Array (single point update, interval query). The road is sorted (by W from small to large sort, w is the same as e from small to large sort), so, for road ri, if Ej>ei (0≤j<i), RI, RJ Two roads there are intersections. With a tree array, we can see how many roads in front of the road ri are less than or equal to EI, and then subtract it to get the number of the e greater than EI for the road ahead of the RI, and the sum is the result.
#include <cstdio> #include <cstring> #include <algorithm> #define LOWBIT (x) (x) & (-X) #define LL    Long longusing namespace Std;struct road{int W, E;        BOOL operator < (const road& b) Const {if (w! = B.W) return w<b.w;    Return e<b.e;  }}r[1000005];int c[1005];int N, M, k;void Add (int x, int val) {for (int i = x; I <=, i + = Lowbit (i)) c[i] + = val;}    int sum (int x) {int ret = 0;    for (int i = x; i > 0; I-= Lowbit (i)) ret + = C[i]; return ret;}    void Solve () {memset (c, 0, sizeof (c));    Sort (R, R+k);    ll res = 0;        for (int i = 0; i < K; ++i) {res + = I-sum (R[I].E);    Add (r[i].e, 1); } printf ("%i64d\n", res);}    int main () {int T, CN = 0;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n, &m, &k);        for (int i = 0; i < K; ++i) scanf ("%d%d", &AMP;R[I].W, &AMP;R[I].E);        printf ("Test Case%d:", ++CN);    Solve (); } return 0;} 

  

POJ 3067 Japan

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.