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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;

struct Event {
  ll kind, depth, x1, x2, y1, y2;
};
bool operator<(const Event &e, const Event &f) {
  if (e.depth != f.depth)
    return e.depth < f.depth;
  if (e.kind != f.kind)
    return e.kind < f.kind;
  return e.x1 < f.x1;
}

int cnt[101][101];

int main() {
  int N, K;
  cin >> N >> K;
  set<ll> x, y;
  int x1, x2, y1, y2, d1, d2;
  vector<Event> evs;
  for (int i = 0; i < N; ++i) {
    cin >> x1 >> y1 >> d1 >> x2 >> y2 >> d2;
    x.insert(x1);
    x.insert(x2);
    y.insert(y1);
    y.insert(y2);
    evs.push_back((Event){0, d1, x1, x2, y1, y2});
    evs.push_back((Event){1, d2, x1, x2, y1, y2});
  }

  map<ll, ll> ix, iy, rx, ry;
  for (set<ll>::iterator it = x.begin(); it != x.end(); ++it) {
    if (ix.find(*it) == ix.end()) {
      ll p = ix.size();
      ix[*it] = p;
      rx[p] = *it;
    }
  }
  for (set<ll>::iterator it = y.begin(); it != y.end(); ++it) {
    if (iy.find(*it) == iy.end()) {
      ll p = iy.size();
      iy[*it] = p;
      ry[p] = *it;
    }
  }

  for (int i = 0; i < evs.size(); ++i) {
    evs[i].x1 = ix[evs[i].x1];
    evs[i].x2 = ix[evs[i].x2];
    evs[i].y1 = iy[evs[i].y1];
    evs[i].y2 = iy[evs[i].y2];
  }

  sort(evs.begin(), evs.end());
  memset(cnt, 0, sizeof(cnt));

  ll idx = 0, pd = 0;
  ll ca = 0, ans = 0;
  while (idx < evs.size()) {
    ll cd = evs[idx].depth;
    if (ca > 0) {
      ans += ca * (cd - pd);
      ca = 0;
    }
    pd = cd;
    while (idx < evs.size()) {
      if (evs[idx].depth != cd)
        break;
      if (evs[idx].kind == 0) {
        for (int i = evs[idx].x1; i < evs[idx].x2; ++i) {
          for (int j = evs[idx].y1; j < evs[idx].y2; ++j)
            cnt[j][i]++;
        }
      } else {
        for (int i = evs[idx].x1; i < evs[idx].x2; ++i) {
          for (int j = evs[idx].y1; j < evs[idx].y2; ++j)
            cnt[j][i]--;
        }
      }
      idx++;
    }

    // calc
    // cout<<idx<<endl;
    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j) {
        // cout<<cnt[i][j]<<" ";
        if (cnt[i][j] >= K) {
          ca += (ll)(rx[j + 1] - rx[j]) * (ry[i + 1] - ry[i]);
        }
      }
      // cout<<endl;
    }
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;

struct Event {
  int kind, depth, x1, x2, y1, y2;
};
bool operator<(const Event &e, const Event &f) {
  if (e.depth != f.depth)
    return e.depth < f.depth;
  if (e.kind != f.kind)
    return e.kind < f.kind;
  return e.x1 < f.x1;
}

int cnt[51][51];

int main() {
  int N, K;
  cin >> N >> K;
  set<int> x, y;
  int x1, x2, y1, y2, d1, d2;
  vector<Event> evs;
  for (int i = 0; i < N; ++i) {
    cin >> x1 >> y1 >> d1 >> x2 >> y2 >> d2;
    x.insert(x1);
    x.insert(x2);
    y.insert(y1);
    y.insert(y2);
    evs.push_back((Event){0, d1, x1, x2, y1, y2});
    evs.push_back((Event){1, d2, x1, x2, y1, y2});
  }

  map<int, int> ix, iy, rx, ry;
  for (set<int>::iterator it = x.begin(); it != x.end(); ++it) {
    if (ix.find(*it) == ix.end()) {
      int p = ix.size();
      ix[*it] = p;
      rx[p] = *it;
      // cout<<*it<<" = "<<p<<endl;
    }
  }
  for (set<int>::iterator it = y.begin(); it != y.end(); ++it) {
    if (iy.find(*it) == iy.end()) {
      int p = iy.size();
      iy[*it] = p;
      ry[p] = *it;
    }
  }

  for (int i = 0; i < evs.size(); ++i) {
    evs[i].x1 = ix[evs[i].x1];
    evs[i].x2 = ix[evs[i].x2];
    evs[i].y1 = iy[evs[i].y1];
    evs[i].y2 = iy[evs[i].y2];
  }

  sort(evs.begin(), evs.end());
  memset(cnt, 0, sizeof(cnt));

  int idx = 0, pd = 0;
  ll ca = 0, ans = 0;
  while (idx < evs.size()) {
    int cd = evs[idx].depth;
    if (ca > 0) {
      ans += ca * (cd - pd);
      ca = 0;
    }
    pd = cd;
    while (idx < evs.size()) {
      if (evs[idx].depth != cd)
        break;
      if (evs[idx].kind == 0) {
        for (int i = evs[idx].x1; i < evs[idx].x2; ++i) {
          for (int j = evs[idx].y1; j < evs[idx].y2; ++j)
            cnt[j][i]++;
        }
      } else {
        for (int i = evs[idx].x1; i < evs[idx].x2; ++i) {
          for (int j = evs[idx].y1; j < evs[idx].y2; ++j)
            cnt[j][i]--;
        }
      }
      idx++;
    }

    // calc
    // cout<<idx<<endl;
    for (int i = 0; i < 51; ++i) {
      for (int j = 0; j < 51; ++j) {
        // cout<<cnt[i][j]<<" ";
        if (cnt[i][j] >= K) {
          ca += (ll)(rx[j + 1] - rx[j]) * (ry[i + 1] - ry[i]);
        }
      }
      // cout<<endl;
    }
  }

  cout << ans << endl;
  return 0;
}