POJ 3067 Japan

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

1 3 4 4 1 4 2 3 3 2 3 1
 

Sample Output

Test case 1: 5
 

Source
PKU
树状数组求逆序数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
    int x;
    int y;
}TA;
TA ta[1000003];
int n,m;
__int64 c[1003];
int cmp(const void *a,const void *b)
{
    if((*(TA*)a).x==(*(TA*)b).x)
       return (*(TA*)a).y-(*(TA*)b).y;
     return (*(TA*)a).x-(*(TA*)b).x;
}
int lb(int x)//又是这样的树状数组模板
{
    return x&(-x);
}
void updata(int i)
{
  for(;i<=m;i+=lb(i))
     c[i]+=1;
}
__int64 rs(int i)
{
    __int64 sum=0;
  for(;i>0;i-=lb(i))
     sum+=c[i];
    return sum;
}
int main()
{
   int i,t,k,nu=1;
   __int64 sum;//这里要注意,会超出int
   scanf("%d",&t);
   while(t--)
   {
     memset(c,0,sizeof(c));
     sum=0;
     scanf("%d%d%d",&n,&m,&k);
     for(i=0;i<k;i++)
      scanf("%d%d",&ta[i].x,&ta[i].y);
     qsort(ta,k,sizeof(ta[0]),cmp);
      for(i=0;i<k;i++)
       {
           sum+=i-rs(ta[i].y);
           updata(ta[i].y);
 
       }
     printf("Test case %d: %I64d\n",nu++,sum);
 
   }
    return 0;
}
                                                                      ------江财小子
原文地址:https://www.cnblogs.com/372465774y/p/2421702.html