H - Japan

来源:互联网 发布:网络论坛 编辑:程序博客网 时间:2024/05/01 17:16

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

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
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#define maxn 1010using namespace std;#define in __int64int n,m,k;in a[maxn];struct node {in e,w;}ww[maxn*maxn];in cmp(node a,node b){if(a.e==b.e)return a.w > b.w;return a.e>b.e;}int lowbit(int x)  //求出二进制中末尾第一个不为0的数的位置k,x+=2^k. {        return  x&(-x);  //return x&(x^(x-1));}void insert(int p)   //构建树状数组 {        while(p<=n)        {                a[p]+=1;                p+=lowbit(p);        }}in sum(int end)  //求1到end区间的和 {        in s=0;        while(end>0)        {                s+=a[end];                end-=lowbit(end);        }        return s;}int main(){int t;int V=0;int i;scanf("%d",&t);while(t--){V++;in ans=0;memset(a,0,sizeof(a));scanf("%d%d%d",&n,&m,&k);for(i=1;i<=k;i++)  scanf("%I64d %I64d",&ww[i].e,&ww[i].w);sort(ww+1,ww+k+1,cmp);for(i=1;i<=k;i++){insert(ww[i].w);ans+=sum(ww[i].w-1);}printf("Test case %d: %I64d\n",V,ans);}return 0;}



原创粉丝点击