Codeforces Round 206 (Div. 1)
D
Transferring Pyramid
Codeforces
2900
原题
← CE →
翻译中…

Vasya and Petya are using an interesting data storing structure: a pyramid.

翻译中…

The pyramid consists of $*n*$ rows, the $*i*$-th row contains $*i*$ cells. Each row is shifted half a cell to the left relative to the previous row. The cells are numbered by integers from 1 to image as shown on the picture below.

翻译中…

An example of a pyramid at $*n* = 5$ is:

翻译中…

image

翻译中…

This data structure can perform operations of two types:

翻译中…
  1. Change the value of a specific cell. It is described by three integers: $"*t* *i* *v*"$, where $*t* = 1$ (the type of operation), $*i*$ — the number of the cell to change and $*v*$ the value to assign to the cell.
  2. Change the value of some subpyramid. The picture shows a highlighted subpyramid with the top in cell $5$. It is described by $*s* + 2$ numbers: $"*t* *i* *v*_{1} *v*_{2} ... *v*_{*s*}"$, where $*t* = 2$, $*i*$ — the number of the top cell of the pyramid, $*s*$ — the size of the subpyramid (the number of cells it has), $*v*_{*j*}$ — the value you should assign to the $*j*$-th cell of the subpyramid.
翻译中…

Formally: a subpyramid with top at the $*i*$-th cell of the $*k*$-th row (the $5$-th cell is the second cell of the third row) will contain cells from rows from $*k*$ to $*n*$, the $(*k* + *p*)$-th row contains cells from the $*i*$-th to the $(*i* + *p*)$-th ($0 ≤ *p* ≤ *n* - *k*$).

翻译中…

Vasya and Petya had two identical pyramids. Vasya changed some cells in his pyramid and he now wants to send his changes to Petya. For that, he wants to find a sequence of operations at which Petya can repeat all Vasya's changes. Among all possible sequences, Vasya has to pick the minimum one (the one that contains the fewest numbers).

翻译中…

You have a pyramid of $*n*$ rows with $*k*$ changed cells. Find the sequence of operations which result in each of the $*k*$ changed cells being changed by at least one operation. Among all the possible sequences pick the one that contains the fewest numbers.

翻译中…

Input

翻译中…

The first line contains two integers $*n*$ and $*k*$ ($1 ≤ *n*, *k* ≤ 10^{5}$).

翻译中…

The next $*k*$ lines contain the coordinates of the modified cells $*r*_{*i*}$ and $*c*_{*i*}$ $(1 ≤ *c*_{*i*} ≤ *r*_{*i*} ≤ *n*)$ — the row and the cell's number in the row. All cells are distinct.

翻译中…

Output

翻译中…

Print a single number showing how many numbers the final sequence has.

翻译中…

Note

翻译中…

One of the possible solutions of the first sample consists of two operations:

翻译中…

$2 4 *v*_{4} *v*_{7} *v*_{8}$

翻译中…

$2 6 *v*_{6} *v*_{9} *v*_{10}$

翻译中…

The picture shows the changed cells color-highlighted. The subpyramid used by the first operation is highlighted blue and the subpyramid used by the first operation is highlighted yellow:

翻译中…

image

翻译中…

样例

Input #1
4 5
3 1
3 3
4 1
4 3
4 4
Output #1
10
InputCopy OutputCopy
Input #2
7 11
2 2
3 1
4 3
5 1
5 2
5 5
6 4
7 2
7 3
7 4
7 5
Output #2
26
InputCopy OutputCopy
题目信息
题号
354D
时间限制
3 s
内存限制
256 MB
标签
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。