Submission #691595

#TimeUsernameProblemLanguageResultExecution timeMemory
691595Dan4LifeRainforest Jumps (APIO21_jumps)C++17
44 / 100
1194 ms61544 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back

const int maxn = (int)2e5+10;
const int maxlg = 24;
const int INF = (int)1e9;

int n, pr[maxn], nx[maxn];
int l[maxlg][maxn], r[maxlg][maxn], hi[maxlg][maxn];

void calc(int jmp[][maxn]){
    for(int i = 1; i < maxlg; i++)
        for(int j = 0; j < n; j++)
            if(jmp[i-1][j]!=-1) jmp[i][j] = jmp[i-1][jmp[i-1][j]];
}

void init(int N, vector<int> a) {
    n = N;
    stack<pair<int,int>> S;
    memset(l,-1,sizeof(l));
    memset(r,-1,sizeof(r));
    memset(pr,-1,sizeof(pr));
    memset(nx,-1,sizeof(nx));
    memset(hi,-1,sizeof(hi));
    for(int i = 0; i < n; i++){
        while(!S.empty() and S.top().fi<a[i]) S.pop();
        if(!S.empty()) pr[i] = S.top().se;
        S.push({a[i],i});
    }
    while(!S.empty()) S.pop();
    for(int i = n-1; i>=0; i--){
        while(!S.empty() and S.top().fi<a[i]) S.pop();
        if(!S.empty()) nx[i] = S.top().se;
        S.push({a[i],i});
    }
    while(!S.empty()) S.pop();
    for(int i = 0; i < n; i++){
        if(pr[i]!=-1) l[0][i]=pr[i];
        if(nx[i]!=-1) r[0][i]=nx[i];
        if(pr[i]!=-1 and nx[i]!=-1)
            hi[0][i] = (a[pr[i]]<a[nx[i]]?nx[i]:pr[i]);
    }
    calc(l), calc(r), calc(hi);
}

int get_path(int *x, int y, int z, int jmp[][maxn], int rx=0){
    int tot = 0;
    for(int i = 20; i >= 0; i--)
        if(jmp[i][*x]>=rx)// and jmp[i][*x]<y)
            if(nx[jmp[i][*x]]!=-1 and nx[jmp[i][*x]]<=z)
                *x = jmp[i][*x], tot+=(1<<i);
    return tot;
}

int minimum_jumps(int A, int x, int C, int D) {
               get_path(&x,C,D,l,A);
    int num1 = get_path(&x,C,D,hi);
    int num2 = get_path(&x,C,D,r);
    return ((nx[x]>=C and nx[x]<=D)?num1+num2+1:-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...