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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ld pi =
    3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989;
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //??£??\\???????????¨???
  ll to, cost;
};
struct S { //???????????±?????\\???????????°
  int from, to, cost;
};
struct H {
  int x, y;
};
ll gcm(ll i, ll j) { //?????§??¬?´???°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld radian(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
}
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
struct K {
  int x, y, z;
};
bool operator<(K a, K b) { return a.x < b.x; }
struct A {
  int x, cost;
};
int n, m, b[5000][5000], ans;
K o[500000];
queue<A> a[5000];
signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> o[i].x >> o[i].y >> o[i].z;
    o[i].x--;
    o[i].y--;
  }
  sort(o, o + m);
  for (int i = 0; i < m; i++)
    a[o[i].y].push(A{o[i].x, o[i].z});
  for (int j = 0; j < n; j++) {
    if (a[j].empty())
      continue;
    for (int i = 0; i < n; i++) {
      while (!(a[j].empty()) && a[j].front().x + a[j].front().cost < i)
        a[j].pop();
      if (a[j].empty() || a[j].front().x > i)
        continue;
      b[i][j] = max(b[i][j], i - a[j].front().x + 1);
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 1; j < n; j++)
      b[i][j] = max(b[i][j], b[i][j - 1] - 1);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (b[i][j])
        ans++;
  cout << ans << endl;
  getchar();
  getchar();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
bool s[5000][5000];
int main() {
  int n, m;
  cin >> n >> m;
  int a, b, x;
  int sum = 0;
  for (int z = 0; z < m; z++) {
    cin >> a >> b >> x;
    a--;
    b--;
    for (int i = a; i <= a + x; i++) {
      for (int j = b; j <= b + (i - a); j++) {
        if (!s[i][j])
          sum++;
        s[i][j] = true;
      }
    }
  }
  cout << sum << endl;
}