【CF 1027B】Numbers on the Chessboard

                            B. 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 following way: the first ⌈n22⌉⌈n22⌉numbers from 11 to ⌈n22⌉⌈n22⌉ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2−⌈n22⌉n2−⌈n22⌉ numbers from ⌈n22⌉+1⌈n22⌉+1 to n2n2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation ⌈xy⌉⌈xy⌉ 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,yixi,yi. The answer to the ii-th query is the number written in the cell xi,yixi,yi (xixi is the row, yiyi is the column). Rows and columns are numbered from 11 to nn.

Input

The first line contains two integers nn and qq (1≤n≤1091≤n≤109, 1≤q≤1051≤q≤105) — 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,yixi,yi (1≤xi,yi≤n1≤xi,yi≤n) — description of the ii-th query.

Output

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,yixi,yi (xixi is the row, yiyi is the column). Rows and columns are numbered from 11 to nn. Queries are numbered from 11 to qq 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+y如果是偶数就输出这是第几个偶数,如果是奇数就输出这是第几个奇数+最后一个格子的数字。

规律在于对n的奇偶性和x+y的奇偶性判定

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))

int main()
{
	ll n,m,i;
	cin>>n>>m;
	while(m--)
	{
		ll x,y,num;
		cin>>x>>y;
		if(n%2==0)
		{
			num=(x-1)*(n/2)+((y+1)/2);
			if((x+y)%2==0)
				cout<<num<<endl;
			else
				cout<<num+n*n/2<<endl;
		}
		else
		{
			num=((x-1)*n+y+1)/2;
			if((x+y)%2==0)
				cout<<num<<endl;
			else
				cout<<num+n*n/2+1<<endl;
		}
	}
	return 0;
}

 

CF的ID终于有颜色了,离红名dalao又进了一步(ಡωಡ)

全部评论

相关推荐

关于我大学本科四年,想了很多,但还是不知道该怎么动笔&nbsp;“大学四年,是我从懵懂少年走向职场青年的转折期。这一路跌跌撞撞,有迷茫,有遗憾,也有成长和决心。”&nbsp;大一刚进来时仍然有高中那股学习劲,经常一个人去图书馆学高等数学,但后面劲头一过便开始在宿舍开启躺平生活(现在想想那段时间真的很爽,无忧无虑)。由于大一担任班干部,所以经常要跟其他班的班干部交流,在此期间认识了隔壁班的一位女生,短发而很可爱,因为很多团建还有比赛都是我们两班一起参加的,而且我和她都是负责人,所以交集很多,后面慢慢地彼此对产生了好感,所以在大一刚开学的2个月后,我们在一起了,彼此之前都是初恋。但当时我真的是太太太直男了,对感情的想...
真烦好烦真烦:骗哥们可以,别把你自己也骗到了就行。哥们被你骗了真无所谓的,打个哈哈就过了。但希望你打完这段话后擦一下眼角,别让眼泪掉在手机屏幕上了就行。你说的这些话,哥们信一下也是没什么的。还能让你有个心里安慰,但这种话说出来骗骗兄弟就差不多得了,哥们信你一下也不会少块肉,但是你别搞得自己也当真了就行。哥们被你骗一下是真无所谓的,兄弟笑笑也就过去了。真不是哥们想要破你防,你擦擦眼泪好好想想,除了兄弟谁还会信你这些话?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
葬爱~冷少:我当时都是上午刷力扣,下午背八股,有活给我先别急,没活就干自己的事情
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务