제출 #737305

#제출 시각아이디문제언어결과실행 시간메모리
737305phoebe밀림 점프 (APIO21_jumps)C++17
31 / 100
4054 ms36936 KiB
#include <bits/stdc++.h>
#include "jumps.h"
using namespace std;

#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define FOR(i, n) for (int i = 0; i < n; i++)
#define PB push_back

const int maxn = 2e5 + 10, INF = 1e9 + 7;
int n, h[maxn], tree[maxn * 4], 
jump_mn[maxn][20], jump_mx[maxn][20];

void build(int tl = 0, int tr = maxn, int i = 1){
    if (tl == tr){
        tree[i] = tl; return;
    }
    int tm = (tl + tr) / 2;
    build(tl, tm, i * 2); build(tm + 1, tr, i * 2 + 1);
    tree[i] = (h[tree[i * 2]] < h[tree[i * 2 + 1]] ? 
    tree[i * 2] : tree[i * 2 + 1]);
}

int solve(int s, int e){
    int re = 0;
    for (int p = 19; p >= 0; p--){
        if (h[jump_mx[s][p]] < h[e]) re += (1<<p), s = jump_mx[s][p];
    }
    if (jump_mx[s][0] == e) return re + 1;
    // cout << s << ' ' << e << endl;
    // cout << h[s] << ' ' << h[e] << endl;
    for (int p = 19; p >= 0; p--){
        if (h[jump_mn[s][p]] < h[e]) re += (1<<p), s = jump_mn[s][p];
    }
    re++; s = jump_mn[s][0];
    // cout << h[s] << ' ' << h[e] << endl;
    if (s == e) return re;
    return INF;
}

void init(int N, vector<int> H){
    fill(h, h + maxn, INF);
    n = N;
    FOR(i, n) h[i] = H[i];
    build();
    stack<int> s;
    for (int i = n - 1; i >= 0; i--){
        while (!s.empty() && h[s.top()] < h[i]) s.pop();
        int x = (s.empty() ? i : s.top());
        jump_mx[i][0] = jump_mn[i][0] = x;
        s.push(i);
    }
    while (!s.empty()) s.pop();
    for (int i = 0; i < n; i++){
        while (!s.empty() && h[s.top()] < h[i]) s.pop();
        int x = (s.empty() ? i : s.top());
        if (h[x] > h[jump_mx[i][0]] && x != i) jump_mx[i][0] = x;
        if (h[x] < h[jump_mn[i][0]] && x != i) jump_mn[i][0] = x;
        s.push(i);
    }
    // FOR(i, n) cout << jump_mx[i][0] << ' '; cout << endl;
    // FOR(i, n) cout << jump_mn[i][0] << ' '; cout << endl;
    for (int p = 1; p < 20; p++){
        FOR(i, n){
            jump_mx[i][p] = jump_mx[jump_mx[i][p - 1]][p - 1];
            jump_mn[i][p] = jump_mn[jump_mn[i][p - 1]][p - 1];
        }
    }
}

int minimum_jumps(int a, int b, int c, int d){
    int re = INF;
    for (int i = a; i <= b; i++){
        for (int j = c; j <= d; j++){
            re = min(re, solve(i, j));
        }
    }
    return (re != INF ? re : -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...