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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  VVI board(n + 2);

  REP(i, 0, board.size()) { board[i].resize(i + 2, 0); }

  REP(i, 0, m) {
    int a, b, x;
    cin >> a >> b >> x;

    a--;
    b--;

    board[a][b]++;
    board[a][b + 1]--;

    board[a + x + 1][b]--;
    board[a + x + 1][b + x + 2]++;

    board[a + x + 2][b + 1]++;
    board[a + x + 2][b + x + 2]--;
  }

  REP(i, 0, board.size()) {
    REP(j, 1, board[i].size()) { board[i][j] += board[i][j - 1]; }
  }

  REP(i, 1, board.size()) {
    REP(j, 0, board[i - 1].size()) { board[i][j] += board[i - 1][j]; }
  }

  REP(i, 1, board.size()) {
    REP(j, 1, board[i].size()) { board[i][j] += board[i - 1][j - 1]; }
  }

  int res = 0;

  REP(i, 0, board.size() - 2) {
    REP(j, 0, board[i].size() - 1) { res += 1 <= board[i][j]; }
  }

  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  VVI board(n + 1), slanting(n + 1);

  REP(i, 0, board.size()) {
    board[i].resize(i + 2, 0);
    slanting[i].resize(i + 2, 0);
  }

  REP(i, 0, m) {
    int a, b, x;
    cin >> a >> b >> x;

    a--;
    b--;

    board[a + x + 1][b]--;
    board[a + x + 1][b + x + 1]++;

    slanting[a][b]++;
    slanting[a + x + 1][b + x + 1]--;
  }

  REP(i, 0, board.size()) {
    REP(j, 1, board[i].size()) { board[i][j] += board[i][j - 1]; }
  }

  REP(i, 1, slanting.size()) {
    REP(j, 1, slanting[i].size()) { slanting[i][j] += slanting[i - 1][j - 1]; }
  }

  REP(i, 0, board.size()) {
    REP(j, 0, board[i].size()) { board[i][j] += slanting[i][j]; }
  }

  REP(i, 1, board.size()) {
    REP(j, 0, board[i - 1].size()) { board[i][j] += board[i - 1][j]; }
  }

  int res = 0;

  REP(i, 0, board.size() - 1) {
    REP(j, 0, board[i].size() - 1) { res += 1 <= board[i][j]; }
  }

  cout << res << endl;

  return 0;
}