20200523 SCOI模拟T2(计算几何)

T2

在这里插入图片描述
在这里插入图片描述
思路:
考虑如何判定一个点在三角形内部
只要它在三角形任意两角内部,那么一定在三角形内部
于是直线 AB 两侧分别处理
对于一侧从两个方向分别极角排序
当一个点 C 同时两次在 D 前时,C Δ A B C \Delta ABC ΔABC 内部
做二维偏序即可

代码:

#include<bits/stdc++.h>
using namespace std;
#define in Read()
#define re register
#define LL long long

inline char ch(){
	static char buf[1<<21],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
}

inline int in{
	int s=0,f=1;char x;
	for(x=ch();x<'0'||x>'9';x=ch())	if(x=='-')	f=-1;
	for( ;x>='0'&&x<='9';x=ch())	s=(s*10)+(x&15);
	return f==1?s:-s; 
}

const int A=3e5+5;
const double ESP=1e-7;
inline int sign(double x){
	if(fabs(x)<ESP)	return 0;
	if(x>0)	return 1;
	return -1;
}
int n;
struct Point{
	double x,y;
	Point (double u=0,double v=0){
		x=u,y=v;
	}
	inline friend Point operator + (const Point u,const Point v){
		return Point(u.x+v.x,u.y+v.y);
	}
	inline friend Point operator - (const Point u,const Point v){
		return Point(v.x-u.x,v.y-u.y);
	}
	inline friend Point operator * (const Point u,double v){
		return Point(u.x*v,u.y*v);
	}
	inline friend Point operator / (const Point u,double v){
		return Point(u.x/v,u.y/v);
	}
	inline friend double operator & (const Point u,const Point v){
		return u.x*v.x+u.y*v.y;
	}
	inline friend double operator ^ (const Point u,const Point v){
		return u.x*v.y-u.y*v.x;
	}
}a,b,p[A];
struct node{
	Point p;
	int id;
}now[A];
int tot;
inline bool cmp1(node u,node v){
	return sign((u.p-a)^(v.p-a))<0;
}
inline bool cmp2(node u,node v){
	return sign((u.p-b)^(v.p-b))<0;
}
inline bool cmp3(node u,node v){
	return sign((u.p-a)^(v.p-a))>0;
}
inline bool cmp4(node u,node v){
	return sign((u.p-b)^(v.p-b))>0;
}
LL tree[A];
inline int lowbit(int x){
	return x&(-x);
}
inline void add(int x,int val){
	while(x<=n){
		tree[x]+=val;
		x+=lowbit(x);
	}
	return;
}
inline LL qurey(int x){
	LL ans=0;
	while(x){
		ans+=tree[x];
		x-=lowbit(x);
	}
	return ans;
}
LL res=0;

inline void scan(){
	int p1=in,p2=in,p3=in,p4=in;
	a.x=p1*1.0,a.y=p2*1.0,b.x=p3*1.0,b.y=p4*1.0;
	for(re int i=1;i<=n;++i){
		p1=in,p2=in;
		p[i].x=p1*1.0,p[i].y=p2*1.0;
	}
	return;
}

inline void find(){
	memset(tree,0,sizeof(tree));
	for(re int i=1;i<=tot;++i){
		res+=i-qurey(now[i].id)-1;
		add(now[i].id,1);
	}
	return;
}

inline void up(){
	tot=0;
	for(re int i=1;i<=n;++i)
		if(sign((b-a)^(p[i]-a))>0){
			now[++tot].p=p[i];
			now[tot].id=0;
		}	
	sort(now+1,now+1+tot,cmp1);
	for(re int i=1;i<=tot;++i)	now[i].id=i;
	sort(now+1,now+1+tot,cmp2);
	find();
	return;
}

inline void down(){
	tot=0;
	for(re int i=1;i<=n;++i)
		if(sign((b-a)^(p[i]-a))<0){
			now[++tot].p=p[i];
			now[tot].id=0;
		}	
	sort(now+1,now+1+tot,cmp3);
	for(re int i=1;i<=tot;++i)	now[i].id=i;
	sort(now+1,now+1+tot,cmp4);
	find();
	return;
}

inline void work(){
	up();
	down();
	printf("%lld\n",res);
}


signed main(){
	freopen("laststardust.in","r",stdin);
	freopen("laststardust.out","w",stdout);
	n=in;
	scan();
	work();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值