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×M, and only one bishop in a cell (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 T (1 ≤ T ≤ 1000) denoting the number of test cases.

On the next T lines, there will be four integers N, M, x, y as described above. Here N (1 ≤ N ≤ 1012) is the number of columns and M (1 ≤ M ≤ 1012) is the number of rows. The bishop is at column x (1 ≤ x ≤ N), row y (1 ≤ 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.


80% Solution Ratio

kunobyangEarliest, Dec '16

Nasif_44thFastest, 0.0s

kunobyangLightest, 131 kB

SIR.24Shortest, 275B


Login to submit