Problem p02357 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define rep(i, a, b) for (uint32 i = (a); i < (b); ++i)
#define bitget(a, b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define C(x) std::cout << #x << " : " << x << std::endl

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
/*
uint32 __builtin_ctz(uint32 c) {
        uint32 t = 31;
        if (c & 0x0000FFFF) { c &= 0x0000FFFF;t ^= 16; }
        if (c & 0x00FF00FF) { c &= 0x00FF00FF;t ^= 8; }
        if (c & 0x0F0F0F0F) { c &= 0x0F0F0F0F;t ^= 4; }
        if (c & 0x33333333) { c &= 0x33333333;t ^= 2; }
        if (c & 0x55555555) { c &= 0x55555555;t ^= 1; }
        return t;
}
uint32 __builtin_clz(uint32 c) {
        uint32 t = 31;
        if (c & 0xFFFF0000) { c &= 0xFFFF0000;t ^= 16; }
        if (c & 0xFF00FF00) { c &= 0xFF00FF00;t ^= 8; }
        if (c & 0xF0F0F0F0) { c &= 0xF0F0F0F0;t ^= 4; }
        if (c & 0xCCCCCCCC) { c &= 0xCCCCCCCC;t ^= 2; }
        if (c & 0xAAAAAAAA) { c &= 0xAAAAAAAA;t ^= 1; }
        return t;
}
uint32 __builtin_clrsb(uint32 c) {
        return c ? __builtin_clz(c) - 1 : 31;
}
//*/
template <typename T> class StaticRMQ {
  std::vector<std::vector<uint32>> table;
  std::vector<T> a;
  uint32 t;
  std::vector<uint32> L, R, part;
  uint32 bitmaskr[32] = {
      0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8, 0xFFFFFFF0, 0xFFFFFFE0,
      0xFFFFFFC0, 0xFFFFFF80, 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
      0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000, 0xFFFF0000, 0xFFFE0000,
      0xFFFC0000, 0xFFF80000, 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
      0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000, 0xF0000000, 0xE0000000,
      0xC0000000, 0x80000000,
  };
  uint32 bitmaskl[32] = {
      0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F,
      0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
      0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF, 0x0003FFFF,
      0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
      0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF,
      0x7FFFFFFF, 0xFFFFFFFF};

public:
  StaticRMQ(std::vector<T> &array) {
    a.resize(array.size());
    L.resize((array.size() + 0x1F) >> 5, 0);
    R.resize((array.size() + 0x1F) >> 5, 1);
    part.resize(array.size(), 0);
    t = 0;
    if (array.size() >> 5) {
      table.resize(31 ^ __builtin_clrsb(array.size() >> 5));
      table[0].resize((array.size() >> 5) + 1);
    } else {
      table.resize(1);
      table[0].resize(1);
    }
    while ((1 << ++t) < (array.size() >> 5)) {
      table[t].resize((array.size() >> 5) - (1 << t) + 1);
    }
    std::stack<uint32> s;
    uint32 m;
    for (t = 0; t < array.size(); ++t) {
      a[t] = array[t];
      part[t] |= 1 << (t & 0x1F);
      if (!(t & 0x1F)) {
        m = array[t];
        table[0][t >> 5] = t;
        while (!s.empty()) {
          s.pop();
        }
      }
      while (!s.empty()) {
        if (array[s.top()] > array[t]) {
          s.pop();
        } else {
          part[t] |= part[s.top()];
          break;
        }
      }
      s.push(t);
      if (m > array[t]) {
        m = array[t];
        R[t >> 5] |= 1 << (t & 0x1F);
        table[0][t >> 5] = t;
      }
      if (!((~t) & 0x1F)) {
        L[t >> 5] = part[t];
      }
    }
    for (t = 0; t + 1 < table.size(); ++t) {
      for (m = 0; m < table[t + 1].size(); ++m) {
        table[t + 1][m] = a[table[t][m]] < a[table[t][m + (1 << t)]]
                              ? table[t][m]
                              : table[t][m + (1 << t)];
      }
    }
  }
  uint32 operator()(uint32 begin, uint32 end) {
    uint32 l = (begin >> 5) + 1;
    uint32 r = --end >> 5;
    if (l < r) {
      t = 31 ^ __builtin_clz(r - l);
      if (a[table[t][l]] > a[table[t][r - (1 << t)]]) {
        t = table[t][r - (1 << t)];
      } else {
        t = table[t][l];
      }
      if (!(a[(begin & ~0x1F) |
              __builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F])] > a[t])) {
        t = (begin & ~0x1F) |
            __builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
      }
      if (!(a[(end & ~0x1F) |
              (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
            a[t])) {
        t = (end & ~0x1F) |
            (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
      }
      return t;
    }
    if (l == r) {
      t = (begin & ~0x1F) |
          __builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
      if (!(a[(end & ~0x1F) |
              (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
            a[t])) {
        t = (end & ~0x1F) |
            (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
      }
      return t;
    }
    return (end & ~0x1F) | __builtin_ctz(part[end] & bitmaskr[begin & 0x1F]);
  }
};

int main(void) {
  uint32 n, l;
  std::cin >> n >> l;
  // scanf("%d %d", &n, &l);
  std::vector<uint32> a(n);
  rep(i, 0, n) {
    std::cin >> a[i];
    // scanf("%d", &a[i]);
  }
  StaticRMQ<uint32> S(a);
  printf("%u", a[S(0, l)]);
  rep(i, 1, n - l + 1) { printf(" %u", a[S(i, i + l)]); }
  printf("\\n");
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define rep(i, a, b) for (uint32 i = (a); i < (b); ++i)
#define bitget(a, b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define C(x) std::cout << #x << " : " << x << std::endl

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
/*
uint32 __builtin_ctz(uint32 c) {
        uint32 t = 31;
        if (c & 0x0000FFFF) { c &= 0x0000FFFF;t ^= 16; }
        if (c & 0x00FF00FF) { c &= 0x00FF00FF;t ^= 8; }
        if (c & 0x0F0F0F0F) { c &= 0x0F0F0F0F;t ^= 4; }
        if (c & 0x33333333) { c &= 0x33333333;t ^= 2; }
        if (c & 0x55555555) { c &= 0x55555555;t ^= 1; }
        return t;
}
uint32 __builtin_clz(uint32 c) {
        uint32 t = 31;
        if (c & 0xFFFF0000) { c &= 0xFFFF0000;t ^= 16; }
        if (c & 0xFF00FF00) { c &= 0xFF00FF00;t ^= 8; }
        if (c & 0xF0F0F0F0) { c &= 0xF0F0F0F0;t ^= 4; }
        if (c & 0xCCCCCCCC) { c &= 0xCCCCCCCC;t ^= 2; }
        if (c & 0xAAAAAAAA) { c &= 0xAAAAAAAA;t ^= 1; }
        return t;
}
uint32 __builtin_clrsb(uint32 c) {
        return c ? __builtin_clz(c) - 1 : 31;
}
//*/
template <typename T> class StaticRMQ {
  std::vector<std::vector<uint32>> table;
  std::vector<T> a;
  uint32 t;
  std::vector<uint32> L, R, part;
  uint32 bitmaskr[32] = {
      0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8, 0xFFFFFFF0, 0xFFFFFFE0,
      0xFFFFFFC0, 0xFFFFFF80, 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
      0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000, 0xFFFF0000, 0xFFFE0000,
      0xFFFC0000, 0xFFF80000, 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
      0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000, 0xF0000000, 0xE0000000,
      0xC0000000, 0x80000000,
  };
  uint32 bitmaskl[32] = {
      0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F,
      0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
      0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF, 0x0003FFFF,
      0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
      0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF,
      0x7FFFFFFF, 0xFFFFFFFF};

public:
  StaticRMQ(std::vector<T> &array) {
    a.resize(array.size());
    L.resize((array.size() + 0x1F) >> 5, 0);
    R.resize((array.size() + 0x1F) >> 5, 1);
    part.resize(array.size(), 0);
    t = 0;
    if (array.size() >> 5) {
      table.resize(31 ^ __builtin_clrsb(array.size() >> 5));
      table[0].resize((array.size() >> 5) + 1);
    } else {
      table.resize(1);
      table[0].resize(1);
    }
    while ((1 << ++t) < (array.size() >> 5)) {
      table[t].resize((array.size() >> 5) - (1 << t) + 1);
    }
    std::stack<uint32> s;
    uint32 m;
    for (t = 0; t < array.size(); ++t) {
      a[t] = array[t];
      part[t] |= 1 << (t & 0x1F);
      if (!(t & 0x1F)) {
        m = array[t];
        table[0][t >> 5] = t;
        while (!s.empty()) {
          s.pop();
        }
      }
      while (!s.empty()) {
        if (array[s.top()] > array[t]) {
          s.pop();
        } else {
          part[t] |= part[s.top()];
          break;
        }
      }
      s.push(t);
      if (m > array[t]) {
        m = array[t];
        R[t >> 5] |= 1 << (t & 0x1F);
        table[0][t >> 5] = t;
      }
      if (!((~t) & 0x1F)) {
        L[t >> 5] = part[t];
      }
    }
    for (t = 0; t + 1 < table.size(); ++t) {
      for (m = 0; m < table[t + 1].size(); ++m) {
        table[t + 1][m] = a[table[t][m]] < a[table[t][m + (1 << t)]]
                              ? table[t][m]
                              : table[t][m + (1 << t)];
      }
    }
  }
  uint32 operator()(uint32 begin, uint32 end) {
    uint32 l = (begin >> 5) + 1;
    uint32 r = --end >> 5;
    if (l < r) {
      t = 31 ^ __builtin_clz(r - l);
      if (a[table[t][l]] > a[table[t][r - (1 << t)]]) {
        t = table[t][r - (1 << t)];
      } else {
        t = table[t][l];
      }
      if (!(a[(begin & ~0x1F) |
              __builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F])] > a[t])) {
        t = (begin & ~0x1F) |
            __builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
      }
      if (!(a[(end & ~0x1F) |
              (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
            a[t])) {
        t = (end & ~0x1F) |
            (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
      }
      return t;
    }
    if (l == r) {
      t = (begin & ~0x1F) |
          __builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
      if (!(a[(end & ~0x1F) |
              (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
            a[t])) {
        t = (end & ~0x1F) |
            (31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
      }
      return t;
    }
    return (end & ~0x1F) | __builtin_ctz(part[end] & bitmaskr[begin & 0x1F]);
  }
};

int main(void) {
  uint32 n, l;
  scanf("%d %d", &n, &l);
  std::vector<uint32> a(n);
  rep(i, 0, n) { scanf("%d", &a[i]); }
  StaticRMQ<uint32> S(a);
  printf("%u", a[S(0, l)]);
  rep(i, 1, n - l + 1) { printf(" %u", a[S(i, i + l)]); }
  printf("\\n");
  return 0;
}