Problem p00782 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>
#define DIF 5000
#define all(n) n.begin(), n.end()
using namespace std;

void draw(vector<vector<bool>> &graph, int sx, int sy, int ex, int ey) {
  for (int i = sy; i < ey; i++)
    for (int j = sx; j < ex; j++)
      graph[i][j] = true;
}

int main() {
  int n, NARI = 1;
  while (cin >> n, n) {
    vector<double> X, Y, X1, X2, Y1, Y2;
    vector<vector<bool>> graph;
    graph.resize(310);

    for (int i = 0; i < 310; i++) {
      graph[i].resize(310);
      for (int j = 0; j < 310; j++)
        graph[i][j] = false;
    }

    for (int i = 0; i < n; i++) {
      double x, y, r;
      cin >> x >> y >> r;
      X.push_back(x + r), X.push_back(x - r);
      Y.push_back(y + r), Y.push_back(y - r);
      X1.push_back(x - r), Y1.push_back(y - r);
      X2.push_back(x + r), Y2.push_back(y + r);
    }

    sort(all(X));
    sort(all(Y));
    X.erase(unique(all(X)), X.end());
    Y.erase(unique(all(Y)), Y.end());

    for (int i = 0; i < n; i++) {

      X1[i] = find(all(X), X1[i]) - X.begin();
      Y1[i] = find(all(Y), Y1[i]) - Y.begin();

      X2[i] = find(all(X), X2[i]) - X.begin();
      Y2[i] = find(all(Y), Y2[i]) - Y.begin();
    }

    for (int i = 0; i < n; i++)
      draw(graph, X1[i], Y1[i], X2[i], Y2[i]);

    double ans = 0;
    for (int i = 0; i < Y.size() - 1; i++)
      for (int j = 0; j < X.size() - 1; j++)
        if (graph[i][j])
          ans += (X[j + 1] - X[j]) * (Y[i + 1] - Y[i]);

    cout << NARI++ << setiosflags(ios::fixed) << setprecision(2) << " " << ans
         << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

int main() {

  int n, cnt = 1;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    set<P> graph;
    P p[2] = {P(0, 0), P(0, 0)};
    double _x, _y, _r;
    rep(k, n) {
      cin >> _x >> _y >> _r;
      P lb, rt;

      lb = P(_x * 10 - _r * 10, _y * 10 - _r * 10),
      rt = P(_x * 10 + _r * 10, _y * 10 + _r * 10);
      if (p[0].F <= lb.F && lb.F <= p[1].F && p[0].F <= rt.F &&
          rt.F <= p[1].F && p[0].S <= lb.S && lb.S <= p[1].S &&
          p[0].S <= rt.S && rt.S <= p[1].S)
        continue;
      if ((p[1].F - p[0].F) * (p[1].S - p[0].S) <
          (rt.F - lb.F) * (rt.S - lb.S)) {
        p[0] = lb, p[1] = rt;
      }
      for (int i = lb.F; i < rt.F; i++) {
        for (int j = lb.S; j < rt.S; j++) {
          graph.insert(P(i, j));
        }
      }
    }
    cout << setiosflags(ios::fixed) << setprecision(2) << cnt++ << " "
         << (double)graph.size() / 100.0 << endl;
  }
  return 0;
}