Submission #1197457

#TimeUsernameProblemLanguageResultExecution timeMemory
1197457Mher777Rainforest Jumps (APIO21_jumps)C++20
0 / 100
4018 ms51208 KiB
#include "jumps.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(7069);

/* -------------------- Typedefs -------------------- */

typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;

/* -------------------- Usings -------------------- */

using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* -------------------- Defines -------------------- */

#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);

/* -------------------- Constants -------------------- */

const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);
const int imp = -1;

const int max_N = 200005, M = 30;
int a[max_N], dp_left[max_N], dp_right[max_N], up[max_N][M], st[max_N][M];
int n, lg;

int qry(int l, int r) {
	int lgx = log2(r - l + 1);
	return max(st[l][lgx], st[r - (1 << lgx) + 1][lgx]);
}

void init(int N, std::vector<int> H) {
	n = N;
	lg = log2(n);
	for (int i = 1; i <= n; ++i) {
		a[i] = H[i - 1];
		st[i][0] = a[i];
		int ind = i - 1;
		while (ind) {
			if (a[ind] > a[i]) break;
			ind = dp_left[ind];
		}
		dp_left[i] = ind;
	}
	for (int i = n; i >= 1; --i) {
		int ind = i + 1;
		while (ind <= n) {
			if (a[ind] > a[i]) break;
			ind = dp_right[ind];
		}
		dp_right[i] = ind;
		up[i][0] = ind;
		for (int lgx = 1; lgx <= lg; ++lgx) {
			up[i][lgx] = up[up[i][lgx - 1]][lgx - 1];
		}
	}
	for (int lgx = 1; lgx <= lg; ++lgx) {
		for (int i = 1; i + (1 << lgx) - 1 <= n; ++i) {
			st[i][lgx] = max(st[i][lgx - 1], st[i + (1 << (lgx - 1))][lgx - 1]);
		}
	}
}

int minimum_jumps(int A, int B, int C, int D) {
	++A, ++B, ++C, ++D;
	int max_to = qry(C, D);
	int node = B;
	while (1) {
		if (dp_left[node] < A) break;
		if (a[dp_left[node]] < max_to) {
			node = dp_left[node];
		}
		else {
			break;
		}
	}
	if (a[node] >= max_to) {
		return imp;
	}
	int ans = MAX, cur = 0;
	while (1) {
		if (node >= C && node <= D) {
			ans = min(ans, cur);
			break;
		}
		if (node > D) break;
		int num_left = a[dp_left[node]];
		int num_right = a[dp_right[node]];
		if (num_right >= num_left) {
			node = dp_right[node];
			++cur;
			continue;
		}
		int cur_node = node;
		int answ = cur + 1;
		for (int lgx = lg; lgx >= 0; --lgx) {
			if (up[cur_node][lgx] < C) {
				cur_node = up[cur_node][lgx];
				answ += (1 << lgx);
			}
		}
		if (up[cur_node][0] >= C && up[cur_node][0] <= D) {
			ans = min(ans, answ);
		}
		++cur;
		node = dp_left[node];
	}
	return (ans == MAX ? imp : ans);
}

/*
7 100
3 2 1 6 4 5 7
0 1 2 2
1 3 5 6
4 4 6 6
*/
#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...