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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N, M;
int row[5010], dia[5010];
vector<int> v;

#define tri(y, x, t) ((y) << 15 | (x) << 2 | (t))

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N >> M;
  int A, B, X;
  rep(i, M) {
    cin >> A >> B >> X;
    X++;
    v.pb(tri(A, B, 0));
    v.pb(tri(A + X, B, 1));
    v.pb(tri(A, B + 1, 3));
    v.pb(tri(A + X, B + X + 1, 2));
  }
  v.pb(INF);
  sort(v.begin(), v.end());
  int ans = 0, sum, cur = 0;
  rep2(y, 1, N + 2) {
    sum = 0;
    rep2(x, 1, y + 2) {
      while ((v[cur] >> 2) == (y << 13 | x)) {
        switch (v[cur] & 3) {
        case 0:
          row[x]++;
          break;
        case 1:
          row[x]--;
          break;
        case 2:
          dia[y - x + 1]++;
          break;
        case 3:
          dia[y - x + 1]--;
          break;
        }
        cur++;
      }
      sum += row[x] + dia[y - x + 1];
      ans += sum > 0;
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int N, M;
int imos[5010][5010];
int row[5010], dia[5010];
vector<int> v;

#define tri(y, x, t) ((y) << 15 | (x) << 2 | (t))

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N >> M;
  int A, B, X;
  rep(i, M) {
    cin >> A >> B >> X;
    X++;
    v.pb(tri(A, B, 0));
    v.pb(tri(A + X, B, 1));
    v.pb(tri(A, B + 1, 3));
    v.pb(tri(A + X, B + X + 1, 2));
  }
  v.pb(INF);
  sort(v.begin(), v.end());
  int cur = 0;
  rep2(y, 1, N + 2) rep2(x, 1, y + 2) {
    while ((v[cur] >> 2) == (y << 13 | x)) {
      switch (v[cur] & 3) {
      case 0:
        row[x]++;
        break;
      case 1:
        row[x]--;
        break;
      case 2:
        dia[y - x + 1]++;
        break;
      case 3:
        dia[y - x + 1]--;
        break;
      }
      cur++;
    }
    imos[y][x] = row[x] + dia[y - x + 1];
  }
  int ans = 0, sum;
  rep2(y, 1, N + 1) {
    sum = 0;
    rep2(x, 1, y + 1) {
      sum += imos[y][x];
      ans += sum > 0;
    }
  }
  cout << ans << endl;
}