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. Web前端 优化方案

    1.减少Http请求  在一个页面中图片,CSS,JS可能N个,如果每个资源都去请求一次服务器的话,那么服务器就会为每个资源开一个线程来完成,这样的话对服务器的压力就很大了.所以解决的方法就是合并资源 ...

  2. Bootstrap历练实例:带列表组的面板

    带列表组的面板 我们可以在任何面板中包含列表组,通过在 <div> 元素中添加 .panel 和 .panel-default 类来创建面板,并在面板中添加列表组.您可以从 列表组 一章中 ...

  3. C#传递数组参数

    在C#中,可以将数组作为参数传递给方法,同时方法可以更改数组元素的值. 一.将一维数组作为参数传递给方法 using System;using System.Collections.Generic;u ...

  4. Java基础面试操作题:Java代理工厂设计模式 ProxyFactory 有一个Baby类,有Cry行为,Baby可以配一个保姆 但是作为保姆必须遵守保姆协议:能够处理Baby类Cry的行为,如喂奶、哄睡觉。

    package com.swift; public class Baby_Baomu_ProxyFactory_Test { public static void main(String[] args ...

  5. ios 设计模式总结

    设计模式:备注:消息传递模型(Message Passing)是Objective-C语言的核心机制.在Objective-C中,没有方法调用这种说法,只有消息传递.在C++或Java中调用某个类的方 ...

  6. 【细节题 离线 树状数组】luoguP4919 Marisa采蘑菇

    歧义差评:但是和题意理解一样了之后细节依然处理了很久,说明还是水平不够…… 题目描述 Marisa来到了森林之中,看到了一排nn个五颜六色的蘑菇,编号从1-n1−n,这些蘑菇的颜色分别为col[1], ...

  7. shell 练习 - 第七周

    1. 用shell实现传入进程pid, 查看对应进程/proc下CPU.内存指标 #!/bin/bash read -p "Input PID Value: " pid pid_e ...

  8. 设置mysql允许外部连接访问

    错误信息: SQL Error (1130): Host ‘192.168.1.88’ is not allowed to connect to this MySQL server 说明所连接的用户帐 ...

  9. OpenCV编译 Make出错 recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed

    OpenCV编译  Make出错 recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed 添 ...

  10. GoF23种设计模式之行为型模式之备忘录模式

    一.概述         在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象的外部保存这个状态.以便以后可以将该对象恢复到原先保存的状态. 二.适用性 1.当需要保存一个对象在某个时刻的状态( ...