Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26270   Accepted: 7132

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

 
设每条公路连接左边城市x和右边城市y,按第一关键字x升序,第二关键字y升序排列后,求逆序对即可。
 
 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
struct edge{
int x,y;
}e[mxn*];
int cmp(edge a,edge b){
if(a.x!=b.x)return a.x<b.x;
return a.y<=b.y;
}
long long t[mxn];
int n,m,k;
int a[mxn];
inline int lowbit(int x){
return x&-x;
}
void add(int p,int v){
while(p<=m){
t[p]+=v;
p+=lowbit(p);
}
return;
}
int sum(int p){
int res=;
while(p){
res+=t[p];
p-=lowbit(p);
}
return res;
}
int main(){
int T;
scanf("%d",&T);
int i,j;
int cas=;
while(T--){
long long ans=;
memset(t,,sizeof t);
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=k;i++) scanf("%d%d",&e[i].x,&e[i].y);
sort(e+,e+k+,cmp);
for(i=;i<=k;i++){
ans+=sum(m)-sum(e[i].y);
add(e[i].y,);
}
printf("Test case %d: %lld\n",++cas,ans);
}
return ;
}

POJ3067 Japan的更多相关文章

  1. poj3067 Japan(树状数组)

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

  2. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  3. POJ3067:Japan(线段树)

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

  4. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...

  5. Japan

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

  6. POJ 3067 Japan

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

  7. cdoj 383 japan 树状数组

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. 采用maven 对tomcat 进行自动部署

    在工作过程中经常会遇到项目频繁发不到额过程,而且在这个过程中会一直进行一些简单但是繁琐的重复性工程 1.打war 包 2.停掉tomcat 3.copy war 包 4.启动tomcat 听说mave ...

  2. Spring XML配置文件无法自动提示 eclipse中XML配置文件open with打开方式选择 XML Editor:注意它的编辑方式也是有两种的design和source

    双击XML配置文件,如果打开方式不正确 则如下图: 都是灰色显示,不会有自动提示,也不会有颜色标注 右击XML配置文件,选择打开方式为XML Editor,则会有颜色标注 如果此时没有自动提示 则要手 ...

  3. cocos2dx for lua 简单的翻牌动画

    local x = 20 local y = display.height/2 for i = 1,16 do--创建16张 local cardFg = display.newSprite(&quo ...

  4. 状态压缩dp 状压dp 详解

    说到状压dp,一般和二进制少不了关系(还常和博弈论结合起来考,这个坑我挖了还没填qwq),二进制是个好东西啊,所以二进制的各种运算是前置知识,不了解的话走下面链接进百度百科 https://baike ...

  5. Spring AOP注解形式简单实现

    实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...

  6. 查看 EGLIBC 版本

    $ ldd --versionldd (Debian EGLIBC 2.13-38+deb7u1) 2.13

  7. 【报错】invalid or unexpected token

    结果发现是把英文的,写成了中文字符,系统没法识别.

  8. JAVA 基础--final 关键字的用法

    在java中,final的含义在不同的场景下有细微的差别,in a word,它指的是“不可变的” 1.修饰数据.这里的可以看到被final修饰的变量,值不能被改变,但是 package FinalT ...

  9. 使用代码生成器“代码工厂”快速生成B/S程序代码

    开发目的: 自动生成C#.HTML.JS.Ajax 代码 .可以节省大量的时间来做业务逻辑的代码,那些重复的代码就不需要....了 环境支持: 硬件环境:window .VS2010+.支持SQLSe ...

  10. 在win2003下apache2.2无法加载php5apache2_4.dll

    昨天在一台win2003的服务器上配置apache和PHP, 我在apache的配置文件httpd.conf里添加 LoadModule php5_module "D:/php-5.4.23 ...