CSES - Datatähti 2022 alku - Results
Submission details
Task:Spiraali (Spiral)
Sender:mbezirgan
Submission time:2021-10-14 19:58:05
Language:C++17
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED15
#2ACCEPTED20
#3ACCEPTED65
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1details
#2ACCEPTED0.01 s2details
#3ACCEPTED0.01 s3details

Code

#include <iostream>
#include <cmath>

struct ULong2
{
	uint64_t x, y;
};

uint64_t GetSpiralValue(uint64_t size, const ULong2& pos)
{
	// Distance form center for each axis
	ULong2 distance =
	{
		pos.x >= size / 2 ? pos.x - size / 2 : size / 2 - pos.x - 1,
		pos.y >= size / 2 ? pos.y - size / 2 : size / 2 - pos.y - 1
	};
	// Maximum distance from the center tells witch ring it is.
	// Maybe can be optimized, but no.
	uint64_t ringFromCenter = std::max(distance.x, distance.y);
	uint64_t 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 + size * size - (2 * ringFromCenter + 2)*(2 * ringFromCenter + 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, { (uint64_t)x, (uint64_t)y }) << " ";
	//	}
	//	std::cout << "\n";
	//}
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10 100
1 1
1 2
1 3
1 4
...

correct output
1
36
35
34
33
...

user output
1
36
35
34
33
...

Test 2

Group: 2

Verdict: ACCEPTED

input
1000 1000
371 263
915 322
946 880
53 738
...

correct output
773533
312166
206053
200080
593922
...

user output
773533
312166
206053
200080
593922
...

Test 3

Group: 3

Verdict: ACCEPTED

input
1000000000 1000
177757853 827347032
409613589 419171337
739269360 256524697
328695530 896842209
...

correct output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...

user output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...