Problem Description

You are given a chessboard of size n×n. It is filled with numbers from 1 to n^2 in the following way: the first ⌈n^2/2⌉ numbers from 1 to ⌈n^2/2⌉ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n^2−⌈n^2/2⌉ numbers from ⌈n^2/2⌉+1 to n^2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation ⌈x/y⌉ means division x by y rounded up.

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

You are given q queries. The i-th query is described as a pair xi,yixi,yi. The answer to the i-th query is the number written in the cellxi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n.

Input

The first line contains two integers n and q (1≤n≤109, 1≤q≤105) — the size of the board and the number of queries.

The next q lines contain two integers each. The i-th line contains two integers xi,yi (1≤xi,yi≤n) — description of the i-th query.

Output

For each query from 1 to q print the answer to this query. The answer to the i-th query is the number written in the cell xi,yi (xixi is the row, yi is the column). Rows and columns are numbered from 1 to n. Queries are numbered from 1 to q in order of the input.

Examples

Input

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

Output

1
8
16
13
4

Input

5 4
2 1
4 2
3 3
3 4

Output

16
9
7
20

题意:以上图的方式填充一个 n*n 大小的格子,查找询问位置点的数字大小

思路:通过存数过程可以知道,第一轮放的横纵坐标和为偶数,第二轮放的坐标和为奇数,根据规律可以知道第一轮放的数满足(x-1)*n+y+1 ,则第二轮存放的数量需要加上 n*n,要注意的是,由于是以一、二轮讨论存放情况的,因此每一轮的每一行只有 n/2 个数,所以最后结果需要除以2

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1001
#define MOD 1e9+7
#define E 1e-6
#define LL long long
using namespace std;
int main()
{LL n,t;cin>>n>>t;while(t--){LL x,y;cin>>x>>y;LL ans=(x-1)*n+y+1;if((x+y)%2)ans+=n*n;cout<<ans/2<<endl;}return 0;
}

Numbers on the Chessboard(CF-1027B)相关推荐

  1. Knights in Chessboard (找规律)

    Knights in Chessboard (找规律) LightOJ-1010 解题思路: 如果只有一行.一列, 放满: 如果只有两行,每隔一个田字格可以放一个田字格: 其他情况可全放白格子或全放黑 ...

  2. 【解题报告】随便练练二(CF 2300)

    [解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...

  3. Commentator problem(CF 2)

    题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...

  4. 【CodeForces - 1027B 】Numbers on the Chessboard (没有营养的找规律题,无聊题)

    题干: You are given a chessboard of size n×nn×n. It is filled with numbers from 11 to n2n2 in the foll ...

  5. codeforces 1027 B - Numbers on the Chessboard(规律)

    给一个n*n的数字矩阵 然后1到n^2/2的就从左到右 从上到下依次放在行列加起来是偶数的格子里 然后n^2/2+1的就放在奇数格子里 然后询问某个格子的数是多少 其实就是根据n是奇数还是偶数 还有询 ...

  6. D. Make a Power of Two(cf#739DIV3)

    D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...

  7. Web of Lies(CF 1548A)

    这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...

  8. Magic Powder - 2 (CF 670_D)

    http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...

  9. 【解题报告】博弈专场 (CF 2000~2200)前五题

    [解题报告]博弈专场 (CF 2000+)前五题 A:Fox and Card Game | CF388C 题意 思路 代码 B:Berzerk | CF786A 题意 思路 代码 C:Ithea P ...

最新文章

  1. centos memcached php,centos系统为php安装memcached扩展步骤
  2. 利用System.EventHandler来实现两个窗体间的事件调用
  3. C和C++的区别 04.函数重载
  4. java eclipse 内存_java – Eclipse 3.5.1使用大量内存
  5. SQL之rand,round,floor,ceiling,cast小数处理函数
  6. 一行代码取出HTML页面某个按钮的css属性,比如margin
  7. Angular input控件的click事件响应处理的调用上下文
  8. 线程的属性 —— 分离的状态(detached state)、栈地址(stack address)、栈大小(stack size)
  9. java xml setdoctype_如何在Java中使用DOM将自定义doctype标记添加到带有xhtml标记的xml中?...
  10. 汉游天下公司的一些感悟
  11. volatile关键字与synchronization关键字的区别?
  12. chrome不能安装adblock插件
  13. 国家语言代码大全【方便翻译查询】
  14. win7找不到网络共享计算机,电脑访问网络共享提示“找不到网络路径”怎么办...
  15. c语言---16 关于goto语句
  16. 二十世纪初的心理学时间轴制作
  17. Creo建模(持续更新)
  18. 谈判技巧——准备阶段
  19. Linux中正则表达式详细命令及代码(附实验笔记)
  20. java .vm_java VM

热门文章

  1. vivo真实案例:中台到底解决了什么问题?
  2. 为什么数字中台是企业应用新基建?
  3. 学完计组后,我马上在「我的世界」造了台显示器,你敢信?
  4. 以 B 站为例,聊聊站内消息系统的设计
  5. 一文彻底掌握二叉查找树
  6. 99%的程序员都在用Lombok,原理竟然这么简单?我也手撸了一个!
  7. 面试官扎心一问:Tomcat 在 SpringBoot 中是如何启动的?
  8. 妹子每天加班1分钟,半年崩溃!向公司索赔55万!
  9. 原生开发小程序 和 wepy 、 mpvue 对比
  10. Dreamweaver中如何格式化代码