Submission #1172071

#TimeUsernameProblemLanguageResultExecution timeMemory
1172071baldwin_huang밀림 점프 (APIO21_jumps)C++20
0 / 100
145 ms89720 KiB
#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> h;
const int INF = 1e9;

struct node {
	int left = -1;
	int right = -1;
};

vector<node> nodes;
vector< vector<int> > binary_lifting(262144 + 1, vector<int>(32, -1)); // It tells you the 2^j ancestor of i.
vector< vector<int> > binary_lifting_x(262144 + 1, vector<int>(32, -1));

void init(int N, vector<int> H) {
	n = N;
	h = H;
	nodes = vector<node>(n); // It's a graph, has the index to the children.

	for (int i = 1; i < n; i++) {
		int target = i - 1;
		while (H[target] <= H[i]) {
			if (target == -1) {
				break;
			}
			target = nodes[target].left;
		}
		nodes[i].left = target;
	}

	for (int i = n - 2; i >= 0; i--) {
		int target = i + 1;
		while (H[target] <= H[i]) {
			if (target == -1) {
				break;
			}
			target = nodes[target].right;
		}
		nodes[i].right = target;
	}

	// Create the binary_lift

	// Create the base ancestors;
	for (int i = 0; i < n; i++) {
		int greatest = -1;
		int ancestor = -1;
		if (nodes[i].left != -1 && greatest < H[nodes[i].left]) {
			greatest = H[nodes[i].left];
			ancestor = nodes[i].left;
		}
		if (nodes[i].right != -1 && greatest < H[nodes[i].right]) {
			greatest = H[nodes[i].right];
			ancestor = nodes[i].right;
		}
		binary_lifting[i][0] = ancestor;
	}

	for (int i = 1; i < 32; i++) {
		for (int j = 0; j < n; j++) {
			if (binary_lifting[j][i - 1] == -1) {
				binary_lifting[j][i] = -1;
			}
			else {
				binary_lifting[j][i] = binary_lifting[binary_lifting[j][i - 1]][i - 1];
			}
		}
	}

	for (int i = 0; i < n; i++) {
		int smallest = 1e9 + 10;
		int ancestor = -1;
		if (nodes[i].left != -1 && smallest > H[nodes[i].left]) {
			smallest = H[nodes[i].left];
			ancestor = nodes[i].left;
		}
		if (nodes[i].right != -1 && smallest > H[nodes[i].right]) {
			smallest = H[nodes[i].right];
			ancestor = nodes[i].right;
		}
		binary_lifting_x[i][0] = ancestor;
	}

	for (int i = 1; i < 32; i++) {
		for (int j = 0; j < n; j++) {
			if (binary_lifting_x[j][i - 1] == -1) {
				binary_lifting_x[j][i] = -1;
			}
			else {
				binary_lifting_x[j][i] = binary_lifting_x[binary_lifting_x[j][i - 1]][i - 1];
			}
		}
	}

	return;
}

int minimum_jumps(int A, int B, int C, int D) {

	if (A != B || C != D) {
		return -1;
	}

	int ans = 0;
	int height = h[A];
	int pos = A;

	// Find the Y that is <= C
	for (int i = 18; i >= 0; i--) {
		if (binary_lifting[pos][i] != -1 && h[binary_lifting[pos][i]] <= h[C] && (h[C] < h[binary_lifting[pos][i + 1]] || binary_lifting[pos][i + 1] == -1)) {
			pos = binary_lifting[pos][i];
			ans += 1<<i;
		}
	}		

	// Does the same but for the X value.
	
	for (int i = 18; i >= 0; i--) {
		if (binary_lifting_x[pos][i] != -1 && h[binary_lifting_x[pos][i]] <= h[C] && (h[C] < h[binary_lifting_x[pos][i + 1]] || binary_lifting_x[pos][i + 1] == -1)) {
			pos = binary_lifting_x[pos][i];
			ans += 1<<i;
		}
	}

	return ans;

}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...