#P2692. Cleaning the Playground

    ID: 15957 Type: Default 1000ms 256MiB

Cleaning the Playground

Cleaning the Playground

The playground is modeled as a grid of \(N\) rows and \(M\) columns. Each boy is responsible for cleaning some consecutive rows, and each girl cleans some consecutive columns. When a boy cleans, he cleans all the cells in his assigned row range, and when a girl cleans, she cleans all the cells in her assigned column range. Note that some cells may be cleaned more than once due to overlapping assignments.

The total number of unique cleaned cells can be computed by taking the union of the rows and columns cleaned by the boys and girls respectively. If \(|U|\) is the number of unique rows cleaned by the boys and \(|V|\) is the number of unique columns cleaned by the girls, then the total cleaned cells is given by:

\[ \text{Cleaned cells} = (|U| \times M) + (|V| \times N) - (|U| \times |V|)\ \]

Given the cleaning assignments, determine the total number of cells cleaned.

inputFormat

The first line contains two integers \(N\) and \(M\) (the number of rows and columns of the grid).

The second line contains an integer \(B\), the number of boys. Each of the following \(B\) lines contains two integers \(r_1\) and \(r_2\) (1-indexed) representing the starting and ending row (inclusive) of a boy's cleaning segment.

The next line contains an integer \(G\), the number of girls. Each of the following \(G\) lines contains two integers \(c_1\) and \(c_2\) (1-indexed) representing the starting and ending column (inclusive) of a girl's cleaning segment.

outputFormat

Output a single integer representing the total number of cells cleaned.

sample

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