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. CorePlot学习六---点击scatterPlot中的symbol点时弹出对应的凝视

    因为项目须要用到用户点击 symbol时,弹出对应的具体信息,发现国内解说的比較少,经过一番搜索验证最终解决,先看效果图: 详细须要改动的代码例如以下: 首先要引用托付方法:CPTScatterPlo ...

  2. MongoDB Shell (mongo)

    https://docs.mongodb.com/getting-started/shell/client/ The mongo shell is an interactive JavaScript ...

  3. php中命名空间和use

    php中命名空间和use 总结 php中的namespace就有点像java中package包的概念 php中的use的概念就是用别人的命名空间中的类 php中的include enquire是引入文 ...

  4. Lamp安装 php-v5.6【ZendGuardLoader】的问题

    Lamp安装 php-v5.6[ZendGuardLoader]的问题 标签(空格分隔):php,linux Apache日志: 就这个问题导致无法解析运行php文件.下面是网上找的解决方案 Zend ...

  5. sicily 1000. LinkedList

    Description template <typename E> class LinkedList { private:     // inner class: linked-list ...

  6. HDFS的安全模式

  7. C#篇(一)——字段与属性

    字段和属性有什么区别? class Student { private int age; public int Age { get { return age; } set { age = value; ...

  8. IE(8~11+) 可用右键加速器

    必应词典工具 立即安装: 网络安装:http://dict.bing.com.cn/tools_dl.aspx?dl=ie8acc&mkt=ZH-CN 开发示例: <?xml versi ...

  9. 976 C. Nested Segments

    You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task ...

  10. Python3基础笔记--函数

    一.函数 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可     特性: 1)代码重用 2)保持一致性 3)可扩展性 参考博客: Py西游攻关之 ...