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
  1. #include"iostream"
  2. #include"algorithm"
  3. #include"cstring"
  4. #include"cstdio"
  5. using namespace std;
  6. structxy
  7. {
  8. int x,y;
  9. }a[1000010];
  10. int c[1005];
  11. //long long int max;
  12. int cmp(const xy&a,const xy&b)
  13. {
  14. if(a.x!=b.x)
  15. return a.x<b.x;
  16. else
  17. return a.y<b.y;
  18. }
  19. int lowbit(int x)
  20. {
  21. return x&(-x);
  22. }
  23. void updata(int x,int d,int max)
  24. {
  25. while(x<=max)
  26. {
  27. c[x]+=d;
  28. x+=lowbit(x);
  29. }
  30. }
  31. long long int getsum(int x)
  32. {
  33. long long int res=0;
  34. while(x>0)
  35. {
  36. res+=c[x];
  37. x-=lowbit(x);
  38. }
  39. return res;
  40. }
  41. int main()
  42. {
  43. int i,t,p=0;
  44. scanf("%d",&t);
  45. while(t--)
  46. {
  47. int n,m,k,max;
  48. memset(c,0,sizeof(c));
  49. max=0;
  50. scanf("%d%d%d",&n,&m,&k);
  51. for(i=0;i<k;i++)
  52. {
  53. scanf("%d%d",&a[i].x,&a[i].y);
  54. if(a[i].y>max)
  55. max=a[i].y;
  56. }
  57. sort(a,a+k,cmp);
  58. long long int sum=0;
  59. updata(a[0].y,1,max);
  60. for(i=1;i<k;i++)
  61. {
  62. sum+=getsum(max)-getsum(a[i].y);
  63. updata(a[i].y,1,max);
  64. }
  65. printf("Test case %d: %lld\n",++p,sum);
  66. }
  67. return 0;
  68. }

Japan的更多相关文章

  1. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...

  2. POJ 3067 Japan

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

  3. cdoj 383 japan 树状数组

    Japan Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/383 Descrip ...

  4. Day 3 @ RSA Conference Asia Pacific & Japan 2016 (morning)

    09.00 – 09.45 hrs Tracks Cloud, Mobile, & IoT Security    A New Security Paradigm for IoT (Inter ...

  5. Day 4 @ RSA Conference Asia Pacific & Japan 2016

    09.00 – 09.45 hrs Advanced Malware and the Cloud: The New Concept of 'Attack Fan-out' Krishna Naraya ...

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

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

  7. poj3067 Japan(树状数组)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...

  8. Japan POJ - 3067 转化思维 转化为求逆序对

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

  9. cdojR - Japan

    地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: R - Japan Time Limit: 3000/1000MS (Java/Others)     ...

随机推荐

  1. Hadoop概念学习系列之hadoop生态系统闲谈(二十五)

    分层次讲解 最底层平台 ------->hdfs  yarn  mapreduce spark 应用层-------->hbase  hive   pig   sparkSQL    nu ...

  2. mysql show processlist 命令详解

    命令格式 SHOW [FULL] PROCESSLIST SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPE ...

  3. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  4. GC:垃圾回收器简介

    Java堆内存被划分为新生代和年老代两部分,新生代主要使用复制和标记-清除垃圾回收算法,年老代主要使用标记-整理垃圾回收算法,因此java虚拟中针对新生代和年老代分别提供了多种不同的垃圾收集器,JDK ...

  5. jQuery实现等比例缩放大图片让大图片自适应页面布局

    通常我们处理缩略图是使用后台代码(PHP..net.Java等)根据大图片生成一定尺寸的缩略图,来供前台页面调用,当然也有使用前台javascript脚本将加载后的大图强行缩放,变成所谓的缩略图,这种 ...

  6. input标签写CSS时需要注意的几点(先收藏)

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-05) 飞鱼的声纳顶部的搜索框让我头疼了很长时间,原因是总不能获得跨浏览器的统一样式.主要的问题有这么两个:一是inpu ...

  7. Linux device tree 简要笔记

    第一.DTS简介     在嵌入式设备上,可能有不同的主板---它们之间差异表现在主板资源不尽相同,比如I2C.SPI.GPIO等接口定义有差别,或者是Timer不同,等等.于是这就产生了BSP的一个 ...

  8. Winform学习手册(目录)

    一.基础: WINFORM学习笔记——创建Winform项目 WINFORM学习手册——TextBox.Lable.Button WINFORM学习笔记——窗体生命周期 WINFORM学习手册——对话 ...

  9. C++学习笔记之运算符重载

    一.运算符重载基本知识 在前面的一篇博文 C++学习笔记之模板(1)——从函数重载到函数模板 中,介绍了函数重载的概念,定义及用法,函数重载(也被称之为函数多态)就是使用户能够定义多个名称相同但特征标 ...

  10. 【M29】引用计数

    1.引用计数这项技术,是为了让等值对象对象共享同一实体.此技术的发展有两个动机:a.记录堆上分配的对象,是垃圾回收机制的简单原理:b.节省内存,多个对象具有相同的值,存储多次很笨.速度更快,等值对象避 ...