http://poj.org/problem?id=3067

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29474   Accepted: 7950

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

 
 
给坐标排序,求逆序对,手模一下好理解、
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N();
int n,m,k,tr[N];
struct Node
{
int west,east;
}node[N*N];
bool cmp(Node a,Node b)
{
if(a.east==b.east) return a.west>b.west;
return a.east>b.east;
} #define lowbit(x) (x&((~x)+1))
inline void Update(int i,int x)
{
for(;i<=N;i+=lowbit(i)) tr[i]+=x;
}
inline int Query(int x)
{
int ret=;
for(;x;x-=lowbit(x)) ret+=tr[x];
return ret;
} int main()
{
int t; scanf("%d",&t); int h=;
for(long long ans=;h<=t;h++,ans=)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++)
scanf("%d%d",&node[i].west,&node[i].east);
sort(node+,node+k+,cmp);
memset(tr,,sizeof(tr));
for(int i=;i<=k;Update(node[i++].west,))
ans+=(long long)Query(node[i].west-);
printf("Test case %d: %I64d\n",h,ans);
}
return ;
}

POJ——T 3067 Japan的更多相关文章

  1. POJ 3067 Japan 【树状数组经典】

    题目链接:POJ 3067 Japan Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32076   Accep ...

  2. poj 3067 Japan(树状数组求逆序数)

    链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...

  3. POJ 3067 Japan (树状数组求逆序对)

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

  4. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...

  5. POJ 3067 Japan

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

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

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

  7. 题解报告:poj 3067 Japan(典型BIT)

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

  8. poj 3067 - Japan(树状数组)

    先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...

  9. POJ 3067 Japan(经典树状数组)

    基础一维树状数组  题意:左边一排 1-n 的城市,右边一排 1-m 的城市,都从上到下依次对应.接着给你一些城市对,表示城市这两个城市相连,最后问你一共有多少个交叉,其中处于城市处的交叉不算并且每个 ...

随机推荐

  1. iOS开发实践之GET和POST请求

    GET和POST请求是HTTP请求方式中最最为常见的. 在说请求方式之前先熟悉HTTP的通信过程: 请求 1.请求行 : 请求方法.请求路径.HTTP协议的版本号 GET /MJServer/reso ...

  2. m_Orchestrate learning system---二十四、thinkphp里面的ajax如何使用

    m_Orchestrate learning system---二十四.thinkphp里面的ajax如何使用 一.总结 一句话总结:其实ajax非常简单:前台要做的事情就是发送ajax请求过来,后台 ...

  3. spring-data-redis 使用过程中需要注意的地方

    1.序列化问题 <!-- SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略. StringRedisTemplate默认采用的是String的序列化策略 ...

  4. PostgreSQL Replication之第九章 与pgpool一起工作(3)

    9.3 理解pgpool的架构 一旦我们安装了pgpool,是时候来讨论软件架构了.从一个用户的角度看,pgpool就像一个 正常的数据库服务器,您可以想连接任何其他服务器一样连接到它: pgpool ...

  5. codeforces 501 B Misha and Changing Handles 【map】

    题意:给出n个名字变化,问一个名字最后变成了什么名字 先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first 写一下样例就好理解 ...

  6. mysql的关联查询简写

    平常的内连接查询: SELECT * from ab_style as a INNER JOIN ab_url as b on a.style_bold=b.url_id 可支持简写风格: selec ...

  7. letCode(771 Jewels and Stones )

    问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...

  8. who---显示目前登录系统的用户信息

    who命令是显示目前登录系统的用户信息.执行who命令可得知目前有那些用户登入系统,单独执行who命令会列出登入帐号,使用的终端机,登入时间以及从何处登入或正在使用哪个X显示器. 语法 who(选项) ...

  9. U-boot 启动内核

    1:什么是UBOOT,为什么要有UBOOT? UBOOT的主要作用是用来启动linux内核,因为CPU不能直接从块设备中执行代码,需要把块设备中的程序复制到内存中,而复制之前还需要进行很多初始化工作, ...

  10. 中断函数中不能使用printf的原因

    vxworks 中断处理程序之所以不用printf,本质在于printf是将信息输出到标准输出设备(STDOUT)中, 整个标准输出设备是一个全局变量,由于有semTake操作,那么就会发生阻塞,vx ...