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

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 100000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 1e9;
int main() {
  int n, m, r, q;
  while (cin >> n >> m && n && m) {
    vector<P> V[10000];
    int ps[n];
    REP(i, n) ps[i] = -1;
    cin >> r;
    REP(i, r) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      b--;
      c--;
      if (d == 1)
        ps[b] = a;
      else
        V[c].PB(MP(ps[b], a));
    }
    cin >> q;
    REP(i, q) {
      vector<P> VV;
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      c--;
      REP(j, V[c].size()) {
        P p = V[c][j];
        if (b < p.first || p.second < a)
          continue;
        if (p.first < a) {
          if (p.second <= b)
            VV.PB(MP(a, p.second));
          else
            VV.PB(MP(a, b));
        } else {
          if (p.second <= b)
            VV.PB(MP(p.first, p.second));
          else
            VV.PB(MP(p.first, b));
        }
      }
      sort(ALL(VV));
      int s = 0, e = 0, ans = 0;
      // REP(i,VV.size())cout<<VV[i].first<<" "<<VV[i].second<<endl;
      REP(j, VV.size()) {
        if (j == 0) {
          s = VV[j].first;
          e = VV[j].second;
          continue;
        }
        if (e >= VV[j].first) {
          e = max(e, VV[j].second);
        } else {
          ans += (e - s);
          s = VV[j].first;
          e = VV[j].second;
        }
        // cout<<s<<" "<<e<<endl;
      }
      ans += (e - s);
      cout << ans << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 100000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = 1e9;
int main() {
  int n, m, r, q;
  while (cin >> n >> m && n && m) {
    vector<P> V[1000];
    int ps[n];
    REP(i, n) ps[i] = -1;
    cin >> r;
    REP(i, r) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      b--;
      c--;
      if (d == 1)
        ps[b] = a;
      else
        V[c].PB(MP(ps[b], a));
    }
    cin >> q;
    REP(i, q) {
      vector<P> VV;
      int a, b, c, ans = 0;
      scanf("%d%d%d", &a, &b, &c);
      c--;
      REP(i, V[c].size()) {
        P p = V[c][i];
        if (b < p.first || p.second < a)
          continue;
        if (p.first < a) {
          if (p.second <= b)
            VV.PB(MP(a, p.second));
          else
            VV.PB(MP(a, b));
        } else {
          if (p.second <= b)
            VV.PB(MP(p.first, p.second));
          else
            VV.PB(MP(p.first, b));
        }
      }
      sort(ALL(VV));
      int s, e;
      // REP(i,VV.size())cout<<VV[i].first<<" "<<VV[i].second<<endl;
      REP(i, VV.size()) {
        if (i == 0) {
          s = VV[i].first;
          e = VV[i].second;
          continue;
        }
        if (e >= VV[i].first) {
          e = max(e, VV[i].second);
        } else {
          ans += (e - s);
          s = VV[i].first;
          e = VV[i].second;
        }
        // cout<<s<<" "<<e<<endl;
      }
      ans += (e - s);
      cout << ans << endl;
    }
  }
}