alexjercan/bugnet huggingface dataset:
Problem p00496 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, T, S, s[2][3000];
int dp[3001][3001];

int dfs(int n, int t) {
  //  printf("%d %d\\n", n,t);
  if (n == N)
    return 0;
  if (dp[n][t] != -1)
    return dp[n][t];
  int res = 0;
  res = max(res, dfs(n + 1, t));
  if (!(t <= S && t + s[1][n] > S) && s[1][n] + t <= T)
    res = max(res, dfs(n + 1, t + s[1][n]) + s[0][n]);
  else if (t <= S && t + s[1][n] > S && S + s[1][n] <= T)
    res = max(res, dfs(n + 1, S + s[1][n]) + s[0][n]);

  return dp[n][t] = res;
}

int main() {
  scanf("%d%d%d", &N, &T, &S);
  rep(i, N) scanf("%d%d", &s[0][i], &s[1][i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\\n", dfs(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, T, S, s[2][3000];
int dp[3001][3001], v[3000];

int dfs(int n, int t) {
  //  printf("%d %d\\n", n,t);
  if (n == N)
    return 0;
  if (dp[n][t] != -1)
    return dp[n][t];
  int res = 0;
  res = max(res, dfs(n + 1, t));
  rep(i, N) {
    if (v[i])
      continue;
    v[i] = 1;
    if (!(t < S && t + s[1][n] > S) && s[1][n] + t <= T)
      res = max(res, dfs(n + 1, t + s[1][n]) + s[0][n]);
    else if (t < S && t + s[1][n] > S && S + s[1][n] + 1 <= T)
      res = max(res, dfs(n + 1, S + s[1][n] + 1) + s[0][n]);
    v[i] = 0;
  }
  return dp[n][t] = res;
}

int main() {
  scanf("%d%d%d", &N, &T, &S);
  rep(i, N) scanf("%d%d", &s[0][i], &s[1][i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\\n", dfs(0, 0));
  return 0;
}
Problem p00496 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <vector>

#include <queue>

using namespace std;

typedef long long ll;
int n, t, s;
int a[3001], b[3001];
int dp[3002][3002];
int main(void) {
  scanf("%d%d%d", &n, &t, &s);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &a[i], &b[i]);
  int res = 0;
  for (int i = 0; i <= t; i++) {
    int M = 0;
    for (int j = 0; j <= n; j++) {
      M = max(M, dp[i][j]);
      dp[i + 1][j] = max(M, dp[i + 1][j]);
      res = max(dp[i + 1][j], res);
      if (j == n)
        continue;
      if ((s <= i || i + b[j] <= s) && i + b[j] <= t) {
        dp[i + b[j]][j + 1] = max(dp[i + b[j]][j + 1], M + a[j]);
        res = max(res, dp[i + b[j]][j + 1]);
      }
    }
  }
  printf("%d\\n", res);

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <vector>

#include <queue>

using namespace std;

typedef long long ll;
int n, t, s;
ll a[3001], b[3001];
ll dp[3001][3001];
int main(void) {
  scanf("%d%d%d", &n, &t, &s);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &a[i], &b[i]);
  ll res = 0;
  for (int i = 0; i <= t; i++) {
    ll M = 0;
    for (int j = 0; j < n; j++) {
      M = max(M, dp[i][j]);
      dp[i + 1][j] = max(M, dp[i + 1][j]);
      if ((s <= i || i + b[j] < s) && i + b[j] <= t)
        dp[i + b[j]][j + 1] = max(dp[i + b[j]][j + 1], M + a[j]);
    }
  }
  for (int i = 0; i <= n; i++)
    res = max(dp[t][i], res);
  printf("%lld\\n", res);

  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
short grd[5010][5010];
short func(short a, short b) { return max(a, b); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    grd[a][b] = x + 1;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= n; j++) {
      if (grd[i][j]) {
        grd[i + 1][j] = func(grd[i + 1][j], grd[i][j] - 1);
        if (j != n) {
          grd[i + 1][j + 1] = func(grd[i + 1][j + 1], grd[i][j] - 1);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (grd[i][j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int imos[5010][5010];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    imos[a][b]++;
    imos[a][b + 1]--;
    imos[a + x + 1][b]--;
    imos[a + x + 2][b + 1]++;
    imos[a + x + 1][b + x + 2]++;
    imos[a + x + 2][b + x + 2]--;
  }
  // 左から右へ
  for (int i = 0; i < 5010; i++) {
    for (int j = 1; j < 5010; j++)
      imos[i][j] += imos[i][j - 1];
  }
  // 上から左下へ
  for (int i = 1; i < 5010; i++) {
    for (int j = 0; j < 5010; j++)
      imos[i][j] += imos[i - 1][j];
  }
  // 上から右下へ
  for (int i = 1; i < 5010; i++) {
    for (int j = 1; j < 5010; j++)
      imos[i][j] += imos[i - 1][j - 1];
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (imos[i][j] != 0)
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

short int t[5001][5001];
int main() {
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    t[a][b] = max((int)t[a][b], x + 1);
  }
  int ans = 0;
  REP(i, n) {
    REP(j, i + 1) {
      if (t[i][j] > 0)
        ans++;
      t[i + 1][j] = max((int)t[i + 1][j], t[i][j] - 1);
      t[i + 1][j + 1] = max((int)t[i + 1][j + 1], t[i][j] - 1);
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int t[5001][5001];
int main() {
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    t[a][b] = max(t[a][b], x + 1);
  }
  int ans = 0;
  REP(i, n) {
    REP(j, i + 1) {
      if (t[i][j] > 0)
        ans++;
      t[i + 1][j] = max(t[i + 1][j], t[i][j] - 1);
      t[i + 1][j + 1] = max(t[i + 1][j + 1], t[i][j] - 1);
    }
  }
  cout << ans << endl;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

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

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

  const int size = n + 3;
  vector<vector<int>> triangle;
  for (int i = 0; i < size; ++i)
    triangle.emplace_back(i + 2, 0);

  for (int i = 0; i < m; ++i) {
    int a, b, x;
    cin >> a >> b >> x;

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

  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < i; ++j) {
      triangle[i][j + 1] += triangle[i][j];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = i + 1; j < size; ++j) {
      triangle[j][i] += triangle[j - 1][i];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 1; i + j < size; ++j) {
      triangle[i + j][j] += triangle[i + j - 1][j - 1];
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      if (triangle[i][j] > 0)
        ++ans;
    }
  }

  cout << ans << endl;

  return EXIT_SUCCESS;
}

Error: MLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

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

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

  const int size = n + 3;
  vector<vector<int>> triangle(size, vector<int>(size, 0));

  for (int i = 0; i < m; ++i) {
    int a, b, x;
    cin >> a >> b >> x;

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

  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < i; ++j) {
      triangle[i][j + 1] += triangle[i][j];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = i + 1; j < size; ++j) {
      triangle[j][i] += triangle[j - 1][i];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 1; i + j < size; ++j) {
      triangle[i + j][j] += triangle[i + j - 1][j - 1];
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      if (triangle[i][j] > 0)
        ++ans;
    }
  }

  cout << ans << endl;

  return EXIT_SUCCESS;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
int bo[12560000];
int cal(int s) { return s * (s + 1) / 2 + 1; }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    bo[cal(a) + b] = x + 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      bo[cal(i) + j] = max(max(bo[cal(i) + j], bo[cal(i - 1) + j] - 1),
                           bo[cal(i - 1) + j - 1] - 1);
      if (bo[cal(i) + j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a, b) (abs((a) - (b)) < EPS)
int bo[5010][5010];
int n, m;
void debug() {
  for (int i = 0; i <= n + 3; i++) {
    for (int j = 0; j <= i + 3; j++) {
      cout << bo[i][j] << ' ';
    }
    cout << endl;
  }
  return;
}
int main() {
  ll ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    bo[a][b]++;
    bo[a][b + 1]--;
    bo[a + x + 1][b]--;
    bo[a + x + 1][b + x + 2]++;
    bo[a + x + 2][b + 1]++;
    bo[a + x + 2][b + x + 2]--;
  }
  for (int i = 1; i < n + 5; i++) {
    for (int j = 1; j <= i + 4; j++) {
      bo[i][j] += bo[i][j - 1];
    }
  }
  // debug();
  for (int i = 1; i < n + 5; i++) {
    for (int j = 1; j <= i + 4; j++) {
      bo[i][j] += bo[i - 1][j];
    }
  }
  // debug();
  for (int i = 1; i <= n + 5; i++) {
    for (int j = 1; j <= i + 4; j++) {
      bo[i][j] += bo[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (bo[i][j])
        ans++;
    }
  }
  // debug();
  cout << ans << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  int N, M;
  cin >> N >> M;

  vector<set<P>> kugi(N + 1);

  for (int i = 0; i <= N; ++i) {
    kugi[i].insert(P(0, i + 1)); // [0,i+1)
  }

  int ans = 0;
  for (int i = 0; i < M; ++i) {
    int A, B, X;
    cin >> A >> B >> X;

    for (int j = 0; j <= X; ++j) {
      int l = B - 1, r = B + j; // [B-1,B+j) is rubber band area
      set<P>::iterator itr = kugi[A + j - 1].begin();
      while (!kugi[A + j - 1].empty() && itr != kugi[A + j - 1].end()) {
        if (r <= itr->first)
          break;
        if (itr->second <= l) {
          ++itr;
          continue;
        }
        if (l <= itr->first && itr->second <= r) {
          ans += itr->second - itr->first;
          itr = kugi[A + j - 1].erase(itr);
          if (kugi[A + j - 1].empty()) {
            break;
          }
        } else if (itr->first >= l && itr->second > r) {
          int temp = itr->second;
          ans += r - itr->first;
          kugi[A + j - 1].erase(itr);
          kugi[A + j - 1].insert(P(r, temp));
          break;
        } else if (itr->first < l && itr->second <= r) {
          int temp = itr->first;
          ans += itr->second - l;
          itr = kugi[A + j - 1].erase(itr);
          kugi[A + j - 1].insert(P(temp, l));
        } else if (itr->first < l && itr->second > r) {
          int templ = itr->first, tempr = itr->second;
          ans += r - l;
          kugi[A + j - 1].erase(itr);
          kugi[A + j - 1].insert(P(templ, l));
          kugi[A + j - 1].insert(P(r, tempr));
          break;
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  int N, M;
  cin >> N >> M;

  vector<set<P>> kugi(N + 1);

  for (int i = 0; i < M; ++i) {
    int A, B, X;
    cin >> A >> B >> X;

    for (int j = 0; j <= X; ++j) {
      kugi[A + j].insert(P(B, B + j + 1)); // [B,B+j+1) = [B,B+j]
    }
  }

  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    int l = 0, r = 0;
    for (set<P>::iterator itr = kugi[i].begin(); itr != kugi[i].end(); ++itr) {
      if (itr->second <= r)
        continue;
      if (itr->first >= r) {
        l = itr->first;
        r = itr->second;
        ans += r - l;
      } else if (itr->second > r) {
        ans += itr->second - r;
        r = itr->second;
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

vector<int> nails[5010];

int main() {
  rep(i, 5010) rep(j, i + 10) nails[i].push_back(0);
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    --a;
    --b;
    ++nails[a][b];
    --nails[a][b + 1];
    --nails[a + x + 1][b];
    ++nails[a + x + 2][b + 1];
    ++nails[a + x + 1][b + x + 2];
    --nails[a + x + 2][b + x + 2];
  }
  rep(i, n + 3) rep(j, i + 3) { nails[i][j + 1] += nails[i][j]; }
  rep(i, n + 3) rep(j, i + 3) { nails[i + 1][j] += nails[i][j]; }
  rep(i, n + 3) rep(j, i + 3) { nails[i + 1][j + 1] += nails[i][j]; }
  int res = 0;
  rep(i, n + 3) rep(j, i + 3) {
    if (nails[i][j] > 0)
      ++res;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

vector<int> nails[5010];

int main() {
  rep(i, 5010) rep(j, i + 10) nails[i].push_back(0);
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    --a;
    --b;
    ++nails[a][b];
    --nails[a][b + 1];
    --nails[a + x + 1][b];
    ++nails[a + x + 2][b + 1];
    ++nails[a + x + 1][b + x + 2];
    --nails[a + x + 2][b + x + 2];
  }
  rep(i, n + 3) rep(j, n + 3) { nails[i][j + 1] += nails[i][j]; }
  rep(i, n + 3) rep(j, n + 3) { nails[i + 1][j] += nails[i][j]; }
  rep(i, n + 3) rep(j, n + 3) { nails[i + 1][j + 1] += nails[i][j]; }
  int res = 0;
  rep(i, n + 3) rep(j, n + 3) {
    if (nails[i][j] > 0)
      ++res;
  }
  cout << res << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#pragma warning(disable : 4996)
int n, m, a, b, c, imos[5009][5009], imos2[5009][5009], imos3[5009][5009];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    // cover (a, b), (a+1, b),..., (a+c, b)
    imos2[a][b]++;
    imos2[a + c + 1][b]--;
    // cover (a, b+1), (a+1, b+2),..., (a+c, b+c+1)
    imos3[a][b + 1]++;
    imos3[a + c + 1][b + c + 2]--;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      imos2[i + 1][j] += imos2[i][j];
    }
  }
  for (int i = -1; i < n; i++) {
    for (int j = 1; i + j <= n; j++) {
      if (i + j <= 0)
        continue;
      imos3[i + j][j] += imos3[i + j - 1][j - 1];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      imos[i][j] += imos2[i][j];
      imos[i][j] -= imos3[i][j];
    }
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += imos[i][j];
      if (sum > 0)
        ret++;
    }
  }
  printf("%d\\n", ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#pragma warning(disable : 4996)
int n, m, a, b, c, imos[5009][5009], imos2[5009][5009], imos3[5009][5009];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    // cover (a, b), (a+1, b),..., (a+c, b)
    imos2[a][b]++;
    imos2[a + c + 1][b]--;
    for (int j = 0; j <= c; j++)
      imos[a + j][b + j + 1]--;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      imos2[i + 1][j] = imos2[i + 1][j] + imos2[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      imos[i][j] += imos2[i][j];
    }
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += imos[i][j];
      if (sum > 0)
        ret++;
    }
  }
  printf("%d\\n", ret);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#pragma warning(disable : 4996)
int N, Q, x, y, z, inv2_vert[5009][5009], inv2_diag[5009][5009],
    imos[5009][5009];
int main() {
  scanf("%d%d", &N, &Q);
  while (Q--) {
    scanf("%d%d%d", &x, &y, &z), x--, y--, z++;
    inv2_vert[x][y]++;
    inv2_vert[x + z][y]--;
    inv2_diag[x][y + 1]--;
    inv2_diag[x + z][y + z + 1]++;
  }
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = i; j < N; j++) {
      sum += inv2_vert[j][i];
      imos[j][i] += sum;
    }
    sum = 0;
    for (int j = 0; j < N - i; j++) {
      sum += inv2_diag[i + j][j];
      imos[i + j][j] += sum;
    }
  }
  int ret = 0;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += imos[i][j];
      if (sum)
        ret++;
    }
  }
  printf("%d\\n", ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
int N, Q, x, y, z, imos[5009][5009];
int main() {
  scanf("%d%d", &N, &Q);
  while (Q--) {
    scanf("%d%d%d", &x, &y, &z), x--, y--, z++;
    for (int i = x; i < x + z; i++)
      imos[i][y]++, imos[i][y + i - x + 1]--;
  }
  int ret = 0;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += imos[i][j];
      if (sum)
        ret++;
    }
  }
  printf("%d\\n", ret);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

// AOJ 0574
#include <iostream>
#include <map>
#define REP(i, x) for (int i = 0; i < (x); i++)
#define TRI(a, b) nails[1 + (((a)-1) * ((a) + 4)) / 2 + 1 + (b)]
using namespace std;

typedef pair<int, int> P;

const int NMAX = 5008;
int nails[NMAX * (NMAX + 1) / 2];

int N, M;

void put(int a, int b, int x) {
  TRI(a, b)++;
  TRI(a, b + 1)--;
  TRI(a + x - 1, b)--;
  TRI(a + x, b + 1)++;
  TRI(a + x - 1, b + x)++;
  TRI(a + x, b + x)--;
}
/*
void show() {
  REP(a,N+1) {
    REP(b,a+2) {
      cout << TRI(a,b);
    }
    cout << endl;
  }
}*/

int count() {
  int sum = 0;
  for (int a = 1; a <= N; a++) {
    for (int b = 1; b <= a; b++) {
      if (TRI(a, b) > 0) {
        sum++;
      }
    }
  }

  return sum;
}

void accumulate() {
  // show();
  for (int a = 1; a <= N; a++)
    for (int b = 1; b <= a + 1; b++)
      TRI(a + 1, b) += TRI(a, b);

  // show();
  for (int a = 1; a <= N; a++)
    for (int b = 1; b <= a + 1; b++)
      TRI(a + 1, b + 1) += TRI(a, b);

  // show();
  for (int a = 1; a <= N; a++)
    for (int b = 1; b <= a + 1; b++)
      TRI(a, b + 1) += TRI(a, b);

  // show();
}

int main() {
  cin >> N >> M;
  REP(i, M) {
    int a, b, x;
    cin >> a >> b >> x;
    put(a, b, x + 2);
  }
  accumulate();
  cout << count() << endl;
}

Error: MLE

Fixed submission:

// AOJ 0574
#include <iostream>
#include <map>
#define REP(i, x) for (int i = 0; i < (x); i++)
using namespace std;

typedef pair<int, int> P;

const int NMAX = 5008;
int nails[NMAX][NMAX];

int N, M;

void put(int a, int b, int x) {
  P head = P(b, a - b + 1), left = P(b, a + x - b + 1),
    right = P(b + x, a - b + 1);
  nails[b][a - b + 1]++;
  nails[b + 1][a - b]--;
  nails[b][a + x - b + 1]--;
  nails[b + 1][a + x - b + 1]++;
  nails[b + x + 1][a - b]++;
  nails[b + x + 1][a - b + 1]--;
}

void accumulate() {

  for (int x = 0; x <= N + 2; x++)
    for (int y = 1; y <= N + 2; y++) {
      nails[x][y] += nails[x][y - 1];
    }

  for (int y = 0; y <= N + 2; y++)
    for (int x = 1; x <= N + 2; x++) {
      nails[x][y] += nails[x - 1][y];
    }

  for (int sum = 0; sum <= N + 3; sum++)
    for (int x = 1; sum - x >= 0; x++) {
      nails[x][sum - x] += nails[x - 1][sum - x + 1];
    }
}

int count() {
  int sum = 0;
  REP(y, N + 1) {
    REP(x, N + 1) {
      if (nails[x][y] > 0)
        sum++;
    }
  }
  return sum;
}

int main() {
  cin >> N >> M;
  REP(i, M) {
    int a, b, x;
    cin >> a >> b >> x;
    put(a, b, x + 1);
  }
  accumulate();
  cout << count() << endl;
}