POJ 3067

Time Limit: 1000MS Memory Limit: 65536K

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

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

Sample Output

Test case 1: 5

题意:

左边一排 1 ~ n 的城市,右边一排 1 ~ m 的城市,都从上到下依次对应。接着给你一些城市对,表示城市这两个城市相连;

最后问你一共有多少个交叉,其中处于城市处的交叉不算,并且每个位置最多只能有一个交叉。

题解:

抽象来说就是求逆序对的问题,虽然放在树状数组的章节里,但不一定要用树状数组,归并排序也可以……

还有关于k的范围……起点最多1000个,终点最多1000个,那么连线最多显然是1000*1000个啊……哪里坑了……

#include<cstdio>
#include<algorithm>
#define MAX 1000*1000+3
using namespace std;
struct Road{
    int u,v;
}road[MAX],tmp[MAX];
int n,m,k;
long long cnt;
bool cmp(Road a,Road b)
{
    if(a.u==b.u) return a.v<b.v;
    else return a.u<b.u;
}
void Merge(int l,int m,int r)
{
    int i = l;
    int j = m + 1;
    int k = l;
    while(i <= m && j <= r)
    {
        if(road[i].v > road[j].v)
        {
            tmp[k++] = road[j++];
            cnt += m - i + 1;
        }
        else
        {
            tmp[k++] = road[i++];
        }
    }
    while(i <= m) tmp[k++] = road[i++];
    while(j <= r) tmp[k++] = road[j++];
    for(int i=l;i<=r;i++) road[i] = tmp[i];
}
void Merge_sort(int l,int r)
{
    if(l < r)
    {
        int m = (l + r) >> 1;
        Merge_sort(l,m);
        Merge_sort(m+1,r);
        Merge(l,m,r);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int kase=1;kase<=t;kase++)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=k;i++) scanf("%d%d",&road[i].u,&road[i].v);
        sort(road+1,road+k+1,cmp);
        cnt=0;
        Merge_sort(1,k);
        printf("Test case %d: %I64d
",kase,cnt);
    }
}


不过既然是在BIT这节里的题,那总归也要用BIT做一下……

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define MAXN 1000*1000+3
 4 using namespace std;
 5 int n,m,k;
 6 struct Road{
 7     int u,v;
 8 }road[MAXN];
 9 bool cmp(Road a,Road b)
10 {
11     if(a.u==b.u) return a.v<b.v;
12     else return a.u<b.u;
13 }
14 //BIT - 单点增加,区间查询 - st
15 struct _BIT{
16     int N,C[MAXN];
17     int lowbit(int x){return x&(-x);}
18     void init(int n)//初始化共有n个点
19     {
20         N=n;
21         for(int i=1;i<=N;i++) C[i]=0;
22     }
23     void add(int pos,int val)//在pos点加上val
24     {
25         while(pos<=N)
26         {
27             C[pos]+=val;
28             pos+=lowbit(pos);
29         }
30     }
31     int sum(int pos)//查询1~pos点的和
32     {
33         int ret=0;
34         while(pos>0)
35         {
36             ret+=C[pos];
37             pos-=lowbit(pos);
38         }
39         return ret;
40     }
41 }BIT;
42 //BIT - 单点增加,区间查询 - ed
43 int main()
44 {
45     int t;
46     scanf("%d",&t);
47     for(int kase=1;kase<=t;kase++)
48     {
49         scanf("%d%d%d",&n,&m,&k);
50         for(int i=1;i<=k;i++) scanf("%d%d",&road[i].u,&road[i].v);
51         sort(road+1,road+k+1,cmp);
52 
53         long long cnt=0;
54         BIT.init(m);
55         for(int i=1;i<=k;i++)
56         {
57             BIT.add(road[i].v,1);//road[i].v出现一次,加上
58             cnt+=BIT.sum(m)-BIT.sum(road[i].v);//计算出在目前情况下,有多少个比road[i].v大的数出现过
59         }
60         printf("Test case %d: %I64d
",kase,cnt);
61     }
62 }


当然啦,事实告诉了我们,用BIT不是没有道理的,各方面都比归并排序求逆序对要好一些。

更多树状数组模板请走传送门:树状数组进阶

原文地址:https://www.cnblogs.com/dilthey/p/6804135.html