C++的一路题,好像是和逆序对有关,求源代码,非常感谢

C++的一道题,好像是和逆序对有关,求源代码,非常感谢!
灯塔(LightHouse)
描述
海上有许多灯塔,为过路船只照明。从平面上看,海域范围是[1, 10^7] × [1, 10^7] 。

C++的一路题,好像是和逆序对有关,求源代码,非常感谢

(图一)

如图一所示,每个灯塔都配有一盏探照灯,照亮其东北、西南两个对顶的直角区域。探照灯的功率之大,足以覆盖任何距离。灯塔本身是如此之小,可以假定它们不会彼此遮挡。

C++的一路题,好像是和逆序对有关,求源代码,非常感谢

(图二)

若灯塔A、B均在对方的照亮范围内,则称它们能够照亮彼此。比如在图二的实例中,蓝、红灯塔可照亮彼此,蓝、绿灯塔则不是,红、绿灯塔也不是。

现在,对于任何一组给定的灯塔,请计算出其中有多少对灯塔能够照亮彼此。

输入
共n+1行。

第1行为1个整数n,表示灯塔的总数。

第2到n+1行每行包含2个整数x, y,分别表示各灯塔的横、纵坐标。

输出
1个整数,表示可照亮彼此的灯塔对的数量。

输入样例
3
2 2
4 3
5 1
输出样例
1
限制
1 ≤ n ≤ 2×10^5

灯塔的坐标x, y是整数,且不同灯塔的x, y坐标均互异

1 ≤ x, y ≤ 10^7

提示
注意机器中整型变量的范围
整型范围 c++ 逆序对 归并排序 灯塔

------解决方案--------------------
20万级别的数据,需要复杂度 <= O(nlogn)的
问题解决的思路类似逆序数对,这里记录的是正序数对,下面是实现的代码,仅供参考:

#include <stdio.h>
#include <stdlib.h>
#define MAX 200000

struct Point{
    int x, y;
};
int compareByX(const void* a, const void* b)
{
    return ((struct Point*)a)->x - ((struct Point*)b)->x;
}

int n;
struct Point a[MAX];//all points
int ay[MAX];        //made of each point's y coordinate
int ordered;        //record how many ordered pairs that is ay[i] < ay[k], before sort ay[]


void merge(int dest[], int src[], int startIndex, int midIndex, int endIndex)
{
    int i = startIndex, j = midIndex + 1, k = startIndex;
    for(; i <= midIndex && j <= endIndex; ++k){
        if(src[i] > src[j]) dest[k] = src[j++];
        else{
            dest[k] = src[i++];
            ordered += endIndex - j + 1;  //here record how many ordered pairs in this segment
        }
    }
    for(; i <= midIndex; ++k, ++i) dest[k] = src[i];
    for(; j <= endIndex; ++k, ++j) dest[k] = src[j];
}

void mergeSort(int dest[], int src[], int startIndex, int endIndex, int mergeIntoSrc)
{
    if(startIndex == endIndex){
        dest[startIndex] = src[startIndex];
    }
    else{
        int midIndex = (startIndex + endIndex) >> 1;
        mergeSort(dest, src, startIndex, midIndex, !mergeIntoSrc);  //use as temporary storage
        mergeSort(dest, src, midIndex+1, endIndex, !mergeIntoSrc);  //use as temporary storage
        if(mergeIntoSrc) merge(src, dest, startIndex, midIndex, endIndex);//store the ordered array to src
        else merge(dest, src, startIndex, midIndex, endIndex);            //store the ordered array to dest
    }
}

void merge_sort(int a[], int n)
{
    static int help[MAX];         //helper space used in merge sort
    mergeSort(help, a, 0, n-1, 1);//use help as temporary storage, store the ordered array to a
}

int main()
{
    int i;
    //input
    scanf("%d", &n);
    for(i = 0; i < n; ++i) scanf("%d%d", &a[i].x, &a[i].y);
    if(n < 2){   //can not make any pair