Submission #1037840

#TimeUsernameProblemLanguageResultExecution timeMemory
1037840AndreyRainforest Jumps (APIO21_jumps)C++14
23 / 100
782 ms34180 KiB
#include "jumps.h"
#include<bits/stdc++.h>
#include <vector>
using namespace std;

int n;
vector<int> haha(200010);
vector<int> lb(200001);
vector<int> rb(200001);
int big[200001][18];
int sm[200001][18];

void calc(int x) {
    stack<pair<int,int>> idk;
    for(int i = 0; i < n; i++) {
        while(!idk.empty() && idk.top().first < haha[i]) {
            idk.pop();
        }
        int c;
        if(idk.empty()) {
            c = n;
        }
        else {
            if(x == 0) {
                c = idk.top().second;
            }
            else {
                c = n-idk.top().second-1;
            }
        }
        if(x == 0) {
            lb[i] = c;
        }
        else {
            rb[n-i-1] = c;
        }
        idk.push({haha[i],i});
    }
}

void init(int N, std::vector<int> H) {
    n = N;
    haha[n] = 0;
    lb[n] = n;
    rb[n] = n;
    for(int i = 0; i < n; i++) {
        haha[i] = H[i];
    }
    calc(0);
    for(int i = 0; i < n/2; i++) {
        swap(haha[i],haha[n-i-1]);
    }
    calc(1);
    for(int i = 0; i < n/2; i++) {
        swap(haha[i],haha[n-i-1]);
    }
    for(int i = 0; i <= n; i++) {
        if(haha[lb[i]] > haha[rb[i]]) {
            big[i][0] = lb[i];
            sm[i][0] = rb[i];
        }
        else {
            big[i][0] = rb[i];
            sm[i][0] = lb[i];
        }
    }
    for(int i = 1; i < 18; i++) {
        for(int j = 0; j <= n; j++) {
            big[j][i] = big[big[j][i-1]][i-1];
            sm[j][i] = sm[sm[j][i-1]][i-1];
        }
    }
}

int minimum_jumps(int a, int b, int c, int d) {
    int ans = 0;
    if(haha[a] > haha[c]) {
        return -1;
    }
    int x = a;
    for(int i = 17; i >= 0; i--) {
        if(big[x][i] != n && haha[big[x][i]] <= haha[c]) {
            x = big[x][i];
            ans+=(1 << i);
        }
    }
    for(int i = 17; i >= 0; i--) {
        if(sm[x][i] != n && haha[sm[x][i]] <= haha[c]) {
            x = sm[x][i];
            ans+=(1 << i);
        }
    }
    if(haha[x] == haha[c]) {
        return ans;
    }
    else {
        return -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...