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. Solidworks drwdot文件如何打开,如何制作Solidworks工程图模板

    1 直接把这个文件拖放进Solidworks窗口   2 文件-保存图纸格式,另存为模板(slddrt文件)   3 搜索"Solidworks工程图如何使用,替换图纸格式模板文件.doc& ...

  2. js中splice()的强大(删除,插入或替换数组的元素)

    1.删除-用于删除元素,两个参数,第一个参数(要删除第一项的位置),第二个参数(要删除的项数) 2.插入-向数组指定位置插入任意项元素.三个参数,第一个参数(其实位置),第二个参数(0),第三个参数( ...

  3. 【Mongodb教程 第九课 】MongoDB 删除文档

    remove() 方法 MongoDB的 remove() 方法用于从集合中删除文档.remove() 方法接受两个参数.第一个是删除criteria ,第二是justOne标志: deletion ...

  4. Windows下的Jupyter Notebook 的介绍(写给新手)(图文详解)

    不多说,直接上干货! Windows下的Python 3.6.1的下载与安装(适合32bits和64bits)(图文详解) Windows下的Jupyter Notebook 安装与自定义启动(图文详 ...

  5. 前端富文本 js 版本

    https://s3.pstatp.com/pgc/v2/resource/tt_ueditor_v3_temple/tt-editor.all.js?20180425

  6. MVC中从Controller像View层传值

    MVC中的Controller不能直接的訪问View层中的控件,那么是怎样的将Controller中值传到View中,经常使用的有4种 ViewData: 是获取或设置视图的字典对象,它里面存放的是键 ...

  7. Being a Hero (hdu 3251 最小割 好题)

    Being a Hero Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. js常用操作事件

    触发描述 方法 用法 点击 onclick="method();"   变换 onchange="testChange();"   双击 ondblclick= ...

  9. 关于mysqld_safe

    昨天花了一天时间写了mysql的源码安装,比较蛋疼.其中对于mysqld_safe尤其不理解,因为使用apt-get安装几乎中间不需要什么配置,只需要service mysql start即可,但是源 ...

  10. AngularJS 指令实践指南(二)

    这个系列教程的第一部分给出了AngularJS指令的基本概述,在文章的最后我们介绍了如何隔离一个指令的scope.第二部分将承接上一篇继续介绍.首先,我们会看到在使用隔离scope的情况下,如何从指令 ...