CF1027B.Numbers on the Chessboard

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 n2−⌈n22⌉n^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.

给你一个 n×nn \times n 的棋盘。棋盘按如下方式填入 11 到 n2n^2 的数字:
前 ⌈n22⌉\lceil \frac{n^2}{2} \rceil 个数(即 11 到 ⌈n22⌉\lceil \frac{n^2}{2} \rceil)按从左到右、从上到下的顺序,填入所有行列坐标之和为偶数的格子中;
其余 n2−⌈n22⌉n^2 - \lceil \frac{n^2}{2} \rceil 个数(即 ⌈n22⌉+1\lceil \frac{n^2}{2} \rceil + 1 到 n2n^2)同样按从左到右、从上到下的顺序,填入所有行列坐标之和为奇数的格子中。
其中,⌈xy⌉\lceil\frac{x}{y}\rceil 表示将 xx 除以 yy 后向上取整。

例如,下图中左侧棋盘对应 n=4n=4 的情况,右侧棋盘对应 n=5n=5 的情况。

你将收到 qq 个查询。第 ii 个查询由一对坐标 (xi,yi)(x_i, y_i) 描述。第 ii 个查询的答案即为第 xix_i 行、第 yiy_i 列(行号与列号均从 11 开始编号)格子中所填的数字。

输入格式

The first line contains two integers nn and qq (1≤n≤1091 \le n \le 10^9, 1≤q≤1051 \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 (1≤xi,yi≤n1 \le x_i, y_i \le n) — description of the ii-th query.

第一行包含两个整数 nn 和 qq(1≤n≤1091 \le n \le 10^9,1≤q≤1051 \le q \le 10^5)—— 分别表示棋盘的大小和查询次数。

接下来的 qq 行每行包含两个整数。第 ii 行包含两个整数 xi,yix_i, y_i(1≤xi,yi≤n1 \le x_i, y_i \le n)—— 描述第 ii 次查询。

输出格式

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.

对于从 11 到 qq 的每个查询,请输出该查询的答案。第 ii 个查询的答案为位于第 xix_i 行、第 yiy_i 列的单元格中所写的数字(其中 xix_i 表示行号,yiy_i 表示列号)。行号与列号均从 11 编号至 nn。查询按输入顺序编号,从 11 到 qq。

输入输出样例

  • 输入#1

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

    输出#1

    1
    8
    16
    13
    4
  • 输入#2

    5 4
    2 1
    4 2
    3 3
    3 4

    输出#2

    16
    9
    7
    20

说明/提示

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

示例中查询的答案在题目描述图片中的黑板上。

输入解题思路,AI测评打分。不知道怎么写?

首页