## DMOPC '18 Contest 6 P5 - Quadrat Sampling

View as PDF

Points: 20 (partial)
Time limit: 2.5s
Memory limit: 256M

Author:
Problem type

Renowned scientist Dr. Henri has just returned from an expedition to Algonquin Park to document the sizes of various animal populations. Unfortunately, he has forgotten to study the most important animal of all: the Canada goose!

As Dr. Henri does not have time to go back to the park, he decides to take a satellite image of it and count the geese in the image instead. Algonquin Park can be represented as a grid of rows and columns. There are geese in the park, the -th of which is located at row and column . Multiple geese may be at the same location.

Of course, it would be too time-consuming to count every single goose in the image. So, Dr. Henri will use a method called quadrat sampling. He will choose rectangles, or quadrats, the -th of which has its top left corner at and its bottom right corner at . Then, he will define the raw total as the sum of the number of geese within quadrat over all from to . He can then use this total to estimate the size of the goose population.

However, the geese have gotten wind of Dr. Henri's plan! They know that Dr. Henri will take the satellite image in exactly minutes from now, and they also know the positions of the quadrats.

In an attempt to make their population seem larger and even more terrifying, each goose may choose to fly to a new position. A goose can fly at the speed of one cell per minute, and in order to save time, each goose will fly along only one of the four cardinal directions, without turning. The geese want to maximize the raw total obtained by Dr. Henri.

What is the largest raw total obtainable?

#### Input Specification

The first line will contain five space-separated integers, , , , , and .
The next lines will each contain two space-separated integers, and , the location of the -th goose.
The next lines will each contain four space-separated integers, , , , and , the position of the -th quadrat.

#### Output Specification

Output one integer, the raw total that Dr. Henri will obtain if the geese choose their positions optimally.

#### Sample Input 1

5 5 3 2 2
1 3
4 1
3 4
1 3 3 5
3 2 4 4

#### Sample Output 1

5

#### Explanation for Sample 1

If goose 1 flies downward to , goose 2 flies rightward to , and goose 3 stays put, Dr. Henri will count 2 geese in quadrat 1 and 3 geese in quadrat 2 for a raw total of 5 geese.

#### Sample Input 2

5 5 3 2 0
1 3
4 1
3 4
1 3 3 5
3 2 4 4

#### Sample Output 2

3

#### Explanation for Sample 2

The geese do not have time to move at all, so Dr. Henri will count 2 geese in quadrat 1 and 1 goose in quadrat 2 for a raw total of 3 geese.