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. hdu 5303 Delicious Apples

    这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...

  2. 【Mongodb教程 第六课 】MongoDB 插入文档

    insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的  insert() 或 save() 方法. 语法 insert() 命令的基本语法如下: >db.CO ...

  3. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  4. Notepad++ 两个格式化插件

    格式化HTML--Tidy2 本来都可以通过Notepad++中的“插件>Plugin Manager>Show Plugin Manager>Tidy2” 这种方式来安装,不过内地 ...

  5. [IT学习]GIT 学习

    最近开始了解GIT.有一些不错的资源记录在下面: 1.GIT for teams A book about teams cooperation. 2.GIT https://learngitbranc ...

  6. Chapter1-data access reloaded:Entity Framework(下)

    1.4 Delving deep into object/relational differences 深入挖掘对象关系的不同 理解面向对象和关系世界的不同是重要的,因为他会影响你设计一个对象模型或者 ...

  7. xcode10的那些事

    前言 这里主要介绍一下Xcode10 版本主要更新的内容.随着iOS12的发布,Xcode10已经可以从Mac App Store下载.Xcode10包含了iOS12.watchOS 5.macOS1 ...

  8. taskTracker和jobTracker 启动失败

    2011-01-05 12:44:42,144 ERROR org.apache.hadoop.mapred.TaskTracker: Can not start task tracker becau ...

  9. C++ 函数部分(1)

    1.编写一个求X的n次方的函数 .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas ...

  10. ABAP JSON

    1 DATA: lv_em TYPE string, lv_em1 TYPE string. DATA: lt_but021 TYPE TABLE OF but021, lt_but0211 TYPE ...