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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;

struct Time {
  int x;
  int y1;
  int y2;
  int act;
  Time(int x, int y1, int y2, int act) : x(x), y1(y1), y2(y2), act(act) {}
  bool operator<(const Time &right) const { return x < right.x; }
};

int compress(vector<int> &x, map<int, int> &zip, vector<int> &unzip) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (int i = 0; i < x.size(); i++) {
    zip[x[i]] = i;
    unzip.push_back(x[i]);
  }
  return x.size();
}

int main() {
  int N, x1, x2, y1, y2;
  vector<int> ylist, yunzip;
  map<int, int> yzip;
  vector<Time> Timeline;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x1 >> y1 >> x2 >> y2;
    ylist.push_back(y1);
    ylist.push_back(y2);
    Time t1(x1, y1, y2, 0);
    Time t2(x2, y1, y2, 1);
    Timeline.push_back(t1);
    Timeline.push_back(t2);
  }

  compress(ylist, yzip, yunzip);
  sort(Timeline.begin(), Timeline.end());

  vector<int> range(4000, 0);

  ull S = 0ull;
  int prevx = Timeline[0].x;
  ull tate;
  int prev;
  int i = 0;
  while (i < Timeline.size()) {
    tate = 0ull;
    prev = -1;
    for (int j = 0; j < 4000; j++) {
      if (prev == -1) {
        if (range[j] >= 1) {
          prev = j;
        }
      } else {
        if (range[j] == 0) {
          tate += (yunzip[j] - yunzip[prev]);
          prev = -1;
        }
      }
    }
    S += (Timeline[i].x - prevx) * tate;
    /*
    cout << tate << endl;
    cout << Timeline[i].x - prevx << endl;
    cout << S << endl;
    */
    prevx = Timeline[i].x;
    int k = i;
    while (i < Timeline.size() && Timeline[i].x == Timeline[k].x) {
      Time t = Timeline[i];
      if (t.act == 0) {
        for (int j = yzip[t.y1]; j < yzip[t.y2]; j++) {
          range[j] += 1;
        }
      } else {
        for (int j = yzip[t.y1]; j < yzip[t.y2]; j++) {
          range[j] -= 1;
        }
      }
      i++;
    }
  }
  cout << S << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 3;
typedef unsigned long long ull;

struct Rec {
  ull x1, y1, x2, y2;
};

ull UnionOfRange(set<int> &now, vector<Rec> &Recs) {
  if (now.empty()) {
    return 0;
  }

  ull res = 0;
  int N = Recs.size();
  vector<pair<int, int>> events2;
  int j = 0;
  for (set<int>::iterator itra = now.begin(); itra != now.end(); itra++) {
    events2.push_back(make_pair(Recs[*itra].y1, j));
    events2.push_back(make_pair(Recs[*itra].y2, j + N));
    j++;
  }

  sort(events2.begin(), events2.end());

  int i = 0;
  int prev2 = events2[0].first;
  set<int> now2;

  while (i < events2.size()) {
    if (!now2.empty()) {
      res += (events2[i].first - prev2);
    }
    prev2 = events2[i].first;
    if (events2[i].second < N) {
      now2.insert(events2[i].second);
    } else {
      now2.erase(events2[i].second - N);
    }
    i++;
  }
  return res;
}

ull UnionOfRectangle(vector<Rec> &Recs) {
  int N = Recs.size();
  vector<pair<int, int>> events;
  for (int i = 0; i < N; i++) {
    events.push_back(make_pair(Recs[i].x1, i + N));
    events.push_back(make_pair(Recs[i].x2, i));
  }
  sort(events.begin(), events.end());

  set<int> now;
  int i = 0;
  int prev = events[0].first;
  ull res = 0;
  while (i < events.size()) {
    res += (events[i].first - prev) * UnionOfRange(now, Recs);
    prev = events[i].first;
    while (events[i].first <= prev) {
      if (events[i].second < N) {
        now.erase(events[i].second);
      } else {
        now.insert(events[i].second - N);
      }
      i++;
    }
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<Rec> Recs;
  ull x1, y1, x2, y2;
  for (int i = 0; i < N; i++) {
    cin >> x1 >> y1 >> x2 >> y2;
    Rec R = {x1, y1, x2, y2};
    Recs.push_back(R);
  }
  cout << UnionOfRectangle(Recs) << endl;
  return 0;
}