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. 1
  2. 3 4 4
  3. 1 4
  4. 2 3
  5. 3 2
  6. 3 1

Sample Output

  1. Test case 1: 5

Source

 
 
 
解析:树状数组(单点更新,区间查询)。把道路进行排序(按照w从小到大排序,w相同则按e从小到大排序),这样,对于道路ri,若ej>ei(0≤j<i),则ri、rj两条道路存在交点。通过树状数组,我们可以知道道路ri前面有多少条道路的e小于等于ei,再用下标减去它即可得到ri前面道路的e大于ei的条数,总和就是结果。
 
 
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define lowbit(x) (x)&(-x)
  5. #define ll long long
  6. using namespace std;
  7.  
  8. struct Road{
  9. int w, e;
  10. bool operator < (const Road& b)const
  11. {
  12. if(w != b.w) return w<b.w;
  13. return e<b.e;
  14. }
  15. }r[1000005];
  16.  
  17. int c[1005];
  18. int n, m, k;
  19.  
  20. void add(int x, int val)
  21. {
  22. for(int i = x; i <= 1000; i += lowbit(i))
  23. c[i] += val;
  24. }
  25.  
  26. int sum(int x)
  27. {
  28. int ret = 0;
  29. for(int i = x; i > 0; i -= lowbit(i))
  30. ret += c[i];
  31. return ret;
  32. }
  33.  
  34. void solve()
  35. {
  36. memset(c, 0, sizeof(c));
  37. sort(r, r+k);
  38. ll res = 0;
  39. for(int i = 0; i < k; ++i){
  40. res += i-sum(r[i].e);
  41. add(r[i].e, 1);
  42. }
  43. printf("%I64d\n", res);
  44. }
  45.  
  46. int main()
  47. {
  48. int t, cn = 0;
  49. scanf("%d", &t);
  50. while(t--){
  51. scanf("%d%d%d", &n, &m, &k);
  52. for(int i = 0; i < k; ++i)
  53. scanf("%d%d", &r[i].w, &r[i].e);
  54. printf("Test case %d: ", ++cn);
  55. solve();
  56. }
  57. return 0;
  58. }

  

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. .NET基础篇——Entity Framework 数据转换层通用类

    在实现基础的三层开发的时候,大家时常会在数据层对每个实体进行CRUD的操作,其中存在相当多的重复代码.为了减少重复代码的出现,通常都会定义一个共用类,实现相似的操作,下面为大家介绍一下Entity F ...

  2. JsRender系列demo(2)多模板-template

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. php获取数组长度的方法(有实例)

    php获取数组长度的方法,php为我们提供了两个函数可以计算一维数组长度,如count,sizeof都可以直接统计数组长度,还有获取二维数组的方法. 在php中获取数组长度方法很简单,php为我们提供 ...

  4. 序列dp笔记√

    例题: http://www.cnblogs.com/gc812/p/5792181.html http://www.cnblogs.com/gc812/p/5792217.html

  5. lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素

    题目: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个.   您在真实的面试中是否遇到过这个题? 样例 给出1->1->2->null,返回 1-& ...

  6. qt外部数据传入实现动态的折线图绘制

    在嵌入式开发中,实现数据收集与显示很常见,对于希望数据稳定的应用来说,               折现图的表现形式很符合条件.               本实现是通过qt的signal-slot来 ...

  7. PowerDesigner模型设计

    原文:PowerDesigner模型设计 绪论 Sybase PowerDesigner(简称PD)是最强大的数据库建模工具,市场占有率第一,功能也确实十分强大,现在最新版本是15.1,已经支持最新的 ...

  8. Zookeeper核心机制

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) Zookeeper是Hado ...

  9. Redis安装教程

    1. Linux下Redis安装教程 (1)安装 #tar xf redis-2.6.14.tar.gz #cd redis-2.6.14 #make #make install (2)配置 修改re ...

  10. C/C++内存存储

    #include <stdio.h> #include "string.h" #include "malloc.h" void Swap(int a ...