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;
}