QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#16569. Coloring

统计

Little C is coloring an $n \times m$ grid with colored pencils. Initially, all cells are blank.

He performs $q$ coloring operations. Each operation involves selecting either a row or a column and adding $1$ layer of color to all cells in that row or column.

Little C likes light colors, so after each operation, he erases the color from all cells that have reached $k$ layers of color, making those cells blank again.

Little C wants to know how many cells have color on them at the end.

Input

The first line contains four integers $n, m, q, k$.

The next $q$ lines each contain two integers $op, x$.

  • If $op=1$, it means adding $1$ layer of color to all cells in row $x$.
  • If $op=2$, it means adding $1$ layer of color to all cells in column $x$.

Output

Output a single integer representing the total number of cells that have color on them at the end.

Examples

Input 1

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

Output 1

8

Note 1

The cell at row $1$, column $1$ has no color. The cell at row $1$, column $2$ has $1$ layer of color. The cell at row $1$, column $3$ has no color. The cell at row $1$, column $4$ has $1$ layer of color.

The cell at row $2$, column $1$ has $1$ layer of color. The cell at row $2$, column $2$ has $2$ layers of color. The cell at row $2$, column $3$ has $1$ layer of color. The cell at row $2$, column $4$ has $2$ layers of color.

The cell at row $3$, column $1$ has $2$ layers of color. The color of the cell at row $3$, column $2$ is erased. The cell at row $3$, column $3$ has $2$ layers of color. The color of the cell at row $3$, column $4$ is also erased.

In the end, there are $8$ cells with color.

Examples 2-4

See paint/paint2.in and paint/paint2.ans, paint/paint3.in and paint/paint3.ans, and paint/paint4.in and paint/paint4.ans in the additional files. These examples satisfy the constraints of test cases $1$, $5$, and $20$ respectively.

Constraints

For $100\%$ of the data, $1 \le n, m \le 2 \times 10^5$, $1 \le k \le q \le 5 \times 10^5$, $op \in \{1, 2\}$. It is guaranteed that when $op=1$, $1 \le x \le n$, and when $op=2$, $1 \le x \le m$.

Test Case ID $n, m \le$ $q \le$ Special Property
$1\sim4$ $3000$ $3000$ None
$5\sim9$ $3000$ $5\times10^5$ None
$10\sim12$ $2\times10^5$ $5\times10^5$ A
$13\sim16$ $2\times10^5$ $5\times10^5$ B
$17\sim20$ $2\times10^5$ $5\times10^5$ None

Special Property A: Guaranteed $op=1$.

Special Property B: Guaranteed $k=2$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.