斜率小于0的连线数量 51Nod - 1107 (树状数组+离散化)

二维平面上N个点之间共有C(n,2)条连线。求这C(n,2)条线中斜率小于0的线的数量。

二维平面上的一个点,根据对应的X Y坐标可以表示为(X,Y)。例如:(2,3) (3,4) (1,5) (4,6),其中(1,5)同(2,3)(3,4)的连线斜率 < 0,因此斜率小于0的连线数量为2。

Input

第1行:1个数N,N为点的数量(0 <= N <= 50000) 
第2 - N + 1行:N个点的坐标,坐标为整数。(0 <= Xii, Yii <= 10^9)

Output

输出斜率小于0的连线的数量。(2,3) (2,4)以及(2,3) (3,3)这2种情况不统计在内。

Sample Input

4
2 3
3 4
1 5
4 6

Sample Output

2

题意如上。

思路

对所有的点根据优先y从小到大,y相同时x从小到大排序。对于每个点与前面的点形成的斜率小于0的线的数量即是y小于该点的点的数量减去x比改点小的点的数量,所有的点相加即得出答案。而比点的x小的点可以用树状数组维护,也可以用归并排序直接求逆序对的数量。

值得一提的是。如果数组开小了,51nod会返回超时,所以如果返回了超时,可以先看看是不是因为自己的数组开小了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sc(x) scanf("%lld",&x)
const int size=1e5+5;
typedef long long LL;
struct Point{
	LL x;LL y;
	friend bool operator<(Point a,Point b)
	{
		if(a.y==b.y) return a.x<b.x;
		return a.y<b.y;
	}
}p[size];
using namespace std;
LL arr[size];
LL id[size];
LL cnt[size];
LL Cnt=0;
void Update(LL Id)
{
	while(Id<=Cnt)
	{
		cnt[Id]++;
		Id+=lowbit(Id);
	}
}
LL num(LL Id)
{
	LL ans=0;
	while(Id>0)
	{
		ans+=cnt[Id];
		Id-=lowbit(Id);
	}
	return ans;
}
int main()
{
	LL n;
	while(~scanf("%lld",&n))
	{
		for(LL i=1;i<=n;i++)
		{
			sc(p[i].x),sc(p[i].y);
			arr[i]=p[i].x;
		}
		sort(p+1,p+n+1);
		sort(arr+1,arr+1+n);
		Cnt=unique(arr+1,arr+1+n)-arr-1;
		for(LL i=1;i<=n;i++) id[i]=lower_bound(arr+1,arr+1+Cnt,p[i].x)-arr;
		LL ans=0;
		int l=1;
		Update(id[1]);
		for(LL i=2;i<=n;i++)
		{
			LL Id=id[i];
			ans+=i-1-num(Id);
			Update(id[i]);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值