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 MBGeohash 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×2n grid placed in a standard coordinate system, where the x coordinate grows to the right and the y 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) such that 0≤x,y<2n.
A 2n×2n map has 22n cells in total. The geohash h(c) of a map cell c is a 2n-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 n times:
A geohash interval [a,b] is the set of cells whose geohash values are between a and b, both inclusive. It is often useful to approximate a map region with a few geohash intervals. Given a set of cells C and an integer t, an optimal t-approximation of C is a region of minimum area that contains C and can be written as a union of at most t geohash intervals. Formally, it is a set S of at most t geohash intervals such that:
The region C is the set of all cells inside a polygon whose sides are parallel to the grid lines. You are also given q integers t1,t2,…,tq. For each tk, find the area of an optimal tk-approximation of C, that is, the number of cells in it.
The first line contains an integer n (1≤n≤30). The side length of the map is 2n.
The second line contains an even integer m (4≤m≤200), the number of vertices of the polygon. The k-th of the next m lines contains two integers xk and yk (0≤xk,yk≤2n), 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 q (1≤q≤100000), the number of queries. The k-th of the next q lines contains a single integer tk (1≤tk≤109), the k-th query.
For each query, print one line. The k-th line contains the area of an optimal tk-approximation of the given region.

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