Poj-3067 Japan (tree array)

Source: Internet
Author: User
Tags ranges

 

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
Japan is a small island country with M regions in the east. Each region number ranges from 1-m to N in the West. Each region number ranges from 1-n. There is a path between them and ask how many intersections there are.
Idea: Tree array;
Note: long is used here, because this wa has been used for many times.
AC code:
 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int d1[2000000],n; 7 struct node 8 { 9     int g,h;10 } t[2000000];11 int cmp(struct node a,struct node b)12 {13     if(a.h==b.h)14         return a.g<b.g;15     return a.h<b.h;16 }17 int lowbit(int x)18 {19     return x&(-x);20 }21 long long sum(int x)22 {23     long long res=0;24     while(x>0)25     {26         res+=d1[x];27         x-=lowbit(x);28     }29     return res;30 }31 void add(int x)32 {33     while(x<=20000)34     {35         d1[x]++;36         x+=lowbit(x);37     }38 }39 int main()40 {41     int a,c,b,i,j,k=0;42     scanf("%d",&a);43     while(a--)44     {45         memset(d1,0,sizeof(d1));46         scanf("%d%d%d",&n,&b,&c);47         for(i=0; i<c; i++)48         {49             scanf("%d%d",&t[i].g,&t[i].h);50         }51         sort(t,t+c,cmp);52         long long ans=0;53         for(i=0; i<c; i++)54         {55             ans+=sum(b)-sum(t[i].g);56             add(t[i].g);57         }58         printf("Test case %d: %lld\n",++k,ans);59     }60 }

 

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.