Language: C++

#define NDEBUG
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
static constexpr double PI = 3.1415926535897932;

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
using intl32 = std::int_least32_t;
using intl64 = std::int_least64_t;
using uintl32 = std::uint_least32_t;
using uintl64 = std::uint_least64_t;

namespace n91
{
template <typename T>
auto scan(T &d) -> typename std::enable_if<std::is_same<T, std::string>::value>::type
{
    d = std::string();
    int c = 0;
    while (c < 'a' || 'z' < c)
        c = fgetc(stdin);
    while ('a' <= c && c <= 'z')
    {
        d.push_back(c);
        c = fgetc(stdin);
    }
}
template <typename T>
auto scan(T &d) -> typename std::enable_if<std::is_signed<T>::value ==
                                           std::is_same<T, std::string>::value>::type
{
    d = 0;
    int c = 0;
    while (c < '0' || '9' < c)
        c = fgetc(stdin);
    while ('0' <= c && c <= '9')
    {
        d = d * 10 + c - '0';
        c = fgetc(stdin);
    }
}
template <typename T>
auto scan(T &d) -> typename std::enable_if<std::is_signed<T>::value>::type
{
    d = 0;
    int c = 0;
    bool f = 0;
    while (c < '0' || '9' < c)
    {
        if (c == '-')
            f = 1;
        c = fgetc(stdin);
    }
    while ('0' <= c && c <= '9')
    {
        d = d * 10 + c - '0';
        c = fgetc(stdin);
    }
    if (f)
        d = -d;
}
}
#ifdef __GNUC__
inline std::uint_fast32_t popcount32(std::uint_fast32_t c)
{
    return __builtin_popcount(c);
}
#elif _MSC_VER_
inline std::uint_fast32_t popcount32(std::uint_fast32_t c)
{
    return _mm_popcnt_u32(c);
}
#else
std::uint_fast32_t popcount32(std::uint_fast32_t c)
{
    c = (c & 0x55555555) + ((c >> 1) & 0x55555555);
    c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
    c = (c + (c >> 4)) & 0x0F0F0F0F;
    c += c >> 8;
    c += c >> 16;
    return c & 0x3F;
}
#endif

template <typename T, size_t bitsize>
class WaveletMatrix
{
    using uint32 = std::uint_fast32_t;
    using uintl32 = std::uint_least32_t;
    std::vector<uintl32> dic[bitsize];
    uint32 border[bitsize];
    inline uint32 rank(const uint32 i, const uint32 index) const
    {
        return dic[i][index >> 4 & ~(uint32)1] + popcount32(dic[i][index >> 4 | 1] & ((uintl32)1 << (index & 0x1f)) - 1);
    }

  public:
    WaveletMatrix(std::vector<T> data)
    {
        const uint32 len = data.size(), block = ((len >> 5) + 1) << 1;
        for (uint32 i = 0; i < bitsize; ++i)
        {
            dic[i] = std::vector<uintl32>(block, 0);
        }
        std::vector<T> L(len), R(len);
        uint32 l, r;
        for (uint32 i = bitsize - 1; ~i; --i)
        {
            l = 0;
            r = 0;
            for (uint32 j = 0; j < len; ++j)
            {
                if (data[j] >> i & 1)
                {
                    R[r++] = data[j];
                    dic[j >> 4 & ~(uint32)1] |= (uintl32)1 << (j & 0x1f);
                }
                else
                {
                    L[l++] = data[j];
                }
            }
            border[i] = l;
            for (j = 1; j < block; j += 2)
            {
                dic[i][j + 1] = dic[i][j - 1] + popcount32(dic[i][j]);
            }
            std::swap(data, L);
            for (uint32 i = l; i < len; ++i)
            {
                data[i] = R[i - l];
            }
        }
    }
    T access(uint32 index) const
    {
        T ret = 0;
        for (uint32 i = bitsize - 1; ~i; --i)
        {
            ret <<= 1;
            if (dic[i][index >> 4 | 1] >> (index & 0x1f) & 1)
            {
                ret |= 1;
                index = rank(i, index) + border[i];
            }
            else
            {
                index -= rank(i, index);
            }
        }
        return ret;
    }
    T rank(uint32 begin, uint32 end, T data) const
    {
        for (uint32 i = bitsize - 1; ~i; --i)
        {
            if (data >> i & 1)
            {
                begin = rank(i, begin) + border[i];
                end = rank(i, end) + border[i];
            }
            else
            {
                begin -= rank(i, begin);
                end -= rank(i, end);
            }
        }
        return end - begin;
    }
    T quantile(uint32 begin, uint32 end, uint32 k) const
    {
        T ret = 0;
        for (uint32 i = bitsize - 1; ~i; --i)
        {
            const uint32 l = rank(i, begin), r = rank(i, end);
            ret <<= 1;
            if (r - l > k)
            {
                begin = l + border[i];
                end = r + border[i];
                ret |= 1;
            }
            else
            {
                begin -= l;
                end -= r;
                k -= r - l;
            }
        }
        return ret;
    }
    T rquantile(uint32 begin, uint32 end, uint32 k) const
    {
        return quantile(begin, end, end - begin - k - 1);
    }
    uint32 less_than(uint32 begin, uint32 end, const T data) const
    {
        uint32 ret = 0;
        for (uint32 i = bitsize - 1; ~i; --i)
        {
            if (data >> i & 1)
            {
                ret += end - begin + rank(i, begin) - rank(i, end);
                begin = rank(i, begin) + border[i];
                end = rank(i, end) + border[i];
            }
            else
            {
                begin -= rank(i, begin);
                end -= rank(i, end);
            }
        }
        return ret + end - begin;
    }
    uint32 more_than(uint32 begin, uint32 end, const T data) const
    {
        uint32 ret = 0;
        for (uint32 i = bitsize - 1; ~i; --i)
        {
            if (data >> i & 1)
            {
                begin = rank(i, begin) + border[i];
                end = rank(i, end) + border[i];
            }
            else
            {
                ret += rank(i, end) - rank(i, begin);
                begin -= rank(i, begin);
                end -= rank(i, end);
            }
        }
        return ret;
    }
    uint32 rangefreq(uint32 begin, uint32 end, T lower, T upper) const
    {
        return end - begin - less_than(begin, end, lower) - more_than(begin, end, upper);
    }
};

int main(void)
{
    uint32 n, l;
    n91::scan(n);
    n91::scan(l);
    std::vector<uint32> a(n);
    for (uint32 i = 0; i < n;++i)
        n91::scan(a[i]);
    WaveletMatrix<uint32, 30> W(a);
    printf("%u", W.rquantile(0, l, 0));
    for (uint32 i = 1; i <= n - l;++i){
        printf(" %u", W.rquantile(i, i + l, 0));
    }
    printf("\n");
    return 0;
}


Compiler output:
a.cc: In constructor 'WaveletMatrix<T, bitsize>::WaveletMatrix(std::vector<_Tp>)':
a.cc:143:18: error: 'j' was not declared in this scope
  143 |             for (j = 1; j < block; j += 2)
      |                  ^
a.cc: In instantiation of 'WaveletMatrix<T, bitsize>::WaveletMatrix(std::vector<_Tp>) [with T = long unsigned int; long unsigned int bitsize = 30]':
a.cc:267:34:   required from here
  267 |     WaveletMatrix<uint32, 30> W(a);
      |                                  ^
a.cc:135:46: error: no match for 'operator|=' (operand types are 'std::vector<unsigned int>' and 'WaveletMatrix<long unsigned int, 30>::uintl32' {aka 'unsigned int'})
  135 |                     dic[j >> 4 & ~(uint32)1] |= (uintl32)1 << (j & 0x1f);
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:38,
                 from /usr/include/c++/14/unordered_map:59,
                 from /usr/include/c++/14/functional:63,
                 from a.cc:12:
/usr/include/c++/14/cstddef:168:3: note: candidate: 'constexpr std::byte& std::operator|=(byte&, byte)'
  168 |   operator|=(byte& __l, byte __r) noexcept
      |   ^~~~~~~~
/usr/include/c++/14/cstddef:168:20: note:   no known conversion for argument 1 from 'std::vector<unsigned int>' to 'std::byte&'
  168 |   operator|=(byte& __l, byte __r) noexcept
      |              ~~~~~~^~~