欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF2056A.Shape Perimeter
Shape Perimeter
There is an by 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 and , each of length . For each step from to , the following happens:
- Move the stamp units to the right and units upwards.
- Press the stamp onto the paper, leaving an by colored square at its current position.
Note that the elements of sequences and have a special constraint: .
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 (). The description of the test cases follows.
The first line of each test case contains two integers and (, ) — the number of operations performed and the side length of the square stamp.
The -th of the next lines contains two integers and () — the distance that the stamp will be moved right and up during the -th operation, respectively.
Note that there are no constraints on the sum of 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 and is pressed times at coordinates , , , and . 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 .
Samples
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
32
8
96
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |