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
#define scanf scanf_s

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 ((1u << ++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] = std::min(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]);
  }
};

template <typename Monoid, typename Operand> class SegTree {
  std::vector<Monoid> tree;
  size_t size;
  int32 i;

public:
  //?????????????????????
  SegTree(size_t length) {
    --length;
    size = length;
    if (size & 0xffff0000) {
      size = size & 0xffff0000;
    }
    if (size & 0xff00ff00) {
      size = size & 0xff00ff00;
    }
    if (size & 0xf0f0f0f0) {
      size = size & 0xf0f0f0f0;
    }
    if (size & 0xcccccccc) {
      size = size & 0xcccccccc;
    }
    if (size & 0xaaaaaaaa) {
      size = size & 0xaaaaaaaa;
    }
    if (length) {
      size <<= 1;
    } else {
      size = 1;
    }
    tree.resize(size << 1);
  }
  //?????????
  void set() {
    i = size << 1;
    while (size < i--) {
      tree[i].set();
    }
    ++i;
    while (--i) {
      tree[i] = tree[i << 1] + tree[i << 1 | 1];
    }
  }
  // tree[index]???data??§??´??°
  void update(size_t index, const Operand &data) {
    index += size;
    tree[index] = tree[index] * data;
    while (index >>= 1)
      tree[index] = tree[index << 1] + tree[index << 1 | 1];
  }
  //[begin,end)???????????????????????????
  Monoid range(size_t begin, size_t end) {
    Monoid retL, retR;
    for (begin += size, end += size; begin < end; begin >>= 1, end >>= 1) {
      if (begin & 1)
        retL = retL + tree[begin++];
      if (end & 1)
        retR = tree[end - 1] + retR;
    }
    return retL + retR;
  }
  // range(0,length)
  Monoid all(void) { return tree[1]; }
};

struct SUB {
  int32 e;
  bool a;
  SUB() { set(); }
  SUB(int32 x, bool y) {
    e = x;
    a = y;
  }
  void set() { a = false; }
  SUB operator*(const SUB &other) {
    if (other.a)
      return other;
    return *this;
  }
};
struct MIN {
  int32 e;
  MIN() { set(); }
  MIN(int32 x) { e = x; }
  void set() { e = 2147483647; }
  MIN operator+(const MIN &other) {
    if (e < other.e)
      return *this;
    return other;
  };
  MIN operator*(const SUB &other) {
    if (other.a)
      return MIN(other.e);
    return *this;
  }
};

int main(void) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  uint32 n, l;
  std::cin >> n >> l;
  std::vector<uint32> a(n);
  rep(i, 0, n) { std::cin >> a[i]; }

  StaticRMQ<uint32> S(a);
  SegTree<MIN, SUB> T(n);
  rep(i, 0, n) T.update(i, SUB(a[i], 1));
  std::vector<uint32> ans(n - l + 1);
  uint32 c = 0;
  //*
  rep(i, 0, n - l + 1) {
    ans[i] = T.range(i, i + l).e;
    if (a[S(i, i + l)] != T.range(i, i + l).e) {
      // std::cout << i << "###\\n";
      ++c;
    }
  }
  if (0) {
    return 0;
  }

  std::cout << ans[0];
  rep(i, 1, n - l + 1) { std::cout << " " << ans[i]; }
  std::cout << "\\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;

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 ((1u << ++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] = std::min(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]);
  }
};

template <typename Monoid, typename Operand> class SegTree {
  std::vector<Monoid> tree;
  size_t size;
  int32 i;

public:
  //?????????????????????
  SegTree(size_t length) {
    --length;
    size = length;
    if (size & 0xffff0000) {
      size = size & 0xffff0000;
    }
    if (size & 0xff00ff00) {
      size = size & 0xff00ff00;
    }
    if (size & 0xf0f0f0f0) {
      size = size & 0xf0f0f0f0;
    }
    if (size & 0xcccccccc) {
      size = size & 0xcccccccc;
    }
    if (size & 0xaaaaaaaa) {
      size = size & 0xaaaaaaaa;
    }
    if (length) {
      size <<= 1;
    } else {
      size = 1;
    }
    tree.resize(size << 1);
  }
  //?????????
  void set() {
    i = size << 1;
    while (size < i--) {
      tree[i].set();
    }
    ++i;
    while (--i) {
      tree[i] = tree[i << 1] + tree[i << 1 | 1];
    }
  }
  // tree[index]???data??§??´??°
  void update(size_t index, Operand &data) {
    index += size;
    tree[index] = tree[index] * data;
    while (index >>= 1)
      tree[index] = tree[index << 1] + tree[index << 1 | 1];
  }
  //[begin,end)???????????????????????????
  Monoid range(size_t begin, size_t end) {
    Monoid retL, retR;
    for (begin += size, end += size; begin < end; begin >>= 1, end >>= 1) {
      if (begin & 1)
        retL = retL + tree[begin++];
      if (end & 1)
        retR = tree[end - 1] + retR;
    }
    return retL + retR;
  }
  // range(0,length)
  Monoid all(void) { return tree[1]; }
};

struct SUB {
  int32 e;
  bool a;
  SUB() { set(); }
  SUB(int32 x, bool y) {
    e = x;
    a = y;
  }
  void set() { a = false; }
  SUB operator*(const SUB &other) {
    if (other.a)
      return other;
    return *this;
  }
};
struct MIN {
  int32 e;
  MIN() { set(); }
  MIN(int32 x) { e = x; }
  void set() { e = 2147483647; }
  MIN operator+(const MIN &other) {
    if (e < other.e)
      return *this;
    return other;
  };
  MIN operator*(const SUB &other) {
    if (other.a)
      return MIN(other.e);
    return *this;
  }
};

int main(void) {
  uint32 n, l;
  scanf("%u %u", &n, &l);
  std::vector<uint32> a(n);
  rep(i, 0, n) scanf("%u", &a[i]);
  StaticRMQ<uint32> S(a);
  /*
  SegTree<MIN, SUB> T(n);
  rep(i, 0, n)
          T.update(i, SUB(a[i], 1));
  rep(i, 0, n - l + 1) {
          if (a[S(i, i + l)] != T.range(i, i + l).e) {
                  std::cout << i << "###\\n";
          }
  }
  /*/
  printf("%u", a[S(0, l)]);
  rep(i, 1, n - l + 1) { printf(" %u", a[S(i, i + l)]); }
  printf("\\n");
  //*/
  return 0;
}