题目链接: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. Java中的时间处理

    日期时间组件使用 java.util.Date:实现类,其对象具有时间.日期组件.java.util.Calendar:抽象类,其对象具有时间.日期组件.java.sql.Date:实现类,其对象具有 ...

  2. Root用户让其他用户运行某程序

    这里以启动tomcat为例 1.安装tomcat不介绍了,自己百度 2.测试能否使用,略 3.创建tomcat用户 useradd tomcat -s /sbin/nologin 创建tomcat,禁 ...

  3. VS2010中VC++目录和C/C++之间的区别。VC++ Directories和C/C++的区别。

    首先,这是个历史遗留问题,说起来比较复杂.其次,这个问题在微软的MSDN博客上已经专门被说起过了,英文好的请直接移步到原文:<VC++ Directories>.另外,stack over ...

  4. pat1015. Reversible Primes (20)

    1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...

  5. 深入redis内部之redis启动过程之二

    接上文,继续分析代码 1. 设置线程安全模式 zmalloc_enable_thread_safeness();/*设置线程安全标识符为1*/ void zmalloc_enable_thread_s ...

  6. Java入门系列-11-类和对象

    这篇文章为你搞懂类和对象的使用 对象:用来描述客观事物的实体,由一组属性和方法组成,万物皆对象. 属性:就是对象的特征,像身高.体重.颜色 方法:对象的行为,如跑.跳 类:类是模子,定义对象将会拥有的 ...

  7. nyoj1032——Save Princess——————【set应用】

    Save Princess 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Yesterday, the princess was kidnapped by a de ...

  8. react native 完美解决启动白屏

    先讲下我的RN版本0.58.5 首先安装react-native-splash-screen(目前使用的版本是3.2.0) 项目地址https://github.com/crazycodeboy/re ...

  9. OpenLayers 3 实现轨迹回放

    function PathBack() { var PVLayer = new ol.layer.Vector({ source: new ol.source.Vector({}) }); var p ...

  10. OLEDB 枚举数据源

    在之前的程序中,可以看到有这样一个功能,弹出一个对话框让用户选择需要连接的数据源,并输入用户名和密码,最后连接:而且在一些数据库管理软件中也提供这种功能--能够自己枚举出系统中存在的数据源,同时还可以 ...