Lakeside Walk 2

Time limit: 1s Memory limit: 64MB Input: Output:

Note the unusual memory limit for this problem.

Inspired by Carlo's success, Alessandro has decided to start his own software company in the mountains. However, unlike Carlo, he settled in a city where the weather is highly unpredictable.

Initially, the entire city is dry, but over the course of QQ days, rainfall and droughts will continuously change its conditions.

The city is represented as an N×MN \times M grid, with cells indexed from (1,1)(1,1) to (N,M)(N,M).

On day ii (0i<Q0 \le i < Q), a rectangular region of the grid undergoes a weather change. Given integers x1x_1, x2x_2, y1y_1, y2y_2, all cells (x,y)(x,y) satisfying:

x1xx2andy1yy2x_1\le x\le x_2\qquad \textrm{and}\qquad y_1\le y\le y_2

will switch their state -- wet cells become dry, and dry cells become wet.

At the end of each day, Alessandro wonders: What is the total perimeter of the wet regions? Unfortunately, he struggles to find this value. Can you help him determine the perimeter after each day's changes?

Input data

The input file consists of:

  • item a line containing integers NN, MM, QQ: NN and MM represent the size of the city, QQ represents the number of days.
  • item QQ lines, with line ii consisting of integers x1i\mathtt{x_1}_{i}, x2i\mathtt{x_2}_{i}, y1i\mathtt{y_1}_{i}, y2i\mathtt{y_2}_{i}, representing the rectangular area that will change on day ii.

Output data

The output file must contain a single line consisting of the QQ integers P0,,PQ1P_{0}, \, \ldots, \, P_{Q-1}.

Constraints and clarifications

  • 1N,M1 000 0001 \le N,M \le 1 \ 000 \ 000.
  • NM10 000 000N \cdot M \le 10 \ 000 \ 000.
  • 1Q200 0001 \le Q \le 200 \ 000.
  • 1x1ix2iN1 \le {x_1}_{i} \le {x_2}_{i} \le N and 1y1iy2iM1 \le {y_1}_{i} \le {y_2}_{i} \le M for each i=0Q1i=0\ldots Q-1.
# Points Constraints
1 0 Examples
2 9 N,M100N,M \le 100, Q100Q \le 100.
3 33 N,M1 000N,M \le 1 \ 000, Q10 000Q \le 10 \ 000.
4 25 NM100 000N \cdot M \le 100 \ 000.
5 33 No additional constraints.

Example

stdin

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

stdout

8
16
24
22

Explanation

In the first sample case the city is represented as a 454 \cdot 5 grid, with (1,1)(1,1) being in the bottom left corner.

In the following figures, at the end of each day, the wet cells will be colored in blue, a red line will mark the perimeter of the wet regions, and a dashed black line will outline the region affected by a weather change.

At the end of the first day, the total length of the perimeter is 88.


At the end of the second day, the total length of the perimeter is 1616.


At the end of the third day, the total length of the perimeter is 2424.


At the end of the fourth day, the total length of the perimeter is 2222.

Log in or sign up to be able to send submissions!