【CF13D】 Triangles

Describtion

Little Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do not contain any blue point inside.

Input

The first line contains two non-negative integer numbers N and M (0 ≤ N ≤ 500, 0 ≤ M ≤ 500) — the number of red and blue points respectively. The following N lines contain two integer numbers each — coordinates of red points. The following M lines contain two integer numbers each — coordinates of blue points. All coordinates do not exceed 109 by absolute value.

Output

Output one integer — the number of distinct triangles with vertices in red points which do not contain any blue point inside.

Examples

input
4 1
0 0
10 0
10 10
5 4
2 1
output
2
input
5 5
5 10
6 1
8 6
-6 -7
7 -1
5 -1
10 -4
-10 -8
-10 5
-2 -8
output
7

Translation

给定平面n个红点,m个蓝点,求三个顶点由红色构成的且内部没有蓝点的三角形个数。

分析

按y坐标升序排序
这道明显是解析几何的题
看如图

对于一个合法的三角形,那么这三条边的toleft测试的点数一定是一样的(出去上下部)
所以我们只用枚举边统计该边的有多少的蓝点在左侧
然后枚举三角形的三边,看三边左边电视是否一样,如果一样,那么一定可以形成三角形
时间复杂度O(n^3);

其中ToLeft Text (叉积)详见
解析几何学习笔记

代码

#include <bits/stdc++.h>
using namespace std;

const int N = 550;
int n,m,f[N][N],ans;

struct point
{
  long long x,y;
  bool operator < (const point &a) const
  {
    return y<a.y;
  }
} red[N],bl[N];

inline long long area(point a,point b,point c)
{
  return a.x*b.y-a.y*b.x+b.x*c.y-b.y*c.x+c.x*a.y-c.y*a.x;
}

inline bool toleft(point a,point b,point c)
{
  if(c.y<=a.y || c.y>b.y) return 0;
  return area(a,b,c)>0;
}

int main()
{
  scanf("%d%d",&n,&m);
  for(int i=1; i<=n; ++i)  scanf("%lld%lld",&red[i].x,&red[i].y);
  for(int i=1; i<=m; ++i)  scanf("%lld%lld",&bl[i].x,&bl[i].y);
  sort(red+1,red+1+n);
  for(int i=1; i<n; ++i)
    for(int j=i+1; j<=n; ++j)
      for(int k=1; k<=m; ++k)
        if(toleft(red[i],red[j],bl[k]))
          f[i][j]++;
  for(int i=1; i<n; ++i)
    for(int j=i+1; j<n; ++j)
      for(int k=j+1; k<=n; ++k)
        if(f[i][j]+f[j][k]==f[i][k])
          ans++;
  printf("%d\n",ans);
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/PiCaHor/p/9750204.html
今日推荐