This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l); i<=(r); ++i)
#define REP(i,l,r) for(int i=(l); i<(r); ++i)
#define FORD(i,r,l) for(int i=(r); i>=(l); --i)
#define REPD(i,r,l) for(int i=(r)-1; i>=(l); --i)
using namespace std;
const int N = 2e5 + 5, logN = 18, INF = 0x3c3c3c3c;
int n, a[N], lft[N][logN], rgt[N][logN], high[N][logN];
vector<int> S[N];
void init(int N, vector<int> H) {
n = N + 2;
REP(i, 1, n) a[i] = H[i - 1];
a[0] = n; a[n] = n + 1;
FOR(i, 0, n) rgt[i][0] = n;
vector<int> Q;
FOR(i, 0, n) {
while (!Q.empty() && a[Q.back()] < a[i]) {
rgt[Q.back()][0] = i;
Q.pop_back();
}
if (Q.size()) lft[i][0] = Q.back();
Q.push_back(i);
}
FOR(i, 0, n) high[i][0] = (a[lft[i][0]] > a[rgt[i][0]] ? lft[i][0] : rgt[i][0]);
REP(j, 1, 18) FOR(i, 0, n) {
lft[i][j] = lft[lft[i][j - 1]][j - 1];
rgt[i][j] = rgt[rgt[i][j - 1]][j - 1];
high[i][j] = high[high[i][j - 1]][j - 1];
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A; ++B; ++C; ++D;
int pos = B;
REPD(i, logN, 0) {
int new_pos = lft[pos][i];
if (A <= new_pos && rgt[new_pos][0] <= D) pos = new_pos;
}
if (C <= rgt[pos][0] && rgt[pos][0] <= D) return 1;
int sum = 0;
REPD(i, logN, 0) {
int new_pos = high[pos][i];
if (rgt[new_pos][0] < C) sum += 1 << i, pos = new_pos;
}
{
int new_pos = high[pos][0];
int new_sum = sum + 1;
if (C <= rgt[new_pos][0] && rgt[new_pos][0] <= D) return new_sum + 1;
}
REPD(i, 18, 0) {
int new_pos = rgt[pos][i];
if (rgt[new_pos][0] < C) sum += 1 << i, pos = new_pos;
}
{
int new_pos = rgt[pos][0];
int new_sum = sum + 1;
if (C <= rgt[new_pos][0] && rgt[new_pos][0] <= D) return new_sum + 1;
}
return -1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |