Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int , int> ii;
- #define SQ(a) (a) * (a)
- #define S second
- #define F first
- ii getCord(int x)
- {
- int row = ceil(sqrt(x));
- int mnRow = SQ(row - 1) + 1;
- int mxRow = SQ(row);
- int mdRow = (mxRow + mnRow) / 2;
- int col = x - mdRow;
- return ii(row ,col);
- }
- bool canDown(int x)
- {
- int row = ceil(sqrt(x));
- int col = x - SQ(row - 1);
- return col % 2;
- }
- int getAns(ii a ,ii b ,int A ,int B)
- {
- int colDiff = abs(b.S - a.S);
- int rowDiff = abs(b.F - a.F);
- int ans = rowDiff + colDiff;
- if( colDiff <= rowDiff )
- {
- ans = 2 * rowDiff;
- if( !canDown(A) ) ans += 1;
- if( !canDown(B) ) ans -= 1;
- }
- return ans;
- }
- void Solve()
- {
- int a; scanf("%d" ,&a);
- int b; scanf("%d" ,&b);
- if( a > b ) swap(a , b);
- printf("%d\n" ,getAns(getCord(a) ,getCord(b) ,a ,b));
- }
- int main()
- {
- int Tc;
- scanf("%d" ,&Tc);
- bool f = true;
- while( Tc-- )
- {
- if( !f ) puts("");
- f = false;
- Solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement