Japan poj3067--树状数组

来源:互联网 发布:如何关闭淘宝店铺 编辑:程序博客网 时间:2024/04/29 16:05
Japan
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16732 Accepted: 4488

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

Source

Southeastern Europe 2006
 
题意是求按照输入的方式建路,会有多少个交叉。可以转化为求右边的一列城市的逆序数。首先当时是排序,注意,当左边的城市相同时右边城市应该从小到大排列,这样才不会增加逆序数。求逆序数的方法是用树状数组来做,具体做法,用一个例子来说明:假如输入的数组是{5,2,1,4,3},

其计算逆序数的过程是这么一个过程。

1>,输入5  调用upDate(5, 1),把第5位设置为1

1 2 3 4 5

0 0 0 0 1

计算1-5上比5小的数字存在么?这里用到了树状数组的getSum5 = 1操作,

现在用输入的下标1 - getSum(5) =0就可以得到对于5的逆序数为0

2>. 输入2,调用upDate(2, 1),把第2位设置为1

1 2 3 4 5

0 1 0 0 1

计算1-2上比2小的数字存在么?这里用到了树状数组的getSum2 = 1操作,

现在用输入的下标2 - getSum(2) =1就可以得到对于2的逆序数为1

3>. 输入1,调用upDate(1, 1),把第1位设置为1

1 2 3 4 5

1 1 0 0 1

计算1-1上比1小的数字存在么?这里用到了树状数组的getSum1 = 1操作,

现在用输入的下标 3 - getSum(1) =2就可以得到对于1的逆序数为2

4.> 输入4,调用upDate(4, 1),把第5位设置为1

1 2 3 4 5

1 1 0 1 1

计算1-4上比4小的数字存在么?这里用到了树状数组的getSum4 = 3操作,

现在用输入的下标4 - getSum(4) =1就可以得到对于4的逆序数为1

5>. 输入3,调用upDate(3, 1),把第3位设置为1

1 2 3 4 5

1 1 1 1 1

计算1-3上比3小的数字存在么?这里用到了树状数组的getSum3 = 3操作,

现在用输入的下标5 - getSum(3) =2就可以得到对于3的逆序数为2

6>. 0+1+2+1+2 = 6 这就是最后的逆序数。。

 

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#define MN 1005*1005using namespace std;int c[MN],N,K,M;class A{   public:   int x,y;}a[MN];int lowbit(int x)//计算树状数组中c数组是几个数字的和?{return x & (-x);}long long get_sum(int x)//求得和值{long long sum = 0,i;for(i = x;i > 0;i -= lowbit(i))sum += c[i];return sum;}void update(int x,int value)//更新值{int i;for(i = x;i <= M ;i+=lowbit(i))c[i]+=value;}bool cmp(A a,A b){if(a.x == b.x){return a.y<b.y;}else if(a.x != b.x)return a.x<b.x;}int main(){int T,i,j,k;long long ans;scanf("%d",&T);for( i = 0;i<T;i++){scanf("%d %d %d",&N,&M,&K);memset(a,0,sizeof(a));for(j = 0;j < K;j++){scanf("%d %d",&a[j].x,&a[j].y);}sort(a,a+K,cmp);memset(c,0,sizeof(c));ans = 0;for(j = 0;j < K;j++){update(a[j].y,1);//求逆序数。ans += (get_sum(M)-get_sum(j));}printf("Test case %d: %I64d\n",i+1,ans);}return 0;}
好的,就这些。
 
原创粉丝点击