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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int f(int y, int x) { return 2 * y + y * (y + 1) / 2 + x; }
int table[18000000];

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    REP(i, M) {
      int Y, X, L;
      scanf("%d %d %d", &Y, &X, &L);
      Y--;
      X--;
      table[f(Y, X)]++;
      table[f(Y, X + 1)]--;
      table[f(Y + L + 1, X)]--;
      table[f(Y + L + 1, X + L + 2)]++;
      table[f(Y + L + 2, X + 1)]++;
      table[f(Y + L + 2, X + L + 2)]--;
    }
    for (int y = 0; y < N + 2; y++) {
      for (int x = 1; x <= y + 2; x++) {
        table[f(y, x)] += table[f(y, x - 1)];
      }
    }
    for (int y = 1; y < N + 2; y++) {
      for (int x = 0; x <= y + 1; x++) {
        table[f(y, x)] += table[f(y - 1, x)];
      }
    }
    for (int y = 1; y < N + 2; y++) {
      for (int x = 1; x <= y + 2; x++) {
        table[f(y, x)] += table[f(y - 1, x - 1)];
      }
    }
    int ans = 0;
    for (int y = 0; y < N; y++) {
      for (int x = 0; x <= y; x++) {
        if (table[f(y, x)] > 0)
          ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    static short add[5000][5001];
    REP(i, M) {
      int A, B, X;
      scanf("%d %d %d", &A, &B, &X);
      A--;
      B--;
      REP(i, X + 1) {
        add[A + i][B]++;
        add[A + i][B + i + 1]--;
      }
    }
    int ans = 0;
    int curr = 0;
    REP(y, N) REP(x, y + 2) {
      curr += add[y][x];
      if (curr > 0)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}