CF354D.Transferring Pyramid

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

The pyramid consists of nn rows, the ii -th row contains ii 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 as shown on the picture below.

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

This data structure can perform operations of two types:

  1. Change the value of a specific cell. It is described by three integers: "tiv""t i v" , where t=1t=1 (the type of operation), ii — the number of the cell to change and vv 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 55 . It is described by s+2s+2 numbers: "tiv1v2...vs""t i v_{1} v_{2} ... v_{s}" , where t=2t=2 , ii — the number of the top cell of the pyramid, ss — the size of the subpyramid (the number of cells it has), vjv_{j} — the value you should assign to the jj -th cell of the subpyramid.

Formally: a subpyramid with top at the ii -th cell of the kk -th row (the 55 -th cell is the second cell of the third row) will contain cells from rows from kk to nn , the (k+p)(k+p) -th row contains cells from the ii -th to the (i+p)(i+p) -th ( 0<=p<=nk0<=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 nn rows with kk changed cells. Find the sequence of operations which result in each of the kk changed cells being changed by at least one operation. Among all the possible sequences pick the one that contains the fewest numbers.

输入格式

The first line contains two integers nn and kk ( 1<=n,k<=1051<=n,k<=10^{5} ).

The next kk lines contain the coordinates of the modified cells rir_{i} and cic_{i} (1<=ci<=ri<=n)(1<=c_{i}<=r_{i}<=n) — the row and the cell's number in the row. All cells are distinct.

输出格式

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

输入输出样例

  • 输入#1

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

    输出#1

    10
    
  • 输入#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
    

    输出#2

    26
    

说明/提示

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

24v4v7v82 4 v_{4} v_{7} v_{8}

26v6v9v102 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:

首页