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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) begin(v), end(v)
bool mas[444][444];

void color(int x, int y, int gx, int gy) {
  for (int i = x; i < gx; i++) {
    for (int j = y; j < gy; j++)
      mas[i][j] = true;
  }
}

int main() {
  int n;
  int t = 0;
  while (cin >> n, n) {
    t++;
    memset(mas, false, sizeof(mas));
    vector<double> xx, yy;
    vector<double> X, Y;
    rep(i, n) {
      double x, y, r;
      cin >> x >> y >> r;
      xx.push_back(x - r);
      xx.push_back(x + r);
      yy.push_back(y - r);
      yy.push_back(y + r);
      X.push_back(x - r);
      X.push_back(x + r);
      Y.push_back(y - r);
      Y.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 < (int)xx.size(); i += 2) {
      xx[i] = find(all(X), xx[i]) - begin(X);
      yy[i] = find(all(Y), yy[i]) - begin(Y);
      xx[i + 1] = find(all(X), xx[i + 1]) - begin(X);
      yy[i + 1] = find(all(Y), yy[i + 1]) - begin(Y);
      color(xx[i], yy[i], xx[i + 1], yy[i + 1]);
    }
    /*//
    rep(i, Y.size()) {
      rep(j, X.size()) cout << (int)mas[j][i];
      cout << endl;
    }
    //*/
    double ans = 0;
    rep(i, X.size() - 1) rep(j, Y.size() - 1) {
      if (!mas[i][j])
        continue;
      ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
      // cout << X[i+1] << " " << X[i] <<  " " << Y[j+1] << " " << Y[j] << endl;
    }
    printf("%d %.2f\\n", t, ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) begin(v), end(v)
bool mas[444][444];

void color(int x, int y, int gx, int gy) {
  mas[x][y] = true;
  if (x + 1 < gx)
    color(x + 1, y, gx, gy);
  if (y + 1 < gy)
    color(x, y + 1, gx, gy);
}

int main() {
  int n;
  int t = 0;
  while (cin >> n, n) {
    t++;
    memset(mas, false, sizeof(mas));
    vector<double> xx, yy;
    vector<double> X, Y;
    rep(i, n) {
      double x, y, r;
      cin >> x >> y >> r;
      xx.push_back(x - r);
      xx.push_back(x + r);
      yy.push_back(y - r);
      yy.push_back(y + r);
      X.push_back(x - r);
      X.push_back(x + r);
      Y.push_back(y - r);
      Y.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 < (int)xx.size(); i += 2) {
      xx[i] = find(all(X), xx[i]) - begin(X);
      yy[i] = find(all(Y), yy[i]) - begin(Y);
      xx[i + 1] = find(all(X), xx[i + 1]) - begin(X);
      yy[i + 1] = find(all(Y), yy[i + 1]) - begin(Y);
      color(xx[i], yy[i], xx[i + 1], yy[i + 1]);
    }
    /*//
    rep(i, Y.size()) {
      rep(j, X.size()) cout << (int)mas[j][i];
      cout << endl;
    }
    //*/
    double ans = 0;
    rep(i, X.size() - 1) rep(j, Y.size() - 1) {
      if (!mas[i][j])
        continue;
      ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
      // cout << X[i+1] << " " << X[i] <<  " " << Y[j+1] << " " << Y[j] << endl;
    }
    printf("%d %.2f\\n", t, ans);
  }

  return 0;
}