Account
GitHubFeedback
Drafts and results saved on this device
Submit on CSES.fi
CSES Problemset/Introductory Problems

Number Spiral

#1071
Ask AI

Open a hint-first prompt in a new AI chat. Grok may ask you to confirm before sending.

Open Claude Desktop

Time limit:1.00 s
Memory limit:512 MB
Unsolved
Download Tests (.zip)

A number spiral is an infinite grid whose upper-left square has number 1. Here are the first five layers of the spiral:

Your task is to find out the number in row yy and column xx.

Input

The first input line contains an integer tt: the number of tests.

After this, there are tt lines, each containing integers yy and xx.

Output

For each test, print the number in row yy and column xx.

Constraints
  • 1≤t≤1051 \le t \le 10^5
  • 1≤y,x≤1091 \le y,x \le 10^9
Sample Test Cases (1)
Example 1ID: sample-1
Input
3
2 3
1 1
4 2
Expected Output
8
1
15
Tools
1
2
3
4
5
6
7
8
9
10
11
12
Draft saved locally
|1 line, 0 chars
Test Bench
Download .zip
Input (stdin)
3
2 3
1 1
4 2
Expected Output
8
1
15