Submission #561511

#TimeUsernameProblemLanguageResultExecution timeMemory
561511ngpin04Rainforest Jumps (APIO21_jumps)C++14
100 / 100
1174 ms52132 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end() 
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  if (a < b) {a = b; return true;} return false;
}
const int Nmax = 2e5 + 5; 
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);

int anc[Nmax][20];
int ptr[Nmax][20];
int ind[Nmax][20];
int l[Nmax];
int r[Nmax];
int a[Nmax];
int n;

int getmax(int i, int j) {
    return (a[i] < a[j]) ? j : i;
}

int getseg(int l, int r) {
    int d = log2(r - l + 1);
    return getmax(ind[l][d], ind[r - bit(d) + 1][d]);
}

void init(int N, std::vector<int> H) {
    n = N;

    for (int i = 0; i < n; i++)
        a[i] = H[i];

    vector <int> s;
    for (int i = 0; i < n; i++) {
        while (s.size() && a[i] > a[s.back()])
            s.pop_back();
        l[i] = (s.size()) ? s.back() : -1;
        s.push_back(i);
    }
    s.clear();
    for (int i = n - 1; i >= 0; i--) {
        while (s.size() && a[i] > a[s.back()])
            s.pop_back();
        r[i] = (s.size()) ? s.back() : -1;
        s.push_back(i);
    }

    for (int i = 0; i < n; i++) {
        int p = -1;
        if (l[i] >= 0)
            p = l[i];
        if (r[i] >= 0) {
            if (p < 0 || a[p] < a[r[i]])
                p = r[i];
        }
        anc[i][0] = p;
    }

    for (int j = 1; j < 20; j++)    
    for (int i = 0; i < n; i++) {
        int par = anc[i][j - 1];
        anc[i][j] = (par < 0) ? -1 : (anc[par][j - 1]);
    }

    for (int i = 0; i < n; i++)
        ind[i][0] = i;

    for (int j = 1; j < 20; j++)
    for (int i = 0; i + bit(j) - 1 < n; i++)
        ind[i][j] = getmax(ind[i][j - 1], ind[i + bit(j - 1)][j - 1]);

    for (int i = 0; i < n; i++)
        ptr[i][0] = r[i];

    for (int j = 1; j < 20; j++)
    for (int i = 0; i < n; i++) 
        ptr[i][j] = (ptr[i][j - 1] == -1) ? -1 : ptr[ptr[i][j - 1]][j - 1];
}

int calc(int s, int t) {
    int res = 0;
    for (int i = 19; i >= 0; i--) 
        if (anc[s][i] >= 0 && a[anc[s][i]] <= a[t])
            s = anc[s][i], res += bit(i);

    for (int i = 19; i >= 0; i--)
        if (ptr[s][i] >= 0 && a[ptr[s][i]] <= a[t])
            s = ptr[s][i], res += bit(i);
    return res;
}

int minimum_jumps(int A, int B, int C, int D) {
    int mx = a[getseg(C, D)];
    int cur = B;
    for (int i = 19; i >= 0; i--) if (cur - bit(i) + 1 >= A) {
        if (a[ind[cur - bit(i) + 1][i]] < mx)
            cur -= bit(i);
    }

    if (cur == B)
        return -1;  

    if (C - B == 1)
        return 1;

    int pos = getseg(cur + 1, B);
    if (r[pos] >= C)
        return 1;

    int mid = getseg(B + 1, C - 1);
    if (a[mid] > mx)
        return -1;
    
    int res = calc(pos, mid) + 1;
    if (l[mid] >= 0 && a[l[mid]] < mx)
        mini(res, calc(pos, l[mid]) + 1);
    return res;
}


#include "jumps.h"
// int main() {
//     int N, Q;
//     assert(2 == scanf("%d %d", &N, &Q));
//     std::vector<int> H(N);
//     for (int i = 0; i < N; ++i) {
//     assert(1 == scanf("%d", &H[i]));
//     }
//     init(N, H);

//     for (int i = 0; i < Q; ++i) {
//     int A, B, C, D;
//     assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
//     printf("%d\n", minimum_jumps(A, B, C, D));
//     }
//     return 0;
// }
#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...