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

Code

#include <iostream>

int main()
{
	int n, t;
	std::cin >> n;
	std::cin >> t;

	int* spiral = new int[n * n]();

	// Generates the spiral
	{
		int x = 0, y = 0;
		char direction = 0;

		for (int i = 1; i <= n * n; i++)
		{
			spiral[y * n + x] = i;

			if (direction == 0)
			{
				y++;
				if (y == n || spiral[y * n + x] != 0)
				{
					direction = 1;
					y--;
					x++;
				}
			}
			else if (direction == 1)
			{
				x++;
				if (x == n || spiral[y * n + x] != 0)
				{
					direction = 2;
					y--;
					x--;
				}
			}
			else if (direction == 2)
			{
				y--;
				if (y < 0 || spiral[y * n + x] != 0)
				{
					direction = 3;
					y++;
					x--;
				}
			}
			else if (direction == 3)
			{
				x--;
				if (x < 0 || spiral[y * n + x] != 0)
				{
					direction = 0;
					y++;
					x++;
				}
			}
		}
	}
	
	struct Int2
	{
		int x, y;
	};
	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 << spiral[pos.y * n + pos.x] << "\n";
	}

	delete[] spiral;
	delete[] positions;
}

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:

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

correct output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc