poj 3067 Japan(典型BIT)

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
解题思路:题目的意思就是东海岸与西海岸分别有N和M个城市,现在准备修k条笔直的高速公路(视作k条直线)来连接东西海岸的某两个城市,并且两条直线至多在某处有一个交点,即不会有三条直线相交于一点,因此我们可以保证新加的一条直线和其他直线相交的点都是一个新的交点。假设有直线相连的两个城市编号分别记为(x1,y1),(x2,y2),那么两条直线相交于一点就满足不等式(x1-x2)*(y1-y2)<0。因此,我们将有直线相连的两个城市编号进行排序:先将东部城市编号升序排序,如果东部城市编号相同,则将西部城市编号也按升序排序,考虑到东西海岸的城市编号都是从北到南分别按1~N、1~M的顺序排列的,在此基础上,我们只需对排序后的西部城市编号进行操作:每次连线前都要查看大于当前编号的点有多少个即为新交点的个数。为什么呢?因为在东部前i-1个城市编号一定不大于第i个城市编号,而对应连线的西部第i个城市编号必须找出大于其编号且已连过线的城市编号的个数才为新交点的个数。树状数组怎么实现呢?每新加一个西部城市编号r之前,先统计区间(r,m]中有多少个编号已有连线,即get_sum(m)-get_sum(r),将个数进行累加,然后再对新加的编号标记为1,表示该编号已和东部某个城市相连,这样就可以找出所有交点的个数了。
AC代码:
 1 #include<iostream>
 2 #include<string.h>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn=1e6+5;//k的范围不确定,而一个城市最多关联上1000个城市,所以数组大小要开1e6+5,总之比1e6大一点即可
 8 int lowbit(int x){return x & -x;}
 9 int t,n,m,k,aa[maxn];
10 struct node{int l,r;}nod[maxn];
11 bool cmp(node x,node y){//东部城市编号先升序排列,如果东部城市编号相同,西部城市编号也按升序排列
12     if(x.l!=y.l)return x.l<y.l;
13     else return x.r<y.r;
14 }
15 void update(int x,int val){
16     while(x<=m){
17         aa[x]+=val;
18         x+=lowbit(x);
19     }
20 }
21 int get_sum(int x){
22     int ret=0;
23     while(x>0){
24         ret+=aa[x];
25         x-=lowbit(x);
26     }
27     return ret;
28 }
29 int main(){
30     while(~scanf("%d",&t)){
31         for(int j=1;j<=t;++j){
32             memset(aa,0,sizeof(aa));
33             scanf("%d%d%d",&n,&m,&k);
34             for(int i=1;i<=k;++i)
35                 scanf("%d%d",&nod[i].l,&nod[i].r);
36             sort(nod+1,nod+k+1,cmp);
37             LL ans=0;
38             for(int i=1;i<=k;++i){
39                 ans+=get_sum(m)-get_sum(nod[i].r);//累加西部城市编号比当前城市编号大的个数即为新交点的个数
40                 update(nod[i].r,1);//再标记该城市已和东部某个城市相连为1
41             }
42             printf("Test case %d: %lld\n",j,ans);
43         }
44     }
45     return 0;
46 }

 

posted @ 2018-08-14 13:33  霜雪千年  阅读(242)  评论(0编辑  收藏  举报