A9239 | Transferring Pyramid
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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  as shown on the picture below.
An example of a pyramid at $n=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: $"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.
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  as shown on the picture below.
An example of a pyramid at $n=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: $"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.
输入格式
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.
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.
输出格式
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:
$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:

$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:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted