CSES Solutions
#1071Introductory Problems

Number Spiral

View on CSES

Solution

1071-Number_Spiral.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4signed main(){
5  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
6  int t; cin>>t;
7  while(t--){
8    int x,y; cin>>x>>y;
9    cout<<(x>y?x&1?(x-1)*(x-1)+y:x*x-y+1:y&1?y*y-x+1:(y-1)*(y-1)+x)<<'\n';
10  }
11  return 0;
12}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.