CSES - Datatähti 2022 alku - Results
Submission details
Task:Spiraali
Sender:mbezirgan
Submission time:2021-10-04 22:47:16 +0300
Language:C++17
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.01 s1details
#20.01 s2details
#3--3details

Compiler report

input/code.cpp: In function 'uint64_t GetSpiralValue(int, const Int2&)':
input/code.cpp:30:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (uint64_t i = 0; i < ringInToCenter; i++)
                       ~~^~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <cmath>

struct Int2
{
	int x, y;
};

uint64_t GetSpiralValue(int size, const Int2& pos)
{
	// Position relative to the center
	Int2 positionRel = 
	{ 
		pos.x >= size / 2 ? pos.x - size / 2 : pos.x - size / 2 + 1, 
		pos.y >= size / 2 ? pos.y - size / 2 : pos.y - size / 2 + 1
	};
	// Distance form center for each axis
	Int2 distance =
	{
		std::abs(positionRel.x),
		std::abs(positionRel.y)
	};
	// 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). Set to 0 as distanceInRing is 1 higher then it should be
	uint64_t ringStartPoint = 1;
	for (uint64_t i = 0; i < ringInToCenter; i++)
	{
		//ringStartPoint += 2 * (size - 2 * i) + 2 * ((size - 2) - 2 * i);
		ringStartPoint += -8 * i + 4 * (uint64_t)size - 4;
	}

	uint64_t distanceInRing;

	// Function like this will be used in every single one derived
	// distanceInRing += 2 * (size - 2 * ringInToCenter) + 2 * ((size - 2) - 2 * ringInToCenter) - (uint64_t)(2*ringFromCenter + positionRel.y);

	if (positionRel.x < 0 && positionRel.y < 0 && positionRel.x == positionRel.y)
	{
		return ringStartPoint;
	}
	else if (distance.x == ringFromCenter)
	{
		if (positionRel.x < 0)
		{
			if (ringFromCenter == 0)
				distanceInRing = 1;
			else
				distanceInRing = (uint64_t)(2 * ringInToCenter + positionRel.y);
		}
		else
		{
			if (ringFromCenter == 0)
				distanceInRing = std::abs(positionRel.y) + 2;
			else
				distanceInRing = -6 * (uint64_t)ringInToCenter + 3 * (uint64_t)size - 2 - (uint64_t)(2 * ringFromCenter + positionRel.y);
		}
	}
	else
	{
		if (positionRel.y < 0)
			distanceInRing = (uint64_t)size - 2 * (uint64_t)ringInToCenter + (uint64_t)(2 * ringFromCenter + positionRel.x);
		else
			distanceInRing = -8 * (uint64_t)ringInToCenter + 4 * (uint64_t)size - 4 - (uint64_t)(2 * ringFromCenter + positionRel.x);
	}

	return ringStartPoint + distanceInRing;
}

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

	for (int i = 0; i < t; i++)
	{
		int 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;
}

Test details

Test 1

Group: 1

Verdict:

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

correct output
1
36
35
34
33
...

user output
1
16
17
18
19
...

Test 2

Group: 2

Verdict:

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

correct output
773533
312166
206053
200080
593922
...

user output
773820
313767
206632
199212
594860
...

Test 3

Group: 3

Verdict:

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

correct output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...

user output
(empty)