POJ——T 3067 Japan

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

Southeastern Europe 2006

给坐标排序,求逆序对,手模一下好理解、

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 const int N(1111);
 8 int n,m,k,tr[N];
 9 struct Node
10 {
11     int west,east;
12 }node[N*N];
13 bool cmp(Node a,Node b)
14 {
15     if(a.east==b.east) return a.west>b.west;
16     return a.east>b.east;
17 }
18
19 #define lowbit(x) (x&((~x)+1))
20 inline void Update(int i,int x)
21 {
22     for(;i<=N;i+=lowbit(i)) tr[i]+=x;
23 }
24 inline int Query(int x)
25 {
26     int ret=0;
27     for(;x;x-=lowbit(x)) ret+=tr[x];
28     return ret;
29 }
30
31 int main()
32 {
33     int t; scanf("%d",&t); int h=1;
34     for(long long ans=0;h<=t;h++,ans=0)
35     {
36         scanf("%d%d%d",&n,&m,&k);
37         for(int i=1;i<=k;i++)
38             scanf("%d%d",&node[i].west,&node[i].east);
39         sort(node+1,node+k+1,cmp);
40         memset(tr,0,sizeof(tr));
41         for(int i=1;i<=k;Update(node[i++].west,1))
42             ans+=(long long)Query(node[i].west-1);
43         printf("Test case %d: %I64d\n",h,ans);
44     }
45     return 0;
46 }
时间: 2024-10-10 07:39:44

POJ——T 3067 Japan的相关文章

poj 3067 Japan(线段树)

题目链接:poj 3067 Japan 题目大意:给定N和M,表示东部和西部城市的数量,然后K条铁路,每条铁路连接东西城市,问说会有多少次交点. 解题思路:线段树维护即可,每条边按照x小的,y小的排序,然后每次查询y+1到M的即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; #define lson(x) ((

两边点连直线求交点总数 树状数组或线段树 poj 3067 Japan

http://poj.org/problem?id=3067 Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23602   Accepted: 6369 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

POJ 3067 Japan(树状数组/求逆序数)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22258   Accepted: 5995 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 coas

POJ 3067 Japan (树状数组)

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

POJ 3067 Japan

题意: 东海岸有x个城市,西海岸有y个.x与y 之间有很多高速公路.问k条路有多少个交叉点. 我就是求的逆序对,把east当作 pos 按照从大到小排序.然后插入.接下来的就跟求逆序对的一样了. 线段树或者数状数组都能过. 注意最后要用long long. (午夜一发,写完吃个面包睡觉. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algori

POJ 3067 Japan (树状数组 &amp;&amp; 控制变量)

题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条公路的起点或者终点相同那这两点不算做相交) 分析: 这里公路信息用(x, y)二元组来表示西海岸的x城市与东海岸的y城市相连, 首先自然想到给k个公路的信息按x或y排个序, 否则变量太乱不助于思考!先设想按x升序排序且先不管x相等的情况, 如果x是升序的, 那我在考虑第i条公路的时候是不是只要关心y

POJ 3067 Japan 树状数组求逆序对

题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把所有的路先按照x值从小到大排序,x值相同的按照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些边的x值一定比自己大,也就是一个逆序对,然后统计起来.记得答案要用long long (__int64) CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorith

POJ 3067 Japan 【 树状数组 】

题意:左边有n个城市,右边有m个城市,现在修k条路,问会形成多少个交点 先按照x从小到大排,x相同的话,则按照y从小到大排,然后对于每一个y统计前面有多少个y比它大,它们就一定会相交 另外要用long long 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&g

{POJ}{树状数组}

总结一下树状数组的题目: {POJ}{3928}{Ping Pong} 非常好的题目,要求寻找一个数组中满足A[i]<A[k]<A[j]的个数,其中i<k<j(或者相反).很巧妙的将题目转化为树状数组的思想,从A[k]考虑,则只需要寻找左边比自己小和右边比自己大的可能性(或者相反),这样就可以用树状数组来维护.思想的转变很重要. {POJ}{1990}{MooFest} n头牛,不同的听力值v,当i,j想要通话时,需要max(v(i),v(j))*(dist[i]-dist[j])