Submission #645466

#TimeUsernameProblemLanguageResultExecution timeMemory
645466baojiaopisuRainforest Jumps (APIO21_jumps)C++14
4 / 100
1262 ms63996 KiB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;

#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz

#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y)
        {
            x = y;
            return true;
        }
        return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y)
        {
            x = y;
            return true;
        }
        return false;
    }

const int MOD = 1e9 + 7; //998244353

template<class X, class Y>
	void add(X &x, const Y &y) {
		x = (x + y);
		if(x >= MOD) x -= MOD;
	}

template<class X, class Y> 
	void sub(X &x, const Y &y) {
		x = (x - y);
		if(x < 0) x += MOD;
	}

/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/

const ll INF = 1e9;
const int N = 2e5 + 10;
const int LOG = 17;

pii f[N][LOG + 2];
int jump[N][LOG + 2], jr[N][LOG + 2];
int a[N], L[N], R[N];

void init(int n, vector<int> aa) {
	for(int i = 1; i <= n; i++) {
		a[i] = aa[i - 1];
		f[i][0] = mp(aa[i - 1], i);
	}

	for(int j = 1; j <= LOG; j++) {
		for(int i = 1; i <= n - (1 << j) + 1; i++) {
			int u = i + (1 << (j - 1));
			f[i][j].se = (f[i][j - 1].fi > f[u][j - 1].fi ? f[i][j - 1].se : f[u][j - 1].se);
			f[i][j].fi = max(f[i][j - 1].fi, f[u][j - 1].fi);
		}
	}

	stack<int> st;
	for(int i = 1; i <= n; i++) {
		while(!st.empty() && a[st.top()] <= a[i]) st.pop();
		if(!st.empty()) L[i] = st.top();
		st.push(i);
	}

	while(!st.empty()) st.pop();

	for(int i = n; i >= 1; i--) {
		while(!st.empty() && a[st.top()] <= a[i]) st.pop();
		if(!st.empty()) R[i] = st.top();
		st.push(i);
	}

	for(int i = 1; i <= n; i++) {
		jump[i][0] = (a[L[i]] > a[R[i]] ? L[i] : R[i]);
		jr[i][0] = R[i];
	}

	for(int j = 1; j <= LOG; j++) {
		for(int i = 1; i <= n; i++) {
			int u = jump[i][j - 1];
			jump[i][j] = jump[u][j - 1];
			u = jr[i][j - 1];
			jr[i][j] = jr[u][j - 1];
		}
	}
}

pii query(int L, int R) {
	if(L > R) return mp(0, 0);
	int x = 31 - btclz(R - L + 1);
	if(f[L][x] > f[R - (1 << x) + 1][x]) return f[L][x];
	else return f[R - (1 << x) + 1][x];
}

int minimum_jumps(int l, int r, int u, int v) {
	++l; ++r; ++u; ++v;
	if(query(r + 1, u - 1).fi >= query(u, v).fi) return -1;
	int pos = -1;
	int L = l, R = r;
	while(L <= R) {
		int mid = (L + R) >> 1;
		if(query(mid, r).fi < query(u, v).fi) {
			pos = mid;
			R = mid - 1;
		} else L = mid + 1;
	}

	if(pos < 0) return -1;

	int ans = 0;
	pos = query(pos, r).se;

	for(int i = LOG; i >= 0; i--) {
		if(!jump[pos][i]) continue;
		if(a[jump[pos][i]] <= query(r + 1, u - 1).fi) {
			pos = jump[pos][i];
			ans += (1 << i);
		}
	}

	for(int i = LOG; i >= 0; i--) {
		if(jr[pos][i] && a[jr[pos][i]] <= query(r + 1, u - 1).fi) {
			pos = jr[pos][i];
			ans += (1 << i);
		}
	}

	return ans + 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...