码迷,mamicode.com
首页 > 其他好文 > 详细

灯塔(LightHouse)

时间:2015-08-26 15:42:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

Description

As shown in the following figure, If another lighthouse is in gray area, they can beacon each other.

技术分享

For example, in following figure, (B, R) is a pair of lighthouse which can beacon each other, while (B, G), (R, G) are NOT.

技术分享

Input

1st line: N

2nd ~ (N + 1)th line: each line is X Y, means a lighthouse is on the point (X, Y).

Output

How many pairs of lighthourses can beacon each other

( For every lighthouses, X coordinates won‘t be the same , Y coordinates won‘t be the same )

Example

Input

3
2 2
4 3
5 1

Output

1

Restrictions

For 90% test cases: 1 <= n <= 3 * 105

For 95% test cases: 1 <= n <= 106

For all test cases: 1 <= n <= 4 * 106

For every lighthouses, X coordinates won‘t be the same , Y coordinates won‘t be the same.

1 <= x, y <= 10^8

Time: 2 sec

Memory: 256 MB

Hints

The range of int is usually [-231, 231 - 1], it may be too small.

第一眼看到这题时第一反应是用树状数组,但是发现数据太大,开不了10的6次方的二维数组,看了别人的博客才知道是用归并排序。

先理解一下题意,要求有多少对两两互相照亮的灯塔,怎么样才能是两两互相照亮的呢,两点的斜率为正,也就是按x递增排序,y也递增。

这样咱们就可以把n个点对x进行排序,找到y有多少对是顺序对就行,这就可以用到归并排序,在对左右两个集合合并时,i,j分别为左右两个集合的

指针,如果le[i]<ri[j],正序对加上n2-j+1对,就这样去考虑。还有就是那里不能用<algorithm>,可以用stdlib.h里的qsort()。

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 
 6 using namespace std;
 7 
 8 
 9 
10 struct w
11 {
12        int x;
13        int y;
14 }k[5100000];
15 int yi[5100000];
16 long long ans;
17 int le[5000005],ri[5000005];
18 
19 int cmp( const void *a ,const void *b)
20 { 
21     return (*(w *)a).x > (*(w *)b).x ; 
22 }
23 
24 
25 void merge(int l,int mi,int r)
26 {
27      int i,j,p,n1=mi-l+1,n2=r-mi;
28      const int MAX=500000005;
29      
30      for (int i=1;i<=n1;i++)
31      le[i]=k[l+i-1].y;
32      for (int i=1;i<=n2;i++)
33      ri[i]=k[mi+i].y;
34       
35       le[n1+1]=MAX;ri[n2+1]=MAX;
36       i=1;j=1;
37       for (int p=l;p<=r;p++)
38       {
39           if (le[i]>ri[j])
40           k[p].y=ri[j++];
41           else
42           {
43               k[p].y=le[i++];
44               ans+=n2-j+1;
45           //    cout <<ans<<endl;
46           }
47       }
48 }
49 void mergesort(int l,int r)
50 {
51      if (l==r)
52      return;
53      int mid=l+(r-l)/2;
54      mergesort(l,mid);
55      mergesort(mid+1,r);
56      merge(l,mid,r);
57 }
58 
59 int main()
60 {
61     int n;
62     cin>>n;
63     for (int i=0;i<n;i++)
64     cin>>k[i].x>>k[i].y;
65     qsort(k,n,sizeof(k[0]),cmp);
66     ans=0;
67     mergesort(0,n-1);
68     cout <<ans<<endl;
69     return 0;
70 }

 

灯塔(LightHouse)

标签:

原文地址:http://www.cnblogs.com/arno-my-boke/p/4760450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!