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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
stack<P> stu[10003];
vector<P> vec[10003];
int N, M, r;
int t[1003], n[1003], m[1003], s[1003];
int q;
int qs[55], qe[55], qm[55];

int cookies(int sx, int sy, int lx, int ly) {
  return max(min(sy, ly) - max(sx, lx), 0);
}

int main() {
  while (1) {
    memset(t, 0, sizeof(t));
    memset(n, 0, sizeof(n));
    memset(m, 0, sizeof(m));
    memset(s, 0, sizeof(s));
    memset(qs, 0, sizeof(qs));
    memset(qe, 0, sizeof(qe));
    memset(qm, 0, sizeof(qm));
    for (int i = 0; i < 10003; i++)
      vec[i].resize(0);
    for (int i = 0; i < 10003; i++) {
      while (1) {
        if (!stu[i].empty())
          stu[i].pop();
        else
          break;
      }
    }

    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    scanf("%d", &r);
    for (int i = 0; i < r; i++) {
      scanf("%d%d%d%d", &t[i], &n[i], &m[i], &s[i]);
      if (s[i] == 1)
        stu[m[i]].push(P(t[i], n[i]));
      if (s[i] == 0) {
        P p = stu[m[i]].top();
        stu[m[i]].pop();
        if (stu[m[i]].empty())
          vec[m[i]].push_back(P(p.first, t[i]));
      }
    }
    scanf("%d", &q);
    int i;
    for (i = 0; i < q; i++) {
      scanf("%d%d%d", &qs[i], &qe[i], &qm[i]);

      int res = 0;
      for (int j = 0; j < vec[qm[i]].size(); j++) {
        res += cookies(qs[i], qe[i], vec[qm[i]][j].first, vec[qm[i]][j].second);
      }

      printf("%d\\n", res);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
stack<P> stu[10003];
vector<P> vec[10003];
int N, M, r;
int t[1003], n[1003], m[1003], s[1003];
int q;
int qs[55], qe[55], qm[55];
int main() {
  while (1) {
    memset(t, 0, sizeof(t));
    memset(n, 0, sizeof(n));
    memset(m, 0, sizeof(m));
    memset(s, 0, sizeof(s));
    memset(qs, 0, sizeof(qs));
    memset(qe, 0, sizeof(qe));
    memset(qm, 0, sizeof(qm));
    for (int i = 0; i < 10003; i++)
      vec[i].resize(0);
    for (int i = 0; i < 10003; i++) {
      while (1) {
        if (!stu[i].empty())
          stu[i].pop();
        else
          break;
      }
    }

    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    scanf("%d", &r);
    for (int i = 0; i < r; i++) {
      scanf("%d%d%d%d", &t[i], &n[i], &m[i], &s[i]);
      if (s[i] == 1)
        stu[m[i]].push(P(t[i], n[i]));
      if (s[i] == 0) {
        P p = stu[m[i]].top();
        stu[m[i]].pop();
        if (stu[m[i]].empty())
          vec[m[i]].push_back(P(p.first, t[i]));
      }
    }
    scanf("%d", &q);
    int i;
    for (i = 0; i < q; i++) {
      scanf("%d%d%d", &qs[i], &qe[i], &qm[i]);

      int res = 0;

      int j;
      bool flg1 = true;
      for (j = 0; j < vec[qm[i]].size(); j++) {
        if (vec[qm[i]][j].first <= qs[i] && vec[qm[i]][j].second >= qs[i]) {
          res += vec[qm[i]][j].second - qs[i];
          flg1 = false;
          break;
        }
      }
      int l;
      bool flg2 = true;
      for (l = 0; j < vec[qm[i]].size(); l++) {
        if (vec[qm[i]][l].first <= qe[i] && vec[qm[i]][l].second >= qe[i]) {
          res += qe[i] - vec[qm[i]][l].first;
          flg2 = false;
          break;
        }
      }

      if (flg1 == false && flg2 == false) {
        for (int ru = j + 1; ru < l; ru++)
          res += vec[qm[i]][ru].second - vec[qm[i]][ru].first;
      }
      if (flg1 == true && flg2 == false) {
        for (int ru = 0; ru < l; ru++)
          res += vec[qm[i]][ru].second - vec[qm[i]][ru].first;
      }
      if (flg1 == true && flg2 == true) {
        for (int ru = 0; ru < vec[qm[i]].size(); ru++)
          res += vec[qm[i]][ru].second - vec[qm[i]][ru].first;
      }
      if (flg1 == false && flg2 == true) {
        for (int ru = j + 1; ru < vec[qm[i]].size(); ru++)
          res += vec[qm[i]][ru].second - vec[qm[i]][ru].first;
      }

      printf("%d\\n", res);
    }
  }
  return 0;
}