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

#include "bits/stdc++.h"

using namespace std;
#define rep(i, n) for (int i = 0; i < signed(n); i++)
#define rrep(i, n) for (int i = signed(n) - 1; i >= 0; i--)
#define all(co) co.begin(), co.end()
// #define int long long
// using LL = long long;
// using P = pair < int, int > ;
// using PI = pair < P, int > ;
typedef pair<int, int> P;
typedef long long LL;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
const int INF = 1 << 29;
const LL LINF = 1ll << 62ll;

struct edge {
  int from, to, cost;
};

int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};

template <typename T = int>
vector<vector<T>> vvector(int h, int w, T def = T()) {
  return move(vector<vector<T>>(h, vector<T>(w, def)));
}
class range {
private:
  struct I {
    int x;
    int operator*() { return x; }
    bool operator!=(I &lhs) { return x < lhs.x; }
    void operator++() { ++x; }
  };
  I i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  I &begin() { return i; }
  I &end() { return n; }
};

signed main() {
  int n;
  while (cin >> n && n) {
    int m;
    cin >> m;
    vector<vector<int>> triangle(n);
    rep(i, n) triangle[i].resize(i + 1);
    rep(i, m) {
      int a, b, x;
      cin >> a >> b >> x;
      a--;
      b--;
      triangle[a][b] = x + 1;
    }
    rep(i, n - 1) rep(j, i + 1) {
      triangle[i + 1][j] = max(triangle[i + 1][j], triangle[i][j] - 1);
      triangle[i + 1][j + 1] = max(triangle[i + 1][j + 1], triangle[i][j] - 1);
    }
    int ans = 0;
    rep(i, n) rep(j, i + 1) ans += !!triangle[i][j];
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
#define rep(i, n) for (int i = 0; i < signed(n); i++)
#define rrep(i, n) for (int i = signed(n) - 1; i >= 0; i--)
#define all(co) co.begin(), co.end()
// #define int long long
// using LL = long long;
// using P = pair < int, int > ;
// using PI = pair < P, int > ;
typedef pair<int, int> P;
typedef long long LL;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
const int INF = 1 << 29;
const LL LINF = 1ll << 62ll;

struct edge {
  int from, to, cost;
};

int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};

template <typename T = int>
vector<vector<T>> vvector(int h, int w, T def = T()) {
  return move(vector<vector<T>>(h, vector<T>(w, def)));
}
class range {
private:
  struct I {
    int x;
    int operator*() { return x; }
    bool operator!=(I &lhs) { return x < lhs.x; }
    void operator++() { ++x; }
  };
  I i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  I &begin() { return i; }
  I &end() { return n; }
};

signed main() {
  int n;
  while (cin >> n && n) {
    int m;
    cin >> m;
    auto triangle = vvector(n, n);
    rep(i, m) {
      int a, b, x;
      cin >> a >> b >> x;
      a--;
      b--;
      triangle[a][b] = x + 1;
    }
    rep(i, n - 1) rep(j, n - 1) {
      triangle[i + 1][j] = max(triangle[i + 1][j], triangle[i][j] - 1);
      triangle[i + 1][j + 1] = max(triangle[i + 1][j + 1], triangle[i][j] - 1);
    }
    int ans = 0;
    rep(i, n) rep(j, n) ans += !!triangle[i][j];
    cout << ans << endl;
  }

  return 0;
}