Next/Previous + smaller/greater element
struct Nextprev {
// next and prev vectors show n and -1 respectively in case when
// required element doesn't exists in vector.
vector<int > nextgreater, prevgreater, nextsmaller, prevsmaller, a;
int n;
Nextprev (vector<int > &arr){
a = vector<int >(arr.begin (), arr.end ());
n = a.size ();
}
void calcNextGreater (){
nextgreater = vector<int >(n, n);
stack<int > st;
for (int i = 0 ; i < n ; i++){
while (!st.empty () && a[st.top ()] < a[i]){
nextgreater[st.top ()] = i;
st.pop ();
}
st.push (i);
}
return ;
}
void calcNextSmaller (){
nextsmaller = vector<int >(n, n);
stack<int > st;
for (int i = 0 ; i < n ; i++){
while (!st.empty () && a[st.top ()] > a[i]){
nextsmaller[st.top ()] = i;
st.pop ();
}
st.push (i);
}
return ;
}
void calcPrevSmaller (){
prevsmaller = vector<int > (n, -1 );
stack<int > st;
for (int i = n - 1 ; i >= 0 ; i--){
while (!st.empty () && a[i] < a[st.top ()]){
prevsmaller[st.top ()] = i;
st.pop ();
}
st.push (i);
}
return ;
}
void calcPrevGreater (){
prevgreater = vector<int >(n, -1 );
stack<int > st;
for (int i = n - 1 ; i >= 0 ; i--){
while (!st.empty () && a[i] > a[st.top ()]){
prevgreater[st.top ()] = i;
st.pop ();
}
st.push (i);
}
return ;
}
};