forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(CSES)NumberSpiral.cpp
103 lines (77 loc) · 1.59 KB
/
(CSES)NumberSpiral.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
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 y and column x.
Input
The first input line contains an integer t: the number of tests.
After this, there are t lines, each containing integers y and x.
Output
For each test, print the number in row y and column x.
Constraints
1≤t≤105
1≤y,x≤109
Example
Input:
3
2 3
1 1
4 2
Output:
8
1
15
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<unordered_map>
#include<queue>
#include<map>
#include<math.h>
#include<set>
#include<stack>
using namespace std;
#define ll long long
//set<string> st;
/*int ans=0;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
char c[4] = {'U','D','L','R'};
int vis[7][7];
*/
/*static bool cmp(pair<int,int> &a,pair<int,int> &b) {
return a.first<b.first;
} */
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--) {
ll r,c;
cin>>r>>c;
ll n,s,x;
if(r>c)
n = r;
else
n = c;
s = n*n;
if(n%2) {
if(r==n) {
x = (s-n+1)-(n-c);
}
else
x = (s-r+1);
}
else {
if(r==n) {
x = (s-c+1);
}
else {
x = (s-n+1)-(n-r);
}
}
cout<<x<<"\n";
}
return 0;
}