Codeforces Round 997 (Div. 2) |
---|
Finished |
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:
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.
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.
For each test case, output a single integer representing the perimeter of the colored shape on the paper.
34 31 12 22 11 21 21 16 73 61 13 16 65 46 1
32 8 96
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:
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:
From the diagram, it can be seen that this shape has a perimeter of $$$32$$$.
Name |
---|