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. grunt安装失败处理

    1.官网 Grunt官网 http://gruntjs.com 2.前言 前段时间一不小心升级了win10(万恶的360),各种不适应各种问题各种软件bug,最终决定回退到win7,然后悲催的发现系统 ...

  2. 嘿嘿,JAVA里第一次运行单元测试成功,立存

    按书上写的单元测试. 居然一次过,爽!!! package org.smart4j.chapter2.test; import java.util.HashMap; import java.util. ...

  3. 心情记录&考试总结 3.30

    并不知道现在要干什么,本人像是一只大颓狗 Em..怎么说呢,今天考完了一场奇怪的试 准确的说,画风很不正常的试 第一题集体爆零 第二题暴力20分 第三题暴力40分,乱搞有加成 改题的话, 第一题有奇怪 ...

  4. dbgrid显示access备注信息

    procedure TfrmAllFind.DBGrid6DrawColumnCell(Sender: TObject; const Rect: TRect;  DataCol: Integer; C ...

  5. 删除元素 不存在 NO 存在 输出余下元素

    #include<stdio.h> #include<stdlib.h> #define N 5 #define NULL 0 #define OK 1 #define ERR ...

  6. iOS 开发中遇到的问题

    1. 关于纠结很久的KVO崩溃问题,其真正原因是,在删除roomItem的KVO之前,将这个对象已经赋值为nil,所以实际上并没有删除他的observer,因此而崩溃:长时间纠结的原因是受.cxx_d ...

  7. 含有特殊字符的JSON串解析方法

    工具方法:public static <T> T parseJsonString(String json,Class<T> classType){ ObjectMapper m ...

  8. Windows系统上如何使用SSH

    Windows系统上如何使用SSH 传统的网络服务程序如FTP.Telnet等,在网络上一般使用明文传送数据.用户账号和口令信息,容易受到中间人的攻击.用户利用SSH协议后能有效防止DNS及IP欺骗, ...

  9. YASKAWA电机控制(2)---调试

    2015 5 23 基础调试—点动 上次接线由于没有接地,导致外壳带电,非常危险. 由于上次接线端子被弄坏,这次自己重做.由于没有压线钳,只用尖嘴钳把线压近端子,有可能会松动. 接线的时候Lc1.Lc ...

  10. Javascript如何判断一个变量是数字类型?

    isNaN()不能判断一个变量是否为数字类型,isNaN(123)值为false,isNaN('123')值也为false.isNaN() 的实际作用跟它的名字isNaN并不一致,isNaN(NaN) ...