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. [Unit Testing] Mock an HTTP request using Nock while unit testing

    When testing functions that make HTTP requests, it's not preferable for those requests to actually r ...

  2. AspNetPager真假分页对照实例

    从開始学习BS已经有一段时间了. 对于BS的设计,都是进行的网页设计,当中包含从数据库中取出来的数据.显示在页面上.曾经在CS中,都是使用GridView等表格控件进行显示,因为数据小.并且右側又有滚 ...

  3. strtok函数

    strtok函数是cstring文件里的函数 strtok函数是cstring文件里的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...

  4. React通过Ajax获取数据

    React 组件的数据可以通过 componentDidMount 方法中的 Ajax 来获取,当从服务端获取数据库可以将数据存储在 state 中,再用 this.setState 方法重新渲染 U ...

  5. 在CentOS上把PHP从5.4升级到5.5

    在CentOS上把PHP从5.4升级到5.5 摘要:本文记录了在CentOS 6.3上,把PHP从5.4.8升级到5.5.13的过程. 1. 概述 在我做的一个项目中,最近我对生产服务器上的一系列系统 ...

  6. 在CentOS上把Nginx从1.2.4升级到1.6.0

    在CentOS上升级把Nginx从1.2.4升级到1.6.0 摘要:本文记录了在CentOS 6.3上,把Nginx从1.2.4升级到1.6.0的过程. 1. 概述 在我做的一个项目中,最近我对生产服 ...

  7. hdoj-2090-算菜价(水题)

    算菜价 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. 2016/05/06 Sublime Text 3 常用插件以及安装方法(转)

    http://www.cnsecer.com/460.html 安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧, ...

  9. java语法基础(一)

    这个是自己前两年做java视频教程时候的课件.感兴趣的同学可以参考下. 这里是纯粹的语法行总结. editplus的使用 选择项目目录 打开editplus 左侧目录导航栏 可以打开盘符,文件夹 可以 ...

  10. 使用buildroot搭建linux文件系统【转】

    本文转载自:http://blog.csdn.net/metalseed/article/details/45423061 (文件系统搭建,强烈建议直接用buildroot,官网上有使用教程非常详细b ...