题目链接:POJ 3067 Japan

Japan

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32076   Accepted: 8620

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

Source

 
 
题目大意:
西岸有N座城市,东岸有M座城市,两岸之间有K条公路,问公路得总交点数。
大概思路:
假设公路连接的西岸城市为X,东岸城市为Y,则当 X_1 < X_2 && Y_2 < Y_1 时两公路相交.
那么问题到这里就差不多解决啦,先按 X 排一个升序,当 X 相等时 按 Y 排升序。排序完成之后,求 Y 序列的逆序数即为交点总数。
注意事项:
①打代码时一定要心平气和
②树状数组是用来求Y的逆序数的所以范围是Y的范围,注意add()函数的边界
③记得初始化
 
AC code(6656K 500ms):
 #include <map>
#include <set>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long int
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = ;
int N, M, K;
int c[MAXN*MAXN]; struct node
{
int x, y;
}num[MAXN*MAXN]; bool cmp(struct node a, struct node b)
{
if(a.x == b.x)
return a.y <= b.y;
return a.x < b.x;
} int lowbit(int x)
{
return x&(-x);
} void add(int i, int value)
{
while(i <= M)
{
c[i]+=value;
i+=lowbit(i);
}
} ll sum(int i)
{
ll res = ;
while(i > )
{
res+=(ll)c[i];
i-=lowbit(i);
}
return res;
} int main()
{
int T_case = ;
int t = ;
scanf("%d", &T_case);
while(T_case--)
{
t++;
scanf("%d%d%d", &N, &M, &K);
memset(c, , sizeof(c));
for(int i = ; i <= K; i++)
{
scanf("%d%d", &num[i].x, &num[i].y);
}
sort(num+, num+K+, cmp);
ll ans = ;
for(int i = ; i <= K; i++)
{
add(num[i].y, );
ans+=(i-sum(num[i].y));
}
printf("Test case %d: %lld\n", t, ans);
}
return ;
}

POJ 3067 Japan 【树状数组经典】的更多相关文章

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

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

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

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

  3. POJ 3067 Japan 树状数组求逆序对

    题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些 ...

  4. POJ 3067 Japan (树状数组 && 控制变量)

    题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...

  5. POJ 3067【树状数组】

    题意: 给你两行数字,n个m个,然后给你k条线直接把两个数连起来,问有多少个交叉的 思路: 假定上一行是起点,下一行是终点. 把路按照起点从大到下排序, 然后可以直接对每条路查询,这条路目前的交叉数, ...

  6. poj3067 Japan(树状数组)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...

  7. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  8. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  9. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

随机推荐

  1. Lakeshore

    用来做 html5 特效,Egret游戏引擎 为什么用Egret开发的游戏在某些Android设备上特别卡? { 在 Android 早期版本( 4.4 之前) ,Android WebView 并不 ...

  2. Html练习 | 小影志首页练习

    <!DOCTYPE html> <head> <title>小影志首页练习</title> <style> /*应用全页字体*/ .pg-f ...

  3. zookeeper 编程框架 curator

    Curator框架提供了一套高级的API, 简化了ZooKeeper的操作. 它增加了很多使用ZooKeeper开发的特性,可以处理ZooKeeper集群复杂的连接管理和重试机制. 这些特性包括: 自 ...

  4. lua输入函数名字符串执行函数

    str = "testA()"loadstring(str)() function testA() ------end 使用loadstring即可执行后面在xlua用了下发现不能 ...

  5. Servlet3.0的文件上传功能

    在Servlet3.0之前,文件上传需要借助于第三方插件,在Servlet3.0之后,Servlet本身开始支持文件上传功能. 获取上传的文件可以通过HTTPServletRequest的getPar ...

  6. 《Python编程从入门到实践》_第九章_类

    创建一个简单的类 根据Dog类创建的每个实列都将存储名字和年龄.我们赋予了每条小狗蹲下(sit())和打滚(roll_over())的能力: class Dog(): ""&quo ...

  7. union、except和intersect查询

    1. union联合查询  (合并) select r.room_id from room r union select rp.num from room_type rp 要求表1和表2的查询结果结构 ...

  8. Hadoop-HA(高可用)集群搭建

    Hadoop-HA集群搭建 一.基础准备工作 1.准备好5台Linux系统虚拟服务器或物理服务器 我这里演示采用虚拟服务器搭建Hadoop-HA集群,各自功能分配如下: NameNode节点:vt-s ...

  9. hiveQL随笔

    hiveQL中union all直接用于select 子句

  10. IO流之递归

    递归: 递归,指在当前方法内调用自己的这种现象 public void method(){ System.out.println(“递归的演示”); //在当前方法内调用自己 method(); } ...