Counting ear shapes

Count quadruples of red points and pairs of blue points forming an ear shape with angle and containment conditions.

Hard8GeometryBrute forceSortingImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

The coordinate plane holds NN red points and MM blue points. Every red point has yy coordinate 00, and every blue point has a yy coordinate greater than 00.

Subin wants to build an ear shape out of these points. An ear shape is made of four distinct red points AA, BB, CC, DD and two distinct blue points PP, QQ, and it must satisfy all three conditions below.

  • Point BB and point CC lie in the interior of segment ADAD.
  • Angle PADPAD, angle PDAPDA, angle QBCQBC and angle QCBQCB are all smaller than 9090 degrees.
  • Point QQ lies in the interior of triangle PADPAD. It must not lie on a side.

Given the red points and the blue points, write a program that counts the ways to build an ear shape.

Swapping AA with DD, or BB with CC, gives the same ear shape. Two ear shapes count as different when they differ in the two points used as AA and DD, the two points used as BB and CC, the point PP, or the point QQ.

Input

The first line contains NN and MM. (1N,M3001 \le N, M \le 300)

The second line contains the xx coordinates of the NN red points.

Each of the next MM lines contains the xx coordinate and the yy coordinate of one blue point.

Every coordinate in the input is a natural number no greater than 1000010\,000. The red points are distinct, and the blue points are distinct.

Output

Print the number of ways to build an ear shape on the first line.

Hint

Suppose the red points sit at x=1x = 1, x=2x = 2, x=6x = 6, x=9x = 9, and the blue points are (3,1)(3, 1), (6,5)(6, 5), (8,4)(8, 4), (5,3)(5, 3). All four red points are used, so AA and DD are the points at x=1x = 1 and x=9x = 9, and BB and CC are the points at x=2x = 2 and x=6x = 6. There are four ways to pick PP and QQ.

  • P=(5,3)P = (5, 3), Q=(3,1)Q = (3, 1)
  • P=(6,5)P = (6, 5), Q=(3,1)Q = (3, 1)
  • P=(8,4)P = (8, 4), Q=(3,1)Q = (3, 1)
  • P=(6,5)P = (6, 5), Q=(5,3)Q = (5, 3)