#P15351. [COCI 2025/2026 #4] 战斧牛排 / Tomahawk

[COCI 2025/2026 #4] 战斧牛排 / Tomahawk

Problem Description

Consider an n×nn\times n matrix AA. Initially, all elements in AA are zero.

Use the following conventions: rows are horizontal and columns are vertical. Rows are numbered from top to bottom as 1∼n1\sim n, and columns are numbered from left to right as 1∼n1\sim n.

There are qq operations:

  • L\texttt{L} xx: here, 1≤x≤⌊n+12⌋1\le x\le \lfloor \frac{n+1}{2}\rfloor.
    • For i=1,…,xi=1,\ldots,x, add (x−i+1)(x-i+1) to every cell in column ii.
  • R\texttt{R} xx: here, 1≤x≤⌊n+12⌋1\le x\le \lfloor \frac{n+1}{2}\rfloor.
    • For i=1,…,xi=1,\ldots,x, add (x−i+1)(x-i+1) to every cell in column (n−i+1)(n-i+1).
  • D\texttt{D} xx: here, 1≤x≤n1\le x\le n.
    • For i=1,…,xi=1,\ldots,x, add (x−i+1)(x-i+1) to every cell in row (n−i+1)(n-i+1).

After all operations, find the range of the matrix (the difference between the maximum value and the minimum value).

Input Format

The first line contains two positive integers n,qn,q (1≤n≤1091\le n\le 10^9, 1≤q≤1051\le q\le 10^5).

The next qq lines each contain a character ss and a positive integer xx, where s∈{L,R,D}s\in \{\texttt{L},\texttt{R},\texttt{D}\}.

  • If s=Ds=\texttt{D}, then 1≤x≤n1\le x\le n;
  • otherwise, 1≤x≤⌊n+12⌋1\le x\le \lfloor \frac{n+1}{2}\rfloor.

Output Format

Output one integer on a single line, representing the range (the difference between the maximum value and the minimum value).

4 2
L 2
R 1
2
3 3
R 2
D 3
R 2
6

Hint

Sample Explanation

Explanation for sample 1:

$\begin{bmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{bmatrix} \rightarrow \begin{bmatrix} 2 & 1 & 0 & 0 \\ 2 & 1 & 0 & 0 \\ 2 & 1 & 0 & 0 \\ 2 & 1 & 0 & 0 \end{bmatrix} \rightarrow \begin{bmatrix} 2 & 1 & 0 & 1 \\ 2 & 1 & 0 & 1 \\ 2 & 1 & 0 & 1 \\ 2 & 1 & 0 & 1 \end{bmatrix}$

Explanation for sample 2:

$\begin{bmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{bmatrix} \rightarrow \begin{bmatrix} 0 & 1 & 2 \\ 0 & 1 & 2 \\ 0 & 1 & 2 \end{bmatrix} \rightarrow \begin{bmatrix} 1 & 2 & 3 \\ 2 & 3 & 4 \\ 3 & 4 & 5 \end{bmatrix} \rightarrow \begin{bmatrix} 1 & 3 & 5 \\ 2 & 4 & 6 \\ 3 & 5 & 7 \end{bmatrix}$

Subtasks

Subtask ID Score Constraints
11 77 n,q≤20n,q\le 20
22 2222 n,q≤100n,q\le 100
33 3131 n≤1000n\le 1000
44 66 nn is even
55 44 No additional constraints

Translated by ChatGPT 5