Codeforces Round 997 (Div. 2)
A
Shape Perimeter
Codeforces
800
原题
←B →
翻译中…

There is an $m$ by $m$ square stamp on an infinite piece of paper. Initially, the bottom-left corner of the square stamp is aligned with the bottom-left corner of the paper. You are given two integer sequences $x$ and $y$, each of length $n$. For each step $i$ from $1$ to $n$, the following happens:

翻译中…
  • Move the stamp $x_i$ units to the right and $y_i$ units upwards.
  • Press the stamp onto the paper, leaving an $m$ by $m$ colored square at its current position.
翻译中…

Note that the elements of sequences $x$ and $y$ have a special constraint: $1\le x_i, y_i\le m - 1$.

翻译中…

Note that you do not press the stamp at the bottom-left corner of the paper. Refer to the notes section for better understanding.

翻译中…

It can be proven that after all the operations, the colored shape on the paper formed by the stamp is a single connected region. Find the perimeter of this colored shape.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $2 \le m \le 100$) — the number of operations performed and the side length of the square stamp.

翻译中…

The $i$-th of the next $n$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le m - 1$) — the distance that the stamp will be moved right and up during the $i$-th operation, respectively.

翻译中…

Note that there are no constraints on the sum of $n$ over all test cases.

翻译中…

Output

翻译中…

For each test case, output a single integer representing the perimeter of the colored shape on the paper.

翻译中…

Note

翻译中…

In the first example, the stamp has a side length of $3$ and is pressed $4$ times at coordinates $(1, 1)$, $(3, 3)$, $(5, 4)$, and $(6, 6)$. The piece of paper looks like that afterwards:

翻译中…

image

翻译中…

Here, the square formed by the first press is colored blue, the second red, the third green, and the fourth purple. The combined shape, whose perimeter we need to calculate, looks like that:

翻译中…

image

翻译中…

From the diagram, it can be seen that this shape has a perimeter of $32$.

翻译中…

样例

Input #1
3
4 3
1 1
2 2
2 1
1 2
1 2
1 1
6 7
3 6
1 1
3 1
6 6
5 4
6 1
Output #1
32
8
96
InputCopy OutputCopy
题目信息
题号
2056A
时间限制
1 s
内存限制
256 MB
标签
构造
数学

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