CF BUDDY
← Problems·

1027B · Numbers on the Chessboard

1200 · implementation, math

Problem: You are given a chessboard of size n×nn \times n. It is filled with numbers from 11 to n2n^2 in the following way: the first n22\lceil \frac{n^2}{2} \rceil numbers from 11 to n22\lceil \frac{n^2}{2} \rceil are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2n22n^2 - \lceil \frac{n^2}{2} \rceil numbers from n22+1\lceil \frac{n^2}{2} \rceil + 1 to n2n^2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation xy\lceil\frac{x}{y}\rceil means division xx by yy rounded up.

For example, the left board on the following picture is the chessboard which is given for n=4n=4 and the right board is the chessboard which is given for n=5n=5.

You are given qq queries. The ii-th query is described as a pair xi,yix_i, y_i. The answer to the ii-th query is the number written in the cell xi,yix_i, y_i (xix_i is the row, yiy_i is the column). Rows and columns are numbered from 11 to nn.

Input Format: The first line contains two integers nn and qq (1n1091 \le n \le 10^9, 1q1051 \le q \le 10^5) — the size of the board and the number of queries.

The next qq lines contain two integers each. The ii-th line contains two integers xi,yix_i, y_i (1xi,yin1 \le x_i, y_i \le n) — description of the ii-th query.

Output Format: For each query from 11 to qq print the answer to this query. The answer to the ii-th query is the number written in the cell xi,yix_i, y_i (xix_i is the row, yiy_i is the column). Rows and columns are numbered from 11 to nn. Queries are numbered from 11 to qq in order of the input.

Note: Answers to the queries from examples are on the board in the picture from the problem statement.

Sample Cases

Case 1

Input

4 5
1 1
4 4
4 3
3 2
2 4

Output

1
8
16
13
4

Case 2

Input

5 4
2 1
4 2
3 3
3 4

Output

16
9
7
20

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback