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代码:
 #include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e6+;//k的范围不确定,而一个城市最多关联上1000个城市,所以数组大小要开1e6+5,总之比1e6大一点即可
int lowbit(int x){return x & -x;}
int t,n,m,k,aa[maxn];
struct node{int l,r;}nod[maxn];
bool cmp(node x,node y){//东部城市编号先升序排列,如果东部城市编号相同,西部城市编号也按升序排列
if(x.l!=y.l)return x.l<y.l;
else return x.r<y.r;
}
void update(int x,int val){
while(x<=m){
aa[x]+=val;
x+=lowbit(x);
}
}
int get_sum(int x){
int ret=;
while(x>){
ret+=aa[x];
x-=lowbit(x);
}
return ret;
}
int main(){
while(~scanf("%d",&t)){
for(int j=;j<=t;++j){
memset(aa,,sizeof(aa));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;++i)
scanf("%d%d",&nod[i].l,&nod[i].r);
sort(nod+,nod+k+,cmp);
LL ans=;
for(int i=;i<=k;++i){
ans+=get_sum(m)-get_sum(nod[i].r);//累加西部城市编号比当前城市编号大的个数即为新交点的个数
update(nod[i].r,);//再标记该城市已和东部某个城市相连为1
}
printf("Test case %d: %lld\n",j,ans);
}
}
return ;
}

题解报告:poj 3067 Japan(典型BIT)的更多相关文章

  1. POJ 3067 Japan 【树状数组经典】

    题目链接:POJ 3067 Japan Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32076   Accep ...

  2. POJ 3067 Japan (树状数组求逆序对)

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

  3. POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]

    Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...

  4. poj 3067 Japan(树状数组求逆序数)

    链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...

  5. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...

  6. POJ 3067 Japan

    Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25489   Accepted: 6907 Descriptio ...

  7. poj 3067 - Japan(树状数组)

    先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...

  8. POJ 3067 Japan(经典树状数组)

    基础一维树状数组  题意:左边一排 1-n 的城市,右边一排 1-m 的城市,都从上到下依次对应.接着给你一些城市对,表示城市这两个城市相连,最后问你一共有多少个交叉,其中处于城市处的交叉不算并且每个 ...

  9. poj 3067 Japan(线段树?,神奇卡时代码,暂未完)

    题目 //暴力的,没什么算法的,被琪琪视为傻逼的代码: //照者学长的神奇幸运卡时代码,虽然能AC,但是中途wa,tle了那么多次,啥也不想说了 //学长威武,能想出sum必须要是—— __int64 ...

随机推荐

  1. Meteor教程

    Meteor 是一个构建在 Node.js 之上的平台,用来开发实时网页程序.Meteor 程序位于数据库和用户界面之间,保持二者之间的数据同步更新. 因为 Meteor 是基于 Node.js 开发 ...

  2. Error Code: 2006 - MySQL 鏈嶅姟鍣ㄥ凡绂荤嚎

    将sql文件导入到mysql时候,就一直报这个错误. 我试过网上各种方法都行不通. 最后将以下一句运行了一下就能够了,并且没有重新启动mysql. SET GLOBAL max_allowed_pac ...

  3. cf246 ENew Reform (并查集找环)

    Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each ...

  4. Eclipse:Some sites could not be found. See the error log for more detail.解决的方法

    今天遇到了一个奇葩的问题.我把我的sdk tools的版本号升级到23后.我在eclipse中尝试升级ADT,发现了这么一个问题,以下分析下原因: 当我在eclipse中选择Help-->Che ...

  5. 3.2.1 配置构建Angular应用——简单的笔记存储应用——展示功能

    本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...

  6. 如何用css给博客换一个好看的样式

    第一步:点击设置,将如下代码复制到页面定制css代码 h3 { color: #fff; background-color: #008eb7; -moz-border-radius: 3px; bor ...

  7. 一个DIV相对于另一个DIV定位

    <div style="position:relative"><div style="position:absolute; top:0px; left: ...

  8. c/c++内存使用原则

    1 no malloc no free 2 no new no delete 如果对象不是new出来的,那么这个对象在生命周期结束后会自动调用析构函数自己释放自己的内存,不需要delete. 但是如果 ...

  9. 城域网IPv6过渡技术—NAT64+DNS64 Test for IPv6 DNS64/NAT64 Compatibility Regularly

    城域网IPv6过渡技术—NAT64+DNS64 - 51CTO.COM http://network.51cto.com/art/201311/419623.htm Supporting IPv6 D ...

  10. FineReport实现java报表统计图表的效果图

    Java报表-ERP图表联动 Java报表-多维坐标轴图 Java报表-静态图表 Java报表-时间坐标轴 Java报表-图表报表动态交互 Java报表-图表热点链接 Java报表-图表缩放 Java ...