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

#include <cstdio>
#include <functional>
#include <vector>

using namespace std;

template <class RandomIt> struct swin_result {
  RandomIt first, last;
  typename RandomIt::value_type top;
  typename RandomIt::difference_type number, largest, smallest;
  swin_result() {}
  swin_result(typename RandomIt::value_type init)
      : top(init), number(0), largest(0), smallest(0) {}
};

template <class RandomIt, class UnaryPredicate, class BinaryOperation,
          class BinaryInvOperation,
          class BinaryPredicate = less<typename RandomIt::value_type>>
swin_result<RandomIt>
slide_window(RandomIt first, RandomIt last, UnaryPredicate unary_p,
             BinaryOperation binary_op, BinaryInvOperation binary_invop,
             typename RandomIt::value_type e,
             BinaryPredicate binary_p = less<typename RandomIt::value_type>()) {
  swin_result<RandomIt> res(e);
  typename RandomIt::value_type cur = e;
  typename RandomIt::difference_type len = 0;
  for (RandomIt open = first, close = open; open < last; ++open) {
    while (!unary_p(cur) && close < last) {
      if (binary_p(res.top, cur)) {
        res.top = cur;
        res.first = open;
        res.last = close;
      }

      cur = binary_op(cur, *close);
      ++close;
      ++len;
    }

    if (unary_p(cur)) {
      res.number += len + 1;
      if (res.largest < len)
        res.largest = len;
      if (res.smallest > len || !res.smallest)
        res.smallest = len;
    }

    cur = binary_invop(cur, *open);
    --len;
  }

  return res;
}

int main() {
  size_t N;
  int S;
  scanf("%zu %d", &N, &S);

  vector<int> a(N);
  for (size_t i = 0; i < N; ++i)
    scanf("%d", &a[i]);

  auto add = [](int lhs, int rhs) -> int { return lhs + rhs; };
  auto sub = [](int lhs, int rhs) -> int { return lhs - rhs; };

  swin_result<vector<int>::iterator> res = slide_window(
      a.begin(), a.end(), [S](int lhs) -> bool { return lhs >= S; }, add, sub,
      0);

  printf("%td\\n", res.smallest);
  return 0;
}

Error: 0
[0, 3]
[4, 4]


Fixed submission:

#include <cstdio>
#include <functional>
#include <vector>

using namespace std;

template <class RandomIt> struct swin_result {
  RandomIt first, last;
  typename RandomIt::value_type top;
  typename RandomIt::difference_type number, largest, smallest;
  swin_result() {}
  swin_result(typename RandomIt::value_type init)
      : top(init), number(0), largest(0), smallest(0) {}
};

template <class RandomIt, class UnaryPredicate, class BinaryOperation,
          class BinaryInvOperation,
          class BinaryPredicate = less<typename RandomIt::value_type>>
swin_result<RandomIt>
slide_window(RandomIt first, RandomIt last, UnaryPredicate unary_p,
             BinaryOperation binary_op, BinaryInvOperation binary_invop,
             typename RandomIt::value_type e,
             BinaryPredicate binary_p = less<typename RandomIt::value_type>()) {
  swin_result<RandomIt> res(e);
  typename RandomIt::value_type cur = e;
  typename RandomIt::difference_type len = 0;
  for (RandomIt open = first, close = open; open != last; ++open) {
    while (!unary_p(cur) && close < last) {
      if (binary_p(res.top, cur)) {
        res.top = cur;
        res.first = open;
        res.last = close;
      }

      cur = binary_op(cur, *close);
      ++close;
      ++len;
    }

    if (close == last)
      break;

    res.number += len + 1;
    if (res.largest < len)
      res.largest = len;
    if (res.smallest > len || !res.smallest)
      fprintf(stderr, "[%tu, %tu]\\n", open - first, close - first),
          res.smallest = len;

    cur = binary_invop(cur, *open);
    if (open++ == close) {
      ++close;
    } else {
      --len;
    }
  }

  return res;
}

int main() {
  size_t N;
  int S;
  scanf("%zu %d", &N, &S);

  vector<int> a(N);
  for (size_t i = 0; i < N; ++i)
    scanf("%d", &a[i]);

  auto add = [](int lhs, int rhs) -> int { return lhs + rhs; };
  auto sub = [](int lhs, int rhs) -> int { return lhs - rhs; };

  swin_result<vector<int>::iterator> res = slide_window(
      a.begin(), a.end(), [S](int lhs) -> bool { return lhs >= S; }, add, sub,
      0);

  printf("%td\\n", res.smallest);
  return 0;
}