Under construction

Still being built. Things may change

Farm

Time limit0.7sMemory limit2048 MB

Summary
Given a rectilinear farm boundary and pest locations, find the fewest pairwise disjoint axis-aligned rectangles inside the farm that cover every pest.
Level

Hard8 of 10

Topics
Geometry, Graph, Sorting
Solved
No attempts yet

Problem

There is a farm that borders a straight road. Suppose the road is on the xx-axis. Each boundary edge of the farm field is either horizontal or vertical. The leftmost and the rightmost edges are vertical and adjacent to the base edge which lies on the road. The length of the base edge is equal to the sum of the lengths of all other horizontal edges. See Figure C.1 (a).

(a)(b)

Figure C.1. A farm field and the pest infestation locations.

In Figure C.1, the dots on the boundary or in the interior of the farm field are the locations where pests have infested. To eradicate the infestation, a farmer divides the infested area into several rectangular areas that satisfy the following conditions.

Conditions

  • Each rectangular area must be contained within the farm. The edges of a rectangle may overlap the boundary of the farm.
  • Each edge of a rectangular area is either horizontal or vertical.
  • Rectangular areas are completely separated from each other, including their boundaries.
  • Each pest infestation location must be contained within one of the rectangular areas. A pest infestation location may lie on an edge of a rectangle.

Figure C.1 (b) shows four rectangular areas covering all pest infestation locations. The farmer wants to minimize the number of rectangular areas.

Given the boundary of a farm and the pest infestation locations, compute the minimum number of rectangular areas that satisfy the above conditions.

Input

The input starts with a line containing two integers, mm (4≤m≤100 0004 \le m \le 100\,000) and nn (0≤n≤100 0000 \le n \le 100\,000), where mm is the number of edges of the farm field and nn is the number of pest infestation locations.

The second line contains mm integers v1,v2,⋯ ,vmv_1, v_2, \cdots, v_m (v1=vm=0v_1 = v_m = 0, 0≤vi≤1060 \le v_i \le 10^6). These are the xx-coordinates of the vertical edges and the yy-coordinates of the horizontal edges. They alternate when traversing the upper boundary of the farm field clockwise from the left end of the base edge to the right end.

Each of the following nn lines contains two integers xx and yy, the coordinate (x,y)(x, y) of a pest infestation location. All locations lie on the boundary or in the interior of the farm field.

Output

Print exactly one line containing an integer, the minimum number of rectangular areas that satisfy the conditions.

Examples3

  1. Example 1

    Input
    12 8
    0 30 20 20 30 40 40 10 50 20 70 0
    4 5
    15 26
    25 15
    35 15
    35 35
    50 5
    55 15
    60 20
    
    Expected output
    4
    
  2. Example 2

    Input
    4 0
    0 10 50 0
    
    Expected output
    0
    
  3. Example 3

    Input
    12 3
    0 3 2 6 4 1 6 4 8 2 10 0
    3 5
    7 3
    3 1
    
    Expected output
    2