#P9514. [JOI Open 2023] 花园 / Garden

[JOI Open 2023] 花园 / Garden

Background

Translated from JOI Open 2023 T3 “庭園 / Garden”.

Problem Description

The JOI Kingdom is a mysterious kingdom with a vast territory. King JOI is the king of the JOI Kingdom, and he is planning to split off part of the territory to build his garden.

The JOI network can be regarded as a sufficiently large 2D grid, tiled by square cells from top to bottom and from left to right. One cell is the origin. Let (x,y)(x,y) denote the cell reached by moving xx cells to the right from the origin, and then moving yy cells up. Here, moving aa cells to the left means moving a-a cells to the right. Similarly, moving aa cells downward means moving a-a cells upward.

There are some artworks in the territory. These artworks are divided into Type A and Type B according to how they are placed in the territory.

  • There are NN kinds of Type A artworks. The ii-th kind of artwork (1iN1\le i\le N) is placed in every cell of the form (Pi+kD,Qi+lD)(P_i+kD,Q_i+lD), where k,lk,l are integers.
  • There are MM kinds of Type B artworks. The jj-th kind of artwork (1jM1\le j\le M) is placed in every cell of the form (Rj+kD,y)(R_j+kD,y) (where k,yk,y are integers) or (x,Sj+lD)(x,S_j+lD) (where l,xl,x are integers).

Note that a single cell may contain multiple artworks of different kinds.

King JOI plans to choose a rectangular region on the grid to build the garden. In other words, he chooses four integers a,b,c,da,b,c,d. Then the cells (x,y)(x,y) form King JOI’s garden, where x,yx,y are integers satisfying axb,cyda\le x\le b,c\le y\le d. Since King JOI likes to see many kinds of artworks, for each of the N+MN+M kinds of artworks, the garden must contain at least one artwork of that kind. On the other hand, if the garden is too large, the citizens of the JOI Kingdom will get angry. Therefore, King JOI wants to minimize the number of cells contained in the garden while satisfying the above condition.

Given the information about the artworks, write a program to compute the minimum number of cells contained in King JOI’s garden.

Input Format

The first line contains three integers N,M,DN,M,D.

The next NN lines each contain two integers Pi,QiP_i,Q_i.

The next MM lines each contain two integers Rj,SjR_j,S_j.

Output Format

Output one integer in a single line, representing the minimum number of cells contained in King JOI’s garden.

2 1 5
1 4
2 2
0 0

8

3 4 100
20 26
81 56
20 3
58 71
74 82
95 61
95 61

2840

5 7 5000
1046 365
4122 1166
4009 2896
1815 4065
4372 1651
2382 123
1475 836
3313 4005
2579 568
4300 4867
1050 3214
3589 4653

10543092

Hint

[Sample Explanation #1]

The figure below shows the cells (x,y)(x,y) in the JOI Kingdom’s territory satisfying 0x<10,0y<100\le x<10,0\le y<10.

In the figure, circles and diamonds represent Type A and Type B artworks, respectively. The integer inside each circle or diamond indicates the kind index of the artwork. If King JOI chooses a=1,b=2,c=2,d=5a=1,b=2,c=2,d=5, then King JOI’s garden is the black rectangular region. In this case, for these three kinds of artworks, King JOI’s garden contains at least one of each kind. The number of cells in the garden is 88. Since there is no smaller garden that satisfies the condition, output 88.

This sample satisfies the constraints of all subtasks.

[Sample Explanation #2]

This sample satisfies the constraints of subtasks 1,4,5,61,4,5,6.

[Sample Explanation #3]

This sample satisfies the constraints of subtasks 1,5,61,5,6.

[Constraints]

For all testdata, it holds that:

  • N,M1N,M\ge 1
  • N+M5×105N+M\le 5\times 10^5
  • 1D5 0001\le D\le 5\ 000
  • 0Pi,Qi,Rj,Sj<D0\le P_i,Q_i,R_j,S_j<D

The additional constraints and scores for the subtasks are shown in the table below.

Subtask Additional Constraints Score
11 M8M\le 8 1515
22 D10,N+M5000D\le 10,N+M\le 5000 66
33 D50,N+M5000D\le 50,N+M\le 5000 88
44 D100,N+M5000D\le 100,N+M\le 5000 1616
55 N+M5000N+M\le 5000 3030
66 No additional constraints 2525

Translated by ChatGPT 5