Garden
Time limit2sMemory limit1024 MB
Find the smallest axis-aligned rectangle that contains at least one artwork of each kind, where type A repeats with period D in both directions and type B in one.
- Level
Hard8 of 10
- Topics
- Two pointers, Segment tree, Sorting, Math
- Solved
- No attempts yet
Statement
JOI Kingdom is a mysterious kingdom which has a boundless expanse of territory. JOI-kun, the king of JOI Kingdom, is planning to cut a part of the territory and make his garden.
The territory of JOI Kingdom is considered as a sufficiently large -dimensional grid. The grid is paved with square cells from the top to the bottom and from the left to the right. There is a cell, which is the origin of the coordinates. Let denote the cell one arrives at when one moves from the origin to the right direction for the distance of cells and to the upward direction for the distance of cells. Here, the left direction for the distance of cells means the right direction for the distance of cells. Similarly, the downward direction for the distance of cells means the upward direction for the distance of cells.
Some artworks are placed on the territory. The artworks are classified into two types, Type A and Type B, according to the way to be placed in the territory.
- There are kinds of artworks of type A. An artwork of -th kind () is placed on every cell of the form , where , are integers.
- There are kinds of artworks of type B. An artwork of -th kind () is placed on every cell of the form , where , are integers, or of the form , where , are integers.
A cell may contain several artworks of different kinds.
JOI-kun is planning to choose a rectangular region on the grid to make a garden. In other words, he will choose integers , , , . Then the cells of the form , where , are integers satisfying , , will constitute JOI-kun's garden. Since JOI-kun likes to see artworks of many kinds, for any of the kinds of artworks, JOI-kun's garden should contain at least one artwork of that kind. On the other hand, the citizens of JOI Kingdom will be angry if JOI-kun plans to make a too large garden. Therefore, JOI-kun wants to minimize the number of cells in the garden so that the above condition is satisfied.
Write a program which, given information of artworks, calculates the minimum number of cells in JOI-kun's garden.
Input
N M D
P_1 Q_1
...
P_N Q_N
R_1 S_1
...
R_M S_M
Output
Write one line to the standard output. The output should contain the minimum number of cells in JOI-kun's garden.
Constraints
- .
- .
- .
- .
- ().
- ().
- ().
- ().
- Given values are all integers.