Crossmarket
Contest: Codeforces Round 816 (Div. 2)
Difficulty rating: 800
Tags: constructive algorithms, greedy, math

Stanley and Megan decided to shop in the "Crossmarket" grocery store, which can be represented as a matrix with $$$n$$$ rows and $$$m$$$ columns.

Stanley and Megan can move to an adjacent cell using $$$1$$$ unit of power. Two cells are considered adjacent if they share an edge. To speed up the shopping process, Megan brought her portals with her, and she leaves one in each cell she visits (if there is no portal yet). If a person (Stanley or Megan) is in a cell with a portal, that person can use $$$1$$$ unit of power to teleport to any other cell with a portal, including Megan's starting cell.

They decided to split up: Stanley will go from the upper-left cell (cell with coordinates $$$(1, 1)$$$) to the lower-right cell (cell with coordinates $$$(n, m)$$$), whilst Megan needs to get from the lower-left cell (cell with coordinates $$$(n, 1)$$$) to the upper-right cell (cell with coordinates $$$(1, m)$$$).

What is the minimum total energy needed for them both to do that?

Note that they can choose the time they move. Time does not affect energy.

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

The only line in the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$).

Output format:
For each test case print a single integer on a new line – the answer.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case they can stick to the following plan:

1. Megan (red circle) moves to the cell $$$(7, 3)$$$. Then she goes to the cell $$$(1, 3)$$$, and Stanley (blue circle) does the same.
2. Stanley uses the portal in that cell (cells with portals are grey) to get to the cell $$$(7, 3)$$$. Then he moves to his destination — cell $$$(7, 5)$$$.
3. Megan also finishes her route and goes to the cell $$$(1, 5)$$$.

The total energy spent is $$$(2 + 6) + (2 + 1 + 2) + (2)= 15$$$, which is our final answer.

Example input:
7
7 5
5 7
1 1
100000 100000
57 228
1 5
5 1

Example output:
15
15
0
299998
340
5
5

Editorial:
Proof:
To start with, Stanley and Megan can complete their steps in any order. So, again, for our convenience let's reorder their moves in such a way, that firstly Megan finishes her route and places portals, and then Stanley does what he needs to do.
What is always the optimal route for Stanley? He goes to the nearest cell with the teleport and then teleports to the nearest possible cell to his finish.
It is obvious, that Stanley can always complete his route without going left or up (except for the teleportations).
Let's try to prove a similar statement for Megan: she can always plan her route avoiding moves left or down. These two cases are almost equivalent, so we will consider the first one.
Let's assume that teleportations are free for Megan. Consider any cell, from which she made her move to the left. Then it is possible to "shift" a segment of the route to the right in order to decrease its length at least by one in conjunction with getting rid of at least one move to the left. More formally we need to construct a new route for her, so the following conditions are met: for each cell in the previous route either itself, or her right neighbor is included in the new route. This can be done by the following algorithm: we will consider cells from the original route $$$A$$$ in order of installation of the portals, and build the new route $$$B$$$ in parallel. If we cannot include the cell $$$(x, y)$$$ from $$$A$$$ to the route $$$B$$$ because it does not have adjacent sides to any of the cells from $$$B$$$. Then we can include the cell $$$(x, y + 1)$$$ to $$$B$$$ (it cannot be out of bounds). Let there be a cell $$$(x_1, y_1)$$$ in $$$A$$$ with a portal, that was installed earlier than in our current cell, and that is adjacent to our current cell. If we could not include cell $$$(x, y)$$$ to the route $$$B$$$, that it means that we also did not include the cell $$$(x_1, y_1)$$$ there, thus cell $$$(x_1, y_1 + 1)$$$ is in $$$B$$$, that also also allows us to include cell $$$(x, y + 1)$$$.
After such an operation Stanley's energy consumption could increase at most by 1. Though energy spent by Megan decreased at least by one. That means that our new route is not more energy-consuming.
That way Megan will never go left or down, so she will spend at least $$$(n - 1) + (m - 1) = n + m - 2$$$ units of power.
If, when all the operations are applied, Stanley teleports from the cell $$$(x_1, y_1)$$$ to $$$(x_2, y_2)$$$, then $$$ \left[ \begin{array}{c} x_1 \le x_2, y_1 \ge y_2 \\ x_1 \ge x_2, y_1 \le y_2 \\ \end{array} \right. $$$. In the first case he teleports along Megan's route and approaches his finish by $$$(x_2 - x_1) - (y_1 - y2)$$$, and in the second case he teleports across Megan's route and approaches his destination by $$$(y_2 - y_1) - (x_1 - x2)$$$. As you can easily notice, first's expression maximum is $$$n - 1$$$, and second's is $$$m - 1$$$. Hence Stanley will spend at least $$$(n - 1) + (m - 1) - max(n - 1, m - 1) = n + m - max(n, m) - 1$$$ units of power. After adding only teleportation we get $$$n + m - max(n, m)$$$
We got two lower bounds, that in sum are $$$n + m - 2 + n + m - max(n, m) = 2n + 2m - max(n, m) - 2 = n + m + min(n, m) - 2$$$, which is not better, than our solution's answer, even though we did not consider Megan's teleportations.