Submission #1298764

#TimeUsernameProblemLanguageResultExecution timeMemory
1298764SamNguyenSquare or Rectangle? (NOI19_squarerect)C++20
0 / 100
1 ms340 KiB
#include "squarerect.h"
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;

struct BinarySearch
{
	using F = function<bool(int)>;

	F pred;
	BinarySearch(F f) : pred(f) {}

	int lower_bound(int min_v, int max_v, bool flipped = false)
	{
		int res_v = max_v + 1;
		while (min_v <= max_v)
		{
			int mid_v = (min_v + max_v) >> 1;
			if (pred(mid_v) ^ flipped)
				res_v = mid_v, max_v = mid_v - 1;
			else
				min_v = mid_v + 1;
		}
		return res_v;
	}
};

const int MAX = 100 + 2;
struct Problem
{
	int memo[MAX][MAX];
	int N, Q, DIVIDERS, BLOCK_LEN;

	Problem(int n = 1, int q = 1, int DIVIDERS = 1) : N(n), Q(q), DIVIDERS(DIVIDERS)
	{
		BLOCK_LEN = N / DIVIDERS;
		memset(memo, -1, sizeof memo);
	}

	inline bool inside_shape_safe(int x, int y)
	{
		if (x <= 0 or x > N or y <= 0 or y > N)
			return false;
		if (memo[x][y] == -1)
			memo[x][y] = inside_shape(x, y);
		return memo[x][y];
	}

	inline pair<int, int> master_grid(int i, int j)
	{
		return make_pair(i * BLOCK_LEN + 1, j * BLOCK_LEN + 1);
	}

	bool solve_large_from_cell(int x, int y)
	{
		BinarySearch bs_by_x([&](int t)
							 { return inside_shape_safe(t, y); });
		BinarySearch bs_by_y([&](int t)
							 { return inside_shape_safe(x, t); });

		int left = bs_by_x.lower_bound(max(1, x - BLOCK_LEN + 1), x - 1);
		int top = bs_by_y.lower_bound(max(1, y - BLOCK_LEN + 1), y - 1);
		int right = bs_by_x.lower_bound(x + 1, N, true) - 1;
		int bottom = right - left + top;

		// cerr << "Solved: " << left << " " << top << "  " << right << " -> " << bottom << endl;

		return inside_shape_safe(right, bottom) and not inside_shape_safe(right, bottom + 1) and not inside_shape_safe(right + 1, bottom);
	}

	bool solve_small_from_cell(int x, int y)
	{
		BinarySearch bs_by_x([&](int t)
							 { return inside_shape_safe(t, y); });
		BinarySearch bs_by_y([&](int t)
							 { return inside_shape_safe(x, t); });

		// int left = bs_by_x.lower_bound(max(1, x - BLOCK_LEN + 1), x - 1);
		// int top = bs_by_y.lower_bound(max(1, y - BLOCK_LEN + 1), y - 1);
		// int right = bs_by_x.lower_bound(max(x + 1, left + BLOCK_LEN), min(N, x + BLOCK_LEN - 1), true);
		// int bottom = bs_by_y.lower_bound(max(y + 1, top + BLOCK_LEN), min(N, y + BLOCK_LEN - 1), true);
		// return right - left == bottom - top;

		if (x == 1)
		{
			int y0 = bs_by_y.lower_bound(max(1, y - BLOCK_LEN + 1), y - 1);
			return inside_shape_safe(x + BLOCK_LEN - 1, y0 + BLOCK_LEN - 1);
		}
		if (y == 1)
		{
			int x0 = bs_by_x.lower_bound(max(1, x - BLOCK_LEN + 1), x - 1);
			return inside_shape_safe(x0 + BLOCK_LEN - 1, y + BLOCK_LEN - 1);
		}

		return false;
	}
};

bool am_i_square(int N, int Q)
{
	Problem problem(N, Q, 5);
	mt19937 rng;

	for (int i = 1; i < 5; i++)
		for (int j = 1; j < 5; j++)
		{
			int x, y;
			tie(x, y) = problem.master_grid(i, j);
			if (problem.inside_shape_safe(x, y))
				return problem.solve_large_from_cell(x, y);
		}

	for (int k = 0; k < 5; k++)
	{
		int x, y;
		tie(x, y) = problem.master_grid(k, 0);
		if (problem.inside_shape_safe(x, y))
			return problem.solve_small_from_cell(x, y);

		if (k == 0)
			continue;

		tie(x, y) = problem.master_grid(0, k);
		if (problem.inside_shape_safe(x, y))
			return problem.solve_small_from_cell(x, y);
	}

	return false;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...