Japan
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25489   Accepted: 6907

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

 
 
 
解析:树状数组(单点更新,区间查询)。把道路进行排序(按照w从小到大排序,w相同则按e从小到大排序),这样,对于道路ri,若ej>ei(0≤j<i),则ri、rj两条道路存在交点。通过树状数组,我们可以知道道路ri前面有多少条道路的e小于等于ei,再用下标减去它即可得到ri前面道路的e大于ei的条数,总和就是结果。
 
 
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) (x)&(-x)
#define ll long long
using namespace std; struct Road{
int w, e;
bool operator < (const Road& b)const
{
if(w != b.w) return w<b.w;
return e<b.e;
}
}r[1000005]; int c[1005];
int n, m, k; void add(int x, int val)
{
for(int i = x; i <= 1000; i += lowbit(i))
c[i] += val;
} int sum(int x)
{
int ret = 0;
for(int i = x; i > 0; i -= lowbit(i))
ret += c[i];
return ret;
} void solve()
{
memset(c, 0, sizeof(c));
sort(r, r+k);
ll res = 0;
for(int i = 0; i < k; ++i){
res += i-sum(r[i].e);
add(r[i].e, 1);
}
printf("%I64d\n", res);
} int main()
{
int t, cn = 0;
scanf("%d", &t);
while(t--){
scanf("%d%d%d", &n, &m, &k);
for(int i = 0; i < k; ++i)
scanf("%d%d", &r[i].w, &r[i].e);
printf("Test case %d: ", ++cn);
solve();
}
return 0;
}

  

POJ 3067 Japan的更多相关文章

  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(树状数组求逆序数)

    链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...

  4. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...

  5. POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]

    Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...

  6. 题解报告:poj 3067 Japan(典型BIT)

    Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for th ...

  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. 如何使用工具进行线上 PHP 性能追踪及分析?

    工作了一两年的 PHPer 大概都多多少少知道一些性能分析的工具,比如 Xdebug.xhprof.New Relic .OneAPM.使用基于 Xdebug 进行 PHP 的性能分析,对于本地开发环 ...

  2. hdu 4324 Triangle LOVE(拓扑排序,基础)

    题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...

  3. android 使用sqlite的一些注意事项

    ①在Activity里创建SQLiteOpenHelper对象时,不要在成员变量里面传入context参数,而要在onCreate里面创建这个SQLiteOpenHelper对象.因为如果在成员变量里 ...

  4. YWE复习

    1.怎样使用C#的[状态栏][StatusStrip]控件 http://jingyan.baidu.com/article/851fbc37e7004e3e1f15ab3d.html 2.toolt ...

  5. 关于C#中timer类

    ·关于C#中timer类 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Tim ...

  6. 关于fisher判别的一点理解

    最近一个朋友问这方面的一些问题,其实之前也就很粗略的看了下fisher,真正帮别人解答问题的时候才知道原来自己也有很多东西不懂.下面小结下自己对fisher判别的理解: 其实fisher和PCA差不多 ...

  7. linux软中断与硬中断实现原理概述

    linux软中断与硬中断实现原理概述. 1.软中断通过open_softirq注册一个软中断处理函数,即在软中断向量表softirq_vec数组中添加新的软中断处理action函数. 2.调用rais ...

  8. SRAM与SDRAM的区别

    http://www.cnblogs.com/spartan/archive/2011/05/06/2038747.html SDRAM SDRAM(Synchronous Dynamic Rando ...

  9. VIM Taglist + ctags

    Windows下 进入http://ctags.sourceforge.net/ 下载ctags 把ctags58.zip解压,随便放个地方,我放到了Home\Vim\vim72下,在ctags58文 ...

  10. ubuntu 折腾之路

    aptitude search :search for the lib...and their realtions. apt-get install :install the app apt-get ...