Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Gutibaji on Chessboard

By himuhasib · Limits 1s, 512 MB

Let there be a chessboard of size N×MN \times M, and only one bishop in a cell (x,y)(x, y). How many cells can the bishop can attack in one move? You can assume that one possible move is staying in the current position.


In the first line of input, there will be a number TT (1T10001 ≤ T ≤ 1000) denoting the number of test cases.

On the next TT lines, there will be four integers NN, MM, xx, yy as described above. Here NN (1N10121 ≤ N ≤ 10^{12}) is the number of columns and MM (1M10121 ≤ M ≤ 10^{12}) is the number of rows. The bishop is at column xx (1xN1 ≤ x ≤ N), row yy (1yM1 ≤ y ≤ M).


For each test case print a single line with the number of cells the bishop can attack with one move.


8 6 2 4
8 6 1 1

The image above is a visualization of the first case.



79% Solution Ratio

ruhanhabib39Earliest, Dec '16

abdullah2222Fastest, 0.0s

ruhanhabib39Lightest, 131 kB

mdvirusShortest, 125B


Login to submit


The bishop can go to 4 directions (diagonally). The number of cells in the upper-left direction (the...

Toph uses cookies. By continuing you agree to our Cookie Policy.