CSES - Datatähti 2022 alku - Results
Submission details
Task:Spiraali
Sender:mbezirgan
Submission time:2021-10-14 19:50:26 +0300
Language:C++17
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'uint64_t GetSpiralValue(int, const ULong2&)':
input/code.cpp:14:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   std::abs(pos.x >= size / 2 ? pos.x - size / 2 : pos.x - size / 2 + 1),
            ~~~~~~^~~~~~~~~~~
input/code.cpp:14:71: error: call of overloaded 'abs(long unsigned int)' is ambiguous
   std::abs(pos.x >= size / 2 ? pos.x - size / 2 : pos.x - size / 2 + 1),
                                                                       ^
In file included from /usr/include/c++/7/cstdlib:77:0,
                 from /usr/include/c++/7/ext/string_conversions.h:41,
                 from /usr/include/c++/7/bits/basic_string.h:6361,
                 from /usr/include/c++/7/string:52,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,

Code

#include <iostream>
#include <cmath>

struct ULong2
{
	uint64_t x, y;
};

uint64_t GetSpiralValue(int size, const ULong2& pos)
{
	// Distance form center for each axis
	ULong2 distance =
	{
		std::abs(pos.x >= size / 2 ? pos.x - size / 2 : pos.x - size / 2 + 1),
		std::abs(pos.y >= size / 2 ? pos.y - size / 2 : pos.y - size / 2 + 1)
	};
	// Maximum distance from the center tells witch ring it is.
	// Maybe can be optimized, but no.
	int ringFromCenter = std::max(distance.x, distance.y);
	int ringInToCenter = (ringFromCenter <= size / 2 ? size / 2 - ringFromCenter : (size / 2 + 1) - ringFromCenter) - 1;

	// Can be optimized as its a linear sum (I think).
	uint64_t ringStartPoint = 1 + (uint64_t)size* (uint64_t)size - std::pow(2 * ringFromCenter + 2, 2);

	ULong2 RelativePos =
	{
		pos.x - ringInToCenter,
		pos.y - ringInToCenter,
	};

	if (RelativePos.x == 0 && RelativePos.y == 0)
		return ringStartPoint;
	if (RelativePos.x == 0)
		return ringStartPoint + RelativePos.y;
	if (RelativePos.y == 2 * ringFromCenter + 1)
		return ringStartPoint + RelativePos.x + 2 * ringFromCenter + 1;
	if (RelativePos.x == 2 * ringFromCenter + 1)
		return ringStartPoint - RelativePos.y + 2*(2 * ringFromCenter + 2) + (2 * ringFromCenter) - 1;
	if (RelativePos.y == 0)
		return ringStartPoint - RelativePos.x + 2 * (2 * ringFromCenter + 2) + 2 * (2 * ringFromCenter);

	return 0;
}

int main()
{
	int n, t;
	std::cin >> n;
	std::cin >> t;
	
	ULong2* positions = new ULong2[t];

	for (int i = 0; i < t; i++)
	{
		uint64_t x, y;
		std::cin >> x;
		std::cin >> y;

		positions[i] = { y - 1, x - 1 };
	}

	for (int i = 0; i < t; i++)
	{
		auto& pos = positions[i];

		std::cout << GetSpiralValue(n, pos) << "\n";
	}

	delete[] positions;
	//for (int y = 0; y < n; y++)
	//{
	//	for (int x = 0; x < n; x++)
	//	{
	//		std::cout << GetSpiralValue(n, { x, y }) << " ";
	//	}
	//	std::cout << "\n";
	//}
}