#54567: 單調棧


11430533@stu.tshs.tp.edu.tw (一孝20周定樂)


#include <bits/stdc++.h>
using namespace std;

int main(void){
    ios::sync_with_stdio(0),cin.tie(0);
 
    int N,M;
    vector<int> v,left,right,query;

    
    while (cin>>N>>M){
        left.assign(N,-1),right.assign(N,-1);
        v.assign(N+1,0),query.assign(M,0);
        stack<int> st;
        
        for (int i=0 ; i<N ; i++) cin>>v[i];
        for (int i=0 ; i<M ; i++) cin>>query[i];
        for (int i=0 ; i<N ; i++){
            while (!st.empty()){
                if (v[st.top()] < v[i]){
                    right[st.top()] = i;
                    st.pop();
                }
                else if (v[st.top()] == v[i]){
                    right[st.top()] = i;
                    left[i] = st.top();
                    st.pop();
                    break;
                }
                else {
                    left[i] = st.top();
                    break;
                }
            }
            st.push(i);
        }
        for (int x:query){
            if (left[x] == -1) {
                cout<<right[x]-x<<" ";
                continue;
            }
            else if (right[x] == -1){
                cout<<x-left[x]<<" ";
                continue;
            }
            cout<<min(x-left[x],right[x]-x)<<" ";
        }
    cout<<"\n";        
    }
}