Submission #1063515

#TimeUsernameProblemLanguageResultExecution timeMemory
1063515fv3The Big Prize (IOI17_prize)C++14
100 / 100
38 ms3928 KiB
#include "prize.h"
#include <bits/stdc++.h>

using namespace std;

vector<int> st;
int nt = 1;

int get_range(int l, int r, int k, int x, int y)
{
	if (x > r || y < l) return 0;
	if (x >= l && y <= r) return st[k];
	int c = (x + y) / 2;
	return get_range(l, r, k*2, x, c) + get_range(l, r, k*2|1, c+1, y);
}

void add(int index)
{
	st[nt + index] = 1;
	for (int i = (nt + index) / 2; i >= 1; i /= 2)
		st[i] = st[i*2] + st[i*2|1];
}

vector<int> parent;
int find(int index)
{
	if (!parent[index]) return index;
	return parent[index] = find(parent[index]);
}

int find_best(int n)
{
	parent = vector<int>(n);

	while (nt < n) nt <<= 1;
	st = vector<int>(2 * nt);

	map<int, int> m;
	m[n] = 2 * n;

	int mx_cnt = 1;
	int left_most = 0;
	while (1)
	{
		int r = n - 1;
		for (auto node : m)
		{
			if (node.second == -1 || node.first < left_most) continue; 
			if (node.second - get_range(0, node.first, 1, 0, nt - 1) > 0)
			{
				r = node.first - 1;
				break;
			}
			left_most = node.first + 1;
		}
		int l = left_most;

		// Find better left and right bounds

		while (1)
		{
			int c = (l + r) / 2;
			int index = find(c);

			if (index == n)
			{
				r = c - 1;
				continue;
			}

			auto val = ask(index);
			int cnt = val[0] + val[1]; 
			if (cnt == 0)
				return index;

			if (cnt > mx_cnt)
			{
				mx_cnt = cnt;
				for (auto&node : m)
				{
					if (node.second == -1 || node.first == n) continue;
					add(node.first);
					node.second = -1;
				}
				left_most = 0;
				m[index] = val[0];
				break;
			}

			if (cnt < mx_cnt)
			{
				parent[index] = index + 1;
				add(index);
				break;
			}

			if (val[0] == get_range(0, index, 1, 0, nt-1))
				l = c + 1;
			else
				r = c - 1;

			m[index] = val[0];
		}

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