Submission #82946

#TimeUsernameProblemLanguageResultExecution timeMemory
82946SaboonTriangles (CEOI18_tri)C++14
100 / 100
715 ms23060 KiB
#include <bits/stdc++.h>
#include <trilib.h>
using namespace std;
const int maxn = 4e4 + 100;
const int SQRT = 201;
int first = 1, m = 3;
int pre[maxn], nex[maxn], nexsqrt[maxn];

void remove (int fi, int mid, int se) {
	m --;
	pre[se] = fi, nex[fi] = se;
	if (mid == first) {
		first = se;
		return;
	}

	int last = mid, cnt = SQRT;
	while (cnt --) {
		last = pre[last];
		nexsqrt[last] = -1;
		if (last == first)
			break;
	}
	int nexto = last;
	cnt = SQRT;
	while (cnt --) {
		nexto = nex[nexto];
		if (nexto == first)
			return;
	}
	while (last != se and nexto != first) {
		nexsqrt[last] = nexto;
		last = nex[last];
		nexto = nex[nexto];
	}
}


void insert (int fi, int mid, int se) {
	m ++;
	nex[fi] = mid, nex[mid] = se;
	pre[se] = mid, pre[mid] = fi;

	int last = mid, cnt = SQRT;
	while (cnt -- and last != first) {
		last = pre[last];
	}
	int nexto = last;
	cnt = SQRT;
	while (cnt --) {
		nexto = nex[nexto];
		if (nexto == first)
			break;
	}
	while (nexto != first) {
		nexsqrt[last] = nexto;
		last = nex[last];
		nexto = nex[nexto];
		if (last == se)
			return;
	}
}

int get (int idx) {
	idx %= m;
	int tmp = first;
	while (idx > 0) {
		if (idx >= SQRT) {
			tmp = nexsqrt[tmp];
			idx -= SQRT;
		}
		else {
			tmp = nex[tmp];
			idx --;
		}
	}
	return tmp;
}

void build_first () {
	if (is_clockwise (1, 2, 3)) {
		nex[1] = 2;
		nex[2] = 3;
		nex[3] = 1;
		pre[1] = 3;
		pre[2] = 1;
		pre[3] = 2;
	}
	else {
		nex[1] = 3;
		nex[3] = 2;
		nex[2] = 1;
		pre[1] = 2;
		pre[2] = 3;
		pre[3] = 1;
	}
}

int main() {
	ios::sync_with_stdio(false);
	memset (nexsqrt, -1, sizeof nexsqrt);
	int n = get_n ();
	build_first ();
	for (int i = 4; i <= n; i++) {
		int lo = 0, hi = m;
		while (hi - lo > 1) {
			int mid = (hi + lo) >> 1;
			int x = get (lo), y = get (mid);
			if (!is_clockwise (x, y, i))
				hi = mid;
			else
				lo = mid;
		}

		int x = get (lo), y = nex[x];
		if (is_clockwise (x, y, i))
			continue;

		insert (x, i, nex[x]);

		while (!is_clockwise (i, nex[i], nex[nex[i]]))
			remove (i, nex[i], nex[nex[i]]);

		while (!is_clockwise (pre[pre[i]], pre[i], i)) 
			remove (pre[pre[i]], pre[i], i);
	}
	give_answer (m);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...