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


#include <algorithm>
#include <iostream>
#include <numeric>
#include <tuple>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_) : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

int main() {
  int N, K, C;
  std::cin >> N >> K >> C;
  std::string S;
  std::cin >> S;
  std::vector<int> work;
  for (int i : range(0, N)) {
    if (S[i] == 'o') {
      work.push_back(i);
    }
  }
  int W = work.size();
  std::vector<int> L(K);
  {
    int l = 0, r = 0;
    for (int i : range(0, K)) {
      L[i] = work[l];
      if (i + 1 == K) {
        break;
      }
      while (work[r] < work[l] + C + 1) {
        ++r;
      }
      l = r;
    }
  }
  std::vector<int> R(K);
  {
    int l = W - 1, r = W - 1;
    for (int i : revrange(0, K)) {
      R[i] = work[r];
      if (i == 0) {
        break;
      }
      while (work[l] > work[r] - C - 1) {
        --l;
      }
      r = l;
    }
  }
  for (int i : range(0, K)) {
    if (L[i] == R[i]) {
      std::cout << L[i] + 1 << '\\n';
    }
  }
  return 0;
}


Error: -11

Fixed submission:


#include <algorithm>
#include <iostream>
#include <numeric>
#include <tuple>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_) : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

int main() {
  int N, K, C;
  std::cin >> N >> K >> C;
  std::string S;
  std::cin >> S;
  std::vector<int> work;
  for (int i : range(0, N)) {
    if (S[i] == 'o') {
      work.push_back(i);
    }
  }
  int W = work.size();
  std::vector<int> L(K);
  {
    int l = 0, r = 0;
    for (int i : range(0, K)) {
      L[i] = work[l];
      while (work[r] < work[l] + C + 1) {
        ++r;
      }
      l = r;
    }
  }
  std::vector<int> R(K);
  {
    int l = W - 1, r = W - 1;
    for (int i : revrange(0, K)) {
      R[i] = work[r];
      while (work[l] > work[r] - C - 1) {
        --l;
      }
      r = l;
    }
  }
  for (int i : range(0, K)) {
    if (L[i] == R[i]) {
      std::cout << L[i] + 1 << '\\n';
    }
  }
  return 0;
}