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

#include <algorithm>
#include <codecvt>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <locale>
#include <memory>
#include <sstream>
#include <string>
#include <utility>

int32_t N;
int32_t p, q, r;
int32_t M;
int32_t X[5000];
int32_t Y[5000];

int32_t K[2][5000 * 3][2]; // 0: s 1: ??????°
auto K_end = K[0];
inline void insert(decltype(K_end) pos, int32_t ns, int32_t nn) {
  for (auto iter = K_end; iter != pos; --iter) {
    (*iter)[0] = (*(iter - 1))[0];
    (*iter)[1] = (*(iter - 1))[1];
  }
  (*pos)[0] = ns;
  (*pos)[1] = nn;
  ++K_end;
}
int main() {
  std::cin >> N;
  while (N != 0) {
    std::cin >> M >> p >> q >> r;
    for (int32_t i = 0; i < M; ++i) {
      std::cin >> X[i] >> Y[i];
    }
    K_end = K[0];
    (*K_end)[0] = 1;
    (*K_end)[1] = N;
    ++K_end;

    for (int32_t i = 0; i < M; ++i) {
      //??????
      int num_buf = 0;
      decltype(K_end) x_end_pos = nullptr;
      for (auto iter = K[i & 1]; iter != K_end; ++iter) {
        num_buf += (*iter)[1];
        if (X[i] <= num_buf) {
          auto nn = (num_buf - X[i]);
          (*iter)[1] -= nn;
          insert(iter + 1, (*iter)[0] + (*iter)[1], nn);
          x_end_pos = iter + 1;
          break;
        }
      }
      num_buf = X[i];
      decltype(K_end) y_end_pos = nullptr;
      for (auto iter = x_end_pos; iter != K_end; ++iter) {
        num_buf += (*iter)[1];
        if (Y[i] <= num_buf) {
          auto nn = (num_buf - Y[i]);
          (*iter)[1] -= nn;
          insert(iter + 1, (*iter)[0] + (*iter)[1], nn);
          y_end_pos = iter + 1;
          break;
        }
      }
      //????????????
      auto iter_out = K[(i + 1) & 1];
      for (auto iter = y_end_pos; iter != K_end; ++iter) {
        (*iter_out)[0] = (*iter)[0];
        (*iter_out)[1] = (*iter)[1];
        ++iter_out;
      }
      for (auto iter = x_end_pos; iter != y_end_pos; ++iter) {
        (*iter_out)[0] = (*iter)[0];
        (*iter_out)[1] = (*iter)[1];
        ++iter_out;
      }
      for (auto iter = K[i & 1]; iter != x_end_pos; ++iter) {
        (*iter_out)[0] = (*iter)[0];
        (*iter_out)[1] = (*iter)[1];
        ++iter_out;
      }
      K_end = iter_out;
    }

    //??????
    int32_t num_buf = 0;
    int32_t count = 0;
    for (auto iter = K[M & 1]; iter != K_end; ++iter) {
      auto n_numbuf = num_buf + (*iter)[1];
      auto add = std::min(r - ((*iter)[0] - 1), (*iter)[1]);
      if (num_buf < p) {
        add -= (p - 1 - num_buf);
      }
      if (q < n_numbuf) {
        add = std::min(add, q - num_buf);
      }
      count += std::max(0, add);
      num_buf = n_numbuf;
    }
    std::cout << count << '\\n';

    std::cin >> N;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <codecvt>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <locale>
#include <memory>
#include <sstream>
#include <string>
#include <utility>

int32_t N;
int32_t p, q, r;
int32_t M;
int32_t X[5000];
int32_t Y[5000];

int32_t K[2][5000 * 3][2]; // 0: s 1: ?????°
auto K_end = K[0];
inline void insert(decltype(K_end) pos, int32_t ns, int32_t nn) {
  for (auto iter = K_end; iter != pos; --iter) {
    (*iter)[0] = (*(iter - 1))[0];
    (*iter)[1] = (*(iter - 1))[1];
  }
  (*pos)[0] = ns;
  (*pos)[1] = nn;
  ++K_end;
}
int main() {
  std::cin >> N;
  while (N != 0) {
    std::cin >> M >> p >> q >> r;
    for (int32_t i = 0; i < M; ++i) {
      std::cin >> X[i] >> Y[i];
    }
    K_end = K[0];
    (*K_end)[0] = 1;
    (*K_end)[1] = N;
    ++K_end;

    for (int32_t i = 0; i < M; ++i) {
      //??????
      int num_buf = 0;
      decltype(K_end) x_end_pos = nullptr;
      for (auto iter = K[i & 1]; iter != K_end; ++iter) {
        num_buf += (*iter)[1];
        if (X[i] <= num_buf) {
          auto nn = (num_buf - X[i]);
          (*iter)[1] -= nn;
          insert(iter + 1, (*iter)[0] + (*iter)[1], nn);
          x_end_pos = iter + 1;
          break;
        }
      }
      num_buf = X[i];
      decltype(K_end) y_end_pos = nullptr;
      for (auto iter = x_end_pos; iter != K_end; ++iter) {
        num_buf += (*iter)[1];
        if (Y[i] <= num_buf) {
          auto nn = (num_buf - Y[i]);
          (*iter)[1] -= nn;
          insert(iter + 1, (*iter)[0] + (*iter)[1], nn);
          y_end_pos = iter + 1;
          break;
        }
      }
      //????????????
      auto iter_out = K[(i + 1) & 1];
      for (auto iter = y_end_pos; iter != K_end; ++iter) {
        (*iter_out)[0] = (*iter)[0];
        (*iter_out)[1] = (*iter)[1];
        ++iter_out;
      }
      for (auto iter = x_end_pos; iter != y_end_pos; ++iter) {
        (*iter_out)[0] = (*iter)[0];
        (*iter_out)[1] = (*iter)[1];
        ++iter_out;
      }
      for (auto iter = K[i & 1]; iter != x_end_pos; ++iter) {
        (*iter_out)[0] = (*iter)[0];
        (*iter_out)[1] = (*iter)[1];
        ++iter_out;
      }
      K_end = iter_out;
    }

    //??????
    int32_t num_buf = 0;
    int32_t count = 0;
    for (auto iter = K[M & 1]; iter != K_end; ++iter) {
      auto n_numbuf = num_buf + (*iter)[1];
      auto add = std::min(r - ((*iter)[0] - 1), (*iter)[1]);
      if (num_buf < p) {
        add -= (p - 1 - num_buf);
      }
      if (q < n_numbuf) {
        add = std::min(add, q - num_buf);
      }
      count += std::max(0, add);
      num_buf = n_numbuf;
    }
    std::cout << count << '\\n';
  }
}

#if 0
#include <codecvt>
// utility wrapper to adapt locale-bound facets for wstring/wbuffer convert
template<class Facet>
struct deletable_facet : Facet
{
	template<class ...Args>
	deletable_facet(Args&& ...args) : Facet(std::forward<Args>(args)...) {}
	~deletable_facet() {}
};

int main()
{
	// UTF-8 narrow multibyte encoding
	std::string data = u8"z\\u00df\\u6c34\\U0001f34c";
	// or u8"z???°´????"
	// or "\\x7a\\xc3\\x9f\\xe6\\xb0\\xb4\\xf0\\x9f\\x8d\\x8c";

	std::ofstream("text.txt") << data;

	// using system-supplied locale's codecvt facet
	std::wifstream fin("text.txt");
	// reading from wifstream will use codecvt<wchar_t, char, mbstate_t>
	// this locale's codecvt converts UTF-8 to UCS4 (on systems such as Linux)
	fin.imbue(std::locale("en_US.UTF-8"));
	std::cout << "The UTF-8 file contains the following UCS4 code points: \\n";
	for (wchar_t c; fin >> c; )
		std::cout << "U+" << std::hex << std::setw(4) << std::setfill('0') << c << '\\n';

	// using standard (locale-independent) codecvt facet
	std::wstring_convert<
		deletable_facet<std::codecvt<char16_t, char, std::mbstate_t>>, char16_t> conv16;
	std::u16string str16 = conv16.from_bytes(data);

	std::cout << "The UTF-8 file contains the following UTF-16 code points: \\n";
	for (char16_t c : str16)
		std::cout << "U+" << std::hex << std::setw(4) << std::setfill('0') << c << '\\n';
}
#endif
#if 0
int main()
{
	std::locale jpn_loc_all("Japanese");
	std::locale jpn_loc(std::locale::classic(), "Japanese",
		std::locale::ctype);// | std::locale::collate);
	std::locale::global(jpn_loc);
	std::wcout.imbue(jpn_loc);
	if(1){
		std::wifstream wifs(L"in.txt");

		if (!wifs) {
			std::wcout << L"ifs.fail()\\n";
		}
		std::wstring buf;
		std::getline(wifs, buf, L'@');
		if (buf.empty()) {
			std::wcout << L"wbuf.empty()\\n";
		}
		std::wcout << buf << std::endl;
		std::wcout << L"------------------" << std::endl;
	}
	{
		std::ifstream ifs("in.txt");
		if (!ifs) {
			std::cout << "ifs.fail()\\n";
		}
		std::string buf;
		std::getline(ifs, buf, '@');
		if (buf.empty()) {
			std::cout << "buf.empty()\\n";
		}
		std::cout << buf << std::endl;
		std::wstring wstr;
		{
			size_t     length = buf.size();
			auto  wc = std::make_unique< wchar_t[]>(length + 1);
			mbstowcs_s(&length, wc.get(), length + 1, buf.c_str(), length); // wc ??????????????????????????\\???
			wstr = wc.get();
		}
		std::wcout << L"------------------" << std::endl;
		if (wstr.empty()) {
			std::wcout << L"buf.empty()\\n";
		}
		std::wcout << wstr << std::endl;
	}
}
#endif