Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int>P;

struct node
{
    int id,val;
    int par,left,right;
};

vector<int> arr;
vector<node> tree;
//n????????????????????????,m???euler_tour??§?±???????????????¨??????????????????
int n,m,root;
int hoge = 0;
vector<int> euler_tour,depth,diff,visit_id;

void make_cartesian_tree()
{
    rep(i,n){
        tree[i] = (node){i,arr[i],-1,-1,-1};
    }
    //??????????????????,???
    stack<P> st;
    st.push(P(0,arr[0]));
    root = 0;
    for(int i = 1;i < n;i++){
        int buff;
        while(1){
            hoge++;
            if(hoge >= 100000){
                cout << "1\n";
                return 0;
            }
            //i????????¨????????´???
            if(st.empty()){
                st.push(P(i,arr[i]));
                tree[i].left = buff;
                tree[buff].par = i;
                root = i;
                break;
            }
            P tp = st.top();
            if(tp.se <= arr[i]){
                tree[i].par = tp.fi; //i??????????????´
                int nd = tree[tp.fi].right;  //i???????????????????????????
                //i????????¨????????????????????¨????????´???
                if(nd != -1){
                    tree[i].left = nd;  //i??????????????????????????????i??????????????¨??????
                    tree[nd].par = i; //i????????????????????????????????????i??¨??????
                }
                tree[tp.fi].right = i;  //i????????????????????????i??¨??????
                st.push(P(i,arr[i]));
                break;
            }
            buff = tp.fi;   //?????????pop??????????????????????¨?
            st.pop();
        }
    }
}

void make_euler_tour(int u,int& id,int d)
{
    visit_id[u] = id;
    euler_tour[id] = u;
    depth[id++] = d;
    if(tree[u].left >= 0){
        make_euler_tour(tree[u].left,id,d+1);
        euler_tour[id] = u;
        depth[id++] = d;
    }
    if(tree[u].right >= 0){
        make_euler_tour(tree[u].right,id,d+1);
        euler_tour[id] = u;
        depth[id++] = d;
    }
}

vector<vector<int> > Sparse_Table;  //i??????????????????2^k????????????????????????depth?????????????°??????????????????????
vector<vector<vector<int> > > Table_Lookup;
vector<int> Block_arr;  //?????????????????§??±??????????°?????????????????????????????????????
vector<int> diff_kai;   //??????????????????diff????????±?????????????????°????????????
int block_size,block_rem,block_cnt,log_block_cnt;

void make_Block_arr()
{
    block_size = ceil(log2(m)/2);
    block_cnt = (m-1) / block_size + 1;
    block_rem = m % block_size;
    log_block_cnt = ceil(log2(block_cnt))+1;
    Block_arr.clear();
    Block_arr.resize(block_cnt);
    rep(i,block_cnt){
        int mn = INF;
        int mn_id = -1;
        rep(j,block_size){
            int now_id = i*block_size+j;
            if(now_id >= m){
                break;
            }
            if(depth[now_id] < mn){
                mn = depth[now_id];
                mn_id = now_id;
            }
        }
        Block_arr[i] = mn_id;
    }
}

void make_Sparse_Table()
{
    Sparse_Table.resize(block_cnt,vector<int>(log_block_cnt,-1));
    rep(i,block_cnt){
        Sparse_Table[i][0] = Block_arr[i];
    }
    rep(j,log_block_cnt-1){
        rep(i,block_cnt){
            if(i + (1 << j) >= block_cnt){
                Sparse_Table[i][j+1] = Sparse_Table[i][j];
            }else{
                if(depth[Sparse_Table[i][j]] <= depth[Sparse_Table[i+(1<<j)][j]]){
                    Sparse_Table[i][j+1] = Sparse_Table[i][j];
                }else{
                    Sparse_Table[i][j+1] = Sparse_Table[i+(1<<j)][j];
                }
            }
        }
    }
}

void make_diff_kai()
{
    diff_kai.resize(block_cnt);
    rep(i,block_cnt){
        int val = 0;
        rep(j,block_size-1){
            int now_id = i*block_size+j;
            if(now_id >= m){
                break;
            }
            if(diff[now_id] > 0){
                val += (1 << (block_size-j-2));
            }
        }
        diff_kai[i] = val;
    }
}

void make_Table_Lookup()
{
    Table_Lookup.resize((1 << block_size),vector<vector<int> >(block_size+1,vector<int>(block_size+1,0)));
    //0???????°?,1????¢????
    rep(i,(1 << block_size)){
        vector<int> vec(block_size-1,-1);
        rep(j,block_size){
            if(i & (1 << (block_size-j-2))){
                vec[j] = 1;
            }
        }
        rep(j,block_size){
            int nw = 0;
            int mn = 0;
            int mn_id = j;
            Table_Lookup[i][j][j+1] = j;
            for(int k=j+2;k<=block_size;k++){
                nw += vec[k-2];
                if(nw < mn){
                    mn = nw;
                    mn_id = k-1;
                }
                Table_Lookup[i][j][k] = mn_id;
            }
        }
    }
}

//????????????????????¨??????pair
P PM_RMQ(int st,int ed)	//???????????????????????????st~ed?????§
{
    int st_block_id = (st == 0)?0:((st-1) / block_size + 1);  //?????????????????????????§?
    int ed_block_id = ed / block_size - 1;    //???????????????????????????
    int st_id = st / block_size;
    int ed_id = ed / block_size;
    int st_rem = st % block_size;
    int ed_rem = ed % block_size;
    int st_val = Block_arr[st_id];
    int ed_val = Block_arr[ed_id];
    int res,mn = -INF;
    if(ed_block_id - st_block_id < 0){    //????????????????????????????????¨??????????????´???
        if(st_id == ed_id){ //????????????????????????????????¨????????´???
            int id_kari = block_size * st_id + Table_Lookup[diff_kai[st_id]][st_rem][ed_rem+1];
            return P(euler_tour[id_kari],arr[euler_tour[id_kari]]);
        }else{  //??£?????????????????????????????¨????????´???
            int cand1 = block_size * st_id + Table_Lookup[diff_kai[st_id]][st_rem][block_size];
            int cand2 = block_size * ed_id + Table_Lookup[diff_kai[ed_id]][0][ed_rem+1];
            if(depth[cand1] <= depth[cand2]){
                return P(euler_tour[cand1],arr[euler_tour[cand1]]);
            }else{
                return P(euler_tour[cand2],arr[euler_tour[cand2]]);
            }
        }
    }else{  //????????????????????????????????¨????????´???
        int num = floor(log2(ed_block_id - st_block_id + 1));
        int cand1 = Sparse_Table[st_block_id][num];
        int cand2 = Sparse_Table[ed_block_id-(1 << num)+1][num];
        int c1,c2;
        if(depth[cand1] <= depth[cand2]){
            c1 = cand1;
        }else{
            c1 = cand2;
        }
        int cand3 = block_size * st_id + Table_Lookup[diff_kai[st_id]][st_rem][block_size];
        int cand4 = block_size * ed_id + Table_Lookup[diff_kai[ed_id]][0][ed_rem+1];
        if(depth[cand3] <= depth[cand4]){
            c2 = cand3;
        }else{
            c2 = cand4;
        }
        if(depth[c1] <= depth[c2]){
            return P(euler_tour[c1],arr[euler_tour[c1]]);
        }else{
            return P(euler_tour[c2],arr[euler_tour[c2]]);
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cartesian_tree????§??????¨euler_tour??§????±????
    int l;
    cin >> n >> l;
    arr.resize(n, 0);
    rep(i,n){
        cin >> arr[i];
    }
    if(n == 1){
        cout << arr[0] << endl;
        return 0;
    }
    tree.resize(n);
    make_cartesian_tree();
    euler_tour.resize(2*n-1, 0),depth.resize(2*n-1, 0),diff.resize(2*n-2, 0),visit_id.resize(n, 0);
    int buff = 0;
    make_euler_tour(root,buff,0);
    rep(i,2*n-2){
        diff[i] = depth[i+1] - depth[i];
    }
    //?±RMQ???????£?
    m = 2*n-1;
    make_Block_arr();
    make_Sparse_Table();
    make_diff_kai();
    make_Table_Lookup();

    //?????¨?????????([l,r]???????°????)
    rep(i,n-l){
        P res = PM_RMQ(min(visit_id[i],visit_id[i+l-1]),max(visit_id[i],visit_id[i+l-1]));;
        cout << res.se << " ";
    }
    P res = PM_RMQ(min(visit_id[n-l],visit_id[n-1]),max(visit_id[n-l],visit_id[n-1]));
    cout << res.se << endl;
    return 0;
}

Compiler output:
a.cc: In function 'void make_cartesian_tree()':
a.cc:51:24: error: return-statement with a value, in function returning 'void' [-fpermissive]
   51 |                 return 0;
      |                        ^