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. Appium对京东App中WebView的处理

    Appium用uiautomator无法对WebView进行className定位,所以只能模拟动作.可以用android sdk自带的monitor工具,先进行截图,再用任意图像处理软件,获取截图的 ...

  2. UIkit的confirm,好看点

    一,官方推荐的样码. <button type="button" class="uk-button" onclick="UIkit.modal. ...

  3. HttpWebRequest和HttpWebResponse

    原文:http://blog.csdn.net/haitaofeiyang/article/details/18362225 申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice ...

  4. chmod命令详细用法

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  5. 李洪强iOS开发之keychain的使用

    通常情况下,我们用NSUserDefaults存储数据信息,但是对于一些私密信息,比如密码.证书等等,就需要使用更为安全的keychain了.keychain里保存的信息不会因App被删除而丢失,在用 ...

  6. map的详细用法

     map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  7. 银联接口(注意项&备忘)

    1,参考文档“证书下载.导出及上传流程.docx” 按照文档上所述,依次进行,导出的证书备用,用于配置文件的项“const SDK_ENCRYPT_CERT_PATH” 2,使用tp框架 新建一个控制 ...

  8. GC垃圾回收之GC.KeepAlive方法

    http://msdn.microsoft.com/zh-cn/library/system.gc.keepalive.aspx http://www.cnblogs.com/ren700622/ar ...

  9. iOS开发网络篇--NSURLConnection

    S简介 NSURLConnection: 作用: 1.负责发送请求,建立客户端和服务器的连接发送数据给服务器 2.并收集来自服务器的响应数据 步骤: 1.创建一个NSURL对象,设置请求路径 2.传入 ...

  10. parent children

    class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...