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>
using namespace std;
//#define int long long
#define double long double
#define FOR(i,l,r,d) for(int i=(l);i<=(r);i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define V st[cidx]
#define LC st[cidx*2]
#define RC st[cidx*2+1]
#define lsb(x) ((x)&(-(x)))
//const int INF = 2147483647;
//const int LNF = INF*INF;
const int INF = 1000000007;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 200010;
const int lgMAX = 20;
int n;
int h[MAX];
int edge_l[MAX], edge_r[MAX];
int anc_l[MAX][lgMAX], anc_r[MAX][lgMAX];
int anc_hi[MAX][lgMAX];
void init(int N, vi H){
/// input
n = N;
FOR(i, 0, n-1, 1){
h[i] = H[i];
}
/// monotonic stack
FOR(i, 0, n-1, 1){
edge_l[i] = INF; /// INF -> no edge
edge_r[i] = INF;
}
vi stk;
FOR(i, 0, n-1, 1){
while(!stk.empty() and h[stk.back()] < h[i]){
stk.pop_back();
}
if(!stk.empty()){
edge_l[i] = stk.back();
}
stk.pb(i);
}
stk.clear();
for(int i=n-1; i>=0; i--){
while(!stk.empty() and h[stk.back()] < h[i]){
stk.pop_back();
}
if(!stk.empty()){
edge_r[i] = stk.back();
}
stk.pb(i);
}
/// 2^k-th ancestor
FOR(i, 0, n-1, 1){
if(edge_l[i]==INF and edge_r[i]==INF){
anc_hi[i][0] = INF;
}
else if(edge_l[i]==INF or edge_r[i]==INF){
anc_hi[i][0] = min(edge_l[i], edge_r[i]);
}
else{
anc_hi[i][0] = (h[edge_l[i]] > h[edge_r[i]]) ? edge_l[i] : edge_r[i];
}
anc_l[i][0] = edge_l[i];
anc_r[i][0] = edge_r[i];
}
FOR(j, 1, lgMAX-1, 1){
FOR(i, 0, n-1, 1){
anc_hi[i][j] = (anc_hi[i][j-1]!=INF) ? anc_hi[ anc_hi[i][j-1] ][j-1] : INF;
anc_l [i][j] = (anc_l [i][j-1]!=INF) ? anc_l [ anc_l [i][j-1] ][j-1] : INF;
anc_r [i][j] = (anc_r [i][j-1]!=INF) ? anc_r [ anc_r [i][j-1] ][j-1] : INF;
}
}
}
int minimum_jumps(int A, int B, int C, int D){
int ret = 0;
for(int j=lgMAX-1; j>=0; j--){
if(anc_l[D][j]!=INF and anc_l[D][j] >= C){
D = anc_l[D][j];
}
}
cerr<<"D = "<<D<<endl;
int start = B;
for(int j=lgMAX-1; j>=0; j--){
if(anc_l[start][j]!=INF and h[anc_l[start][j]] < h[D] and anc_l[start][j] >= A){
start = anc_l[start][j];
}
}
cerr<<"start = "<<start<<endl;
int wall = B;
for(int j=lgMAX-1; j>=0; j--){
if(anc_r[wall][j]!=INF and anc_r[wall][j] < C){
wall = anc_r[wall][j];
}
}
cerr<<"wall = "<<wall<<endl;
if(h[wall] > h[D] or edge_r[wall]==-1 or h[edge_r[wall]] > h[D]) return -1;
int v = start;
for(int j=lgMAX-1; j>=0; j--){
if(anc_hi[v][j]!=INF and h[anc_hi[v][j]] <= h[wall]){
v = anc_hi[v][j];
ret += (1<<j);
}
}
if(h[v] < h[wall] and anc_hi[v][0]!=INF and h[anc_hi[v][0]] <= h[D]){
v = anc_hi[v][0];
ret++;
}
for(int j=lgMAX-1; j>=0; j--){
if(anc_r[v][j]!=INF and anc_r[v][j] < C){
v = anc_r[v][j];
ret += (1<<j);
}
}
if(C <= v and v <= D){
return ret;
}
else if(C <= edge_r[v] and edge_r[v] <= D){
return ret + 1;
}
else{
return -1;
}
}
/*
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//init(7, {3, 2, 1, 6, 4, 5, 7});
//cerr<<minimum_jumps(4, 4, 6, 6)<<endl; /// 2
//cerr<<minimum_jumps(1, 3, 5, 6)<<endl; /// 1
//cerr<<minimum_jumps(0, 1, 2, 2)<<endl; /// -1
init(4, {4, 1, 2, 3});
cerr<<minimum_jumps(0, 0, 3, 3)<<endl; /// -1
return 0;
}
*/
# | 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... |