题目链接: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. 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

 
 
题目大意:
西岸有N座城市,东岸有M座城市,两岸之间有K条公路,问公路得总交点数。
大概思路:
假设公路连接的西岸城市为X,东岸城市为Y,则当 X_1 < X_2 && Y_2 < Y_1 时两公路相交.
那么问题到这里就差不多解决啦,先按 X 排一个升序,当 X 相等时 按 Y 排升序。排序完成之后,求 Y 序列的逆序数即为交点总数。
注意事项:
①打代码时一定要心平气和
②树状数组是用来求Y的逆序数的所以范围是Y的范围,注意add()函数的边界
③记得初始化
 
AC code(6656K 500ms):
  1. #include <map>
  2. #include <set>
  3. #include <vector>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <algorithm>
  8. #define ll long long int
  9. #define INF 0x3f3f3f3f
  10. using namespace std;
  11.  
  12. const int MAXN = ;
  13. int N, M, K;
  14. int c[MAXN*MAXN];
  15.  
  16. struct node
  17. {
  18. int x, y;
  19. }num[MAXN*MAXN];
  20.  
  21. bool cmp(struct node a, struct node b)
  22. {
  23. if(a.x == b.x)
  24. return a.y <= b.y;
  25. return a.x < b.x;
  26. }
  27.  
  28. int lowbit(int x)
  29. {
  30. return x&(-x);
  31. }
  32.  
  33. void add(int i, int value)
  34. {
  35. while(i <= M)
  36. {
  37. c[i]+=value;
  38. i+=lowbit(i);
  39. }
  40. }
  41.  
  42. ll sum(int i)
  43. {
  44. ll res = ;
  45. while(i > )
  46. {
  47. res+=(ll)c[i];
  48. i-=lowbit(i);
  49. }
  50. return res;
  51. }
  52.  
  53. int main()
  54. {
  55. int T_case = ;
  56. int t = ;
  57. scanf("%d", &T_case);
  58. while(T_case--)
  59. {
  60. t++;
  61. scanf("%d%d%d", &N, &M, &K);
  62. memset(c, , sizeof(c));
  63. for(int i = ; i <= K; i++)
  64. {
  65. scanf("%d%d", &num[i].x, &num[i].y);
  66. }
  67. sort(num+, num+K+, cmp);
  68. ll ans = ;
  69. for(int i = ; i <= K; i++)
  70. {
  71. add(num[i].y, );
  72. ans+=(i-sum(num[i].y));
  73. }
  74. printf("Test case %d: %lld\n", t, ans);
  75. }
  76. return ;
  77. }

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. ansile 命令解释选项

    1, -a MODULE_ARGS --args=MODULE_ARGS 作用传递参数给模块使用 2, --ask-vault-pass 执行时询问vault的密码 3, -B SECONDS --b ...

  2. 牛客网Java刷题知识点之垃圾回收算法过程、哪些内存需要回收、被标记需要清除对象的自我救赎、对象将根据存活的时间被分为:年轻代、年老代(Old Generation)、永久代、垃圾回收器的分类

    不多说,直接上干货! 首先,大家要搞清楚,java里的内存是怎么分配的.详细见 牛客网Java刷题知识点之内存的划分(寄存器.本地方法区.方法区.栈内存和堆内存) 哪些内存需要回收 其实,一般是对堆内 ...

  3. Task的一些用法总结

    一.Task和多线程以及异常的捕获示例代码: static void Main(string[] args) { // 产生CancellationToken的类,该类允许使用Cancel方法终止线程 ...

  4. 浅谈辄止WCF:完成最基本的WCF项目(1)

    Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. WCF的所有服务都会公开契约.契约包 ...

  5. matlab矩阵中如何去掉重复的行;如何找到相同的行,并找到其位置

    找到了2个函数:unique和ismember 1. 去掉其中的重复行:unique 例子: IDX = [,,; ,,; ,,; ,,; ,,; ,,]; classNo = unique(IDX, ...

  6. C++程序设计基础(5)sizeof的使用

    1.知识点 (1)sizeof是一个单目运算发,而不是一个函数,其用于获取操作数所占内存空间的字节数. (2)sizeof的操作数可以使类型名,也可以是表达式,如果是类型名则直接获得该类型所占字节数, ...

  7. MySQL中设置同一张表中一个字段的值等于另一个字段的值

    今天遇到了一个需求,我在一张表中新增了一个字段,因为这张表以前已经有很多数据了,这样对于以前的数据来说,新增的这个字段的值也就是为该字段的默认值,现在需要将新增的这个字段添加上数据,数据来源为同表的另 ...

  8. 虚拟环境--pipenv

    1.安装pipenv,这个工具属于python3 升级pip : pip3 install pipenv 2.在项目中创建虚拟环境 3.激活虚拟环境,进入虚拟环境 进入虚拟环境之前: pipenv s ...

  9. WebGrease—异常来自 HRESULT:0x80131040

    一.错误源: 未能加载文件或程序集“WebGrease, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一 ...

  10. 一分钟学会Git操作流程

    今天整理下公司操作git 流程,尽量用最最简洁的方式整理出来,方便以后有新来的同事学习使用. 我整理的这个Git操作,基本上只需要一分钟,就可以轻松上手啦!!! 一. 拉取提交操作 1.拉取远程代码 ...