Submission #720033

#TimeUsernameProblemLanguageResultExecution timeMemory
720033numberesRainforest Jumps (APIO21_jumps)C++17
4 / 100
1158 ms62120 KiB
/**
 * @date    2023-04-06 17:59:22
 * RAmen!
 */
#include<bits/stdc++.h>
#include "jumps.h"
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define ll long long
using namespace std;
int n;
int h[200005], lg2[200005];
int hi[200005][18], lo[200005][18], L[200005][18];
int l[200005], r[200005], p[200005];
int mx[200005][18];
bool cmp(int x, int y) {
	return h[x] > h[y];
}
int chmax(int x, int y) {
	return h[x] > h[y] ? x : y;
}
int query(int x, int y) {
	int d = lg2[y - x + 1];
	return chmax(mx[x][d], mx[y - (1 << d) + 1][d]);
}
void init(int N, vector<int> H) {
	n = N;
	for(int i = 0; i < n; i++) {
		h[i + 1] = H[i]; p[i + 1] = i + 1; 
		mx[i + 1][0] = i + 1; if(i > 0) lg2[i + 1] = lg2[(i + 1) >> 1] + 1;
	}
	for(int i = 1; i <= n; i++) {
		l[i] = i;
		while(l[i] - 1 >= 1 && h[l[i] - 1] <= h[i]) l[i] = l[l[i] - 1];
	}
	for(int i = n; i >= 1; i--) {
		r[i] = i;
		while(r[i] + 1 <= n && h[r[i] + 1] <= h[i]) r[i] = r[r[i] + 1];
	}
	sort(p + 1, p + n + 1, cmp);
	for(int i = 1; i <= n; i++) {
		int v = p[i];
		hi[v][0] = (h[l[v] - 1] >= h[r[v] + 1] ? l[v] - 1 : r[v] + 1);
		lo[v][0] = (r[v] + 1 > n ? l[v] - 1 : l[v] - 1 < 1 ? r[v] + 1 :
			(h[l[v] - 1] <= h[r[v] + 1] ? l[v] - 1 : r[v] + 1));
		L[i][0] = l[i] - 1;
		for(int j = 1; j <= 17; j++) {
			hi[v][j] = hi[hi[v][j - 1]][j - 1];
			lo[v][j] = lo[lo[v][j - 1]][j - 1];
			L[i][j] = L[L[i][j - 1]][j - 1];
		}
	}
	for(int j = 1; j <= 17; j++)
		for(int i = 1; i + (1 << j) - 1 <= n; i++) {
			mx[i][j] = chmax(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
		}
}
int minimum_jumps(int A, int B, int C, int D) {
	A++; B++; C++; D++;
	int e = query(C, D), m = query(B, C - 1);
	if(h[e] < h[m]) return -1;
	int v = B, cnt = 0;
	for(int i = 17; i >= 0; i--)
		if(L[v][i] && L[v][i] >= A && h[L[v][i]] <= h[m])
			v = L[v][i];
	for(int i = 17; i >= 0; i--)
		if(hi[v][i] && h[hi[v][i]] <= h[m]) {
			v = hi[v][i]; cnt += (1 << i);
		}
	for(int i = 17; i >= 0; i--)
		if(lo[v][i] && h[lo[v][i]] <= h[m]) {
			v = lo[v][i]; cnt += (1 << i);
		}
	return cnt + (h[v] != h[m]) + 1;
}
#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...