Geohash Grid

For a rectilinear polygon inside a 2^n by 2^n grid, answer up to 1e5 queries asking the smallest cell count of a union of at most t geohash intervals covering it.

Hard9Divide and conquerTreeGreedyDynamic programmingNo attempts yetTime limit5sMemory limit512 MB

Problem

Geohash is a way of encoding map coordinates as scalar values so that geographical data can be stored and queried efficiently in databases. In this problem, a map is a 2n×2n2^n \times 2^n grid placed in a standard coordinate system, where the xx coordinate grows to the right and the yy coordinate grows upward. A map cell is a unit square aligned with the coordinate axes whose lower-left corner is a point with integer coordinates (x,y)(x, y) such that 0x,y<2n0 \le x, y < 2^n.

A 2n×2n2^n \times 2^n map has 22n2^{2n} cells in total. The geohash h(c)h(c) of a map cell cc is a 2n2n-bit nonnegative integer, built one bit at a time starting from the most significant bit. Set the viewport to the entire map and repeat the following two steps nn times:

  1. Divide the viewport into two equal parts, the left half and the right half. If cell cc is in the left half, the next bit is 0; otherwise it is 1. The half that contains cell cc becomes the new viewport.
  2. Divide the viewport into two equal parts, the bottom half and the top half. If cell cc is in the bottom half, the next bit is 0; otherwise it is 1. The half that contains cell cc becomes the new viewport.

A geohash interval [a,b][a, b] is the set of cells whose geohash values are between aa and bb, both inclusive. It is often useful to approximate a map region with a few geohash intervals. Given a set of cells CC and an integer tt, an optimal tt-approximation of CC is a region of minimum area that contains CC and can be written as a union of at most tt geohash intervals. Formally, it is a set SS of at most tt geohash intervals such that:

  • Every cell of CC is contained in at least one interval of SS.
  • The number of cells in the union of all intervals of SS is as small as possible.

The region CC is the set of all cells inside a polygon whose sides are parallel to the grid lines. You are also given qq integers t1,t2,,tqt_1, t_2, \ldots, t_q. For each tkt_k, find the area of an optimal tkt_k-approximation of CC, that is, the number of cells in it.

Input

The first line contains an integer nn (1n301 \le n \le 30). The side length of the map is 2n2^n.

The second line contains an even integer mm (4m2004 \le m \le 200), the number of vertices of the polygon. The kk-th of the next mm lines contains two integers xkx_k and yky_k (0xk,yk2n0 \le x_k, y_k \le 2^n), the coordinates of one vertex of the polygon. The vertices are given in counterclockwise order. Every side of the polygon is either vertical or horizontal. The polygon does not intersect or touch itself, and no two consecutive sides are parallel.

The next line contains an integer qq (1q1000001 \le q \le 100\,000), the number of queries. The kk-th of the next qq lines contains a single integer tkt_k (1tk1091 \le t_k \le 10^9), the kk-th query.

Output

For each query, print one line. The kk-th line contains the area of an optimal tkt_k-approximation of the given region.

Hint

For the region in the figure, the intervals [3,29][3, 29], [33,33][33, 33] and [36,37][36, 37] form an optimal 3-approximation. The union of the three intervals has area 30.