Frequencies

Time limit: 0.15s Memory limit: 256MB Input: Output:

On an nnn \cdot n matrix, initially empty, you perform mm operations. Each operation can be of the following two kinds:

  • Horizontal(l,r,x)(l, r, x): All cells in rows l,l+1,,rl, l + 1, \dots, r are set to value xx
  • Vertical(l,r,x)(l, r, x): All cells in columns l,l+1,,rl, l + 1, \dots, r are set to value xx

After executing all operations, report how many times do the least and most frequent elements occur in the matrix.

Input

The first line contains the size of the matrix nn, and the number of operations mm.

Each of the next mm lines describe one operation.

The ithi^{th} operation is described by 4 values ti,li,ri,xit_i, l_i, r_i, x_i, where tit_i is a character that describes the kind of the ithi^{th} operation (either H for horizontal or V for vertical) and li,ri,xil_i, r_i, x_i describe the ithi^{th} operation.

Output

The output consists of a single line that contains the frequency of the element that appears least often frminfr_{min} and the frequency of the element that appears most often in the matrix frmaxfr_{max} after carrying out the mm operations.

Constraints

  • 1n1 000 0001 \leq n \leq 1 \ 000 \ 000
  • ti{t_i \in \{ H, V}\}
  • 1lirin1 \leq l_i \leq r_i \leq n
  • 1m200 0001 \leq m \leq 200 \ 000
  • 1xi100 0001 \leq x_i \leq 100 \ 000

Subtasks

  • For 20 points: 1n1 0001 \leq n \leq 1 \ 000, 1m1001 \leq m \leq 100, 1xi401 \leq x_i \leq 40
  • For another 20 points: 1n2 0001 \leq n \leq 2 \ 000, 1n2m1 000 000 0001 \leq n^2 \cdot m \leq 1 \ 000 \ 000 \ 000
  • For another 20 points: 1n6 0001 \leq n \leq 6 \ 000, 1m100 0001 \leq m \leq 100 \ 000
  • For another 20 points: 1n200 0001 \leq n \leq 200 \ 000
  • For another 20 points: No further restrictions

Note: The tests for this task are scored individually!

Example 1

stdin

5 4
H 1 4 2
H 3 5 1
V 2 2 1
H 3 4 3

stdout

7 10

Example 2

stdin

6 5
V 5 5 3
H 4 5 4
V 1 6 3
V 1 2 2
V 4 4 2

stdout

18 18

Example 3

stdin

6 5
H 3 4 2
V 4 5 1
V 4 6 2
H 5 6 2
H 5 6 4

stdout

12 18

Example 4

stdin

8 8
H 4 8 3
H 2 3 3
V 5 7 3
V 4 5 2
H 1 6 2
V 7 8 2
V 5 6 2
H 2 4 4

stdout

6 34

Explanation

In the first example, after applying all the operations, the matrix looks as follows:

2 1 2 2 22 \ 1 \ 2 \ 2 \ 2
2 1 2 2 22 \ 1 \ 2 \ 2 \ 2
3 3 3 3 33 \ 3 \ 3 \ 3 \ 3
3 3 3 3 33 \ 3 \ 3 \ 3 \ 3
1 1 1 1 11 \ 1 \ 1 \ 1 \ 1

The least frequent element is 11 with a frequency of 77, and the most frequent element is 33 with a frequency of 1010.

In the third example, after applying all the operations, the matrix looks as follows:

_ _ _ 2 2 2\_ \ \_ \ \_ \ 2 \ 2 \ 2
_ _ _ 2 2 2\_ \ \_ \ \_ \ 2 \ 2 \ 2
2 2 2 2 2 22 \ 2 \ 2 \ 2 \ 2 \ 2
2 2 2 2 2 22 \ 2 \ 2 \ 2 \ 2 \ 2
4 4 4 4 4 44 \ 4 \ 4 \ 4 \ 4 \ 4
4 4 4 4 4 44 \ 4 \ 4 \ 4 \ 4 \ 4

The least frequent element is 44 with a frequency of 1212 and the most frequent element is 22 with a frequency of 1818.

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