题目链接: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. 【3dsMax安装失败,如何卸载、安装3dMax 2010?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  2. LeetCode 200.岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  3. 牛客网Java刷题知识点之ArrayList 、LinkedList 、Vector 的底层实现和区别

    不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...

  4. Mysql系列-字符集

    字符集 怎样选择合适的字符集 如果应用程序需要发布到很多国家和地区,需要支持各种各样的文字,则选择Unicode编码,Mysql中即UTF-8.q如果需要将数据导入数据库,这时候要注意数据库字符集对数 ...

  5. Linux下安装配置MongoDB数据库

    说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.130 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/s ...

  6. 4、加载:Loading

    /* ---html----*/ <ion-content> <button (click)="manual()">手动关闭</button> ...

  7. Investigating issues with Unmanaged Memory. First steps. (转载)

    原文:http://kate-butenko.blogspot.tw/2012/07/investigating-issues-with-unmanaged.html I want to write ...

  8. Csharp: TreeView 初始化设置默认选择节点

    /// <summary> /// 设置查找的节点为选定节点 /// 涂聚文 /// 2013-07-15 /// </summary> /// <param name= ...

  9. scss-嵌套规则

    在编写css代码的时候,可能由于嵌套的原因,需要多次重复书写选择器. 代码如下: #content article h1 { color: #333 } #content article p { ma ...

  10. CSS设计模式之三权分立模式篇 ( 转)

    转自 海玉的博客 市面上我们常常会看到各种各样的设计模式书籍,Java设计模式.C#设计模式.Ruby设计模式等等.在众多的语言设计模式中我唯独找不到关于CSS设计模式的资料,即使在网上找到类似内容, ...