alexjercan/bugnet huggingface dataset:
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define calc(x, y) ((((x) * ((x) + 1)) / 2) + (y))
#define safe_calc(x, y) ((((x) + 2) * ((x) + 3) / 2) + (y) + 1)

int n;
int sum;
int nails[13000000];

void process() {
  int i;

  for (i = 1; i < n + 3; i++) {
    int j;
    for (j = 1; j <= i; j++) {
      nails[calc(i, j)] += nails[calc(i, j) - 1];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, i)] += nails[calc(i + j - 1, i)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, j)] += nails[calc(i + j - 1, j - 1)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 0; j <= i; j++) {
      if (nails[calc(i, j)] > 0)
        sum++;
    }
  }
}

int main() {
  int i;
  int m;

  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    a--;
    b--;
    x++;
    nails[safe_calc(a, b)]++;
    nails[safe_calc(a, b) + 1]--;
    nails[safe_calc(a + x, b)]--;
    nails[safe_calc(a + x, b + x) + 1]++;
    nails[safe_calc(a + x + 1, b + 1)]++;
    nails[safe_calc(a + x + 1, b + x + 1)]--;
  }

  process();

  printf("%d\\n", sum);
}

Error: 0

Fixed submission:

#include <stdio.h>

#define calc(x, y) ((((x) * ((x) + 1)) / 2) + (y))
#define safe_calc(x, y) ((((x) + 2) * ((x) + 3) / 2) + (y) + 1)

int n;
int sum;
int nails[12517506];

void process() {
  int i;

  for (i = 1; i < n + 3; i++) {
    int j;
    for (j = 1; j <= i; j++) {
      nails[calc(i, j)] += nails[calc(i, j) - 1];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, i)] += nails[calc(i + j - 1, i)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, j)] += nails[calc(i + j - 1, j - 1)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 0; j <= i; j++) {
      if (nails[calc(i, j)] > 0)
        sum++;
    }
  }
}

int main() {
  int i;
  int m;

  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    a--;
    b--;
    x++;
    nails[safe_calc(a, b)]++;
    nails[safe_calc(a, b) + 1]--;
    nails[safe_calc(a + x, b)]--;
    nails[safe_calc(a + x, b + x) + 1]++;
    nails[safe_calc(a + x + 1, b + 1)]++;
    nails[safe_calc(a + x + 1, b + x + 1)]--;
  }

  process();

  printf("%d\\n", sum);
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const int MAXN = 5200;
vector<int> table[MAXN];

int main(void) {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < MAXN; i++) {
    table[i].resize(i + 20);
  }
  while (M--) {
    int a, b, x;
    cin >> a >> b >> x;
    table[a][b]++;
    table[a][b + 1]--;
    table[a + x + 1][b]--;
    table[a + x + 2][b + 1]++;
    table[a + x + 1][b + x + 2]++;
    table[a + x + 2][b + x + 2]--;
  }
  // 左から右
  for (int i = 0; i < MAXN - 100; i++) {
    for (int j = 1; j < i + 10; j++) {
      table[i][j] += table[i][j - 1];
    }
  }
  // 右上から左下
  for (int i = 1; i < MAXN - 100; i++) {
    for (int j = 0; j < i + 10; j++) {
      table[i][j] += table[i - 1][j];
    }
  }
  // 左上から右下
  for (int i = 1; i < MAXN - 100; i++) {
    for (int j = 1; j < i + 10; j++) {
      table[i][j] += table[i - 1][j - 1];
    }
  }
  int ans = 0;
  for (int i = 0; i < MAXN; i++) {
    for (int j = 0; j < i + 10; j++) {
      if (table[i][j] > 0)
        ans++;
    }
  }
  cout << ans << endl;
  //    for (int i = 0; i < 10; i++) {
  //        for (int j = 0; j < 10; j++) {
  //            cout << table[i][j] << "\\t";
  //        }
  //        cout << endl;
  //    }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const int MAXN = 5200;
int table[MAXN][MAXN];

int main(void) {
  int N, M;
  cin >> N >> M;
  while (M--) {
    int a, b, x;
    cin >> a >> b >> x;
    table[a][b]++;
    table[a][b + 1]--;
    table[a + x + 1][b]--;
    table[a + x + 2][b + 1]++;
    table[a + x + 1][b + x + 2]++;
    table[a + x + 2][b + x + 2]--;
  }
  // 左から右
  for (int i = 0; i < MAXN - 100; i++) {
    for (int j = 1; j < i + 10; j++) {
      table[i][j] += table[i][j - 1];
    }
  }
  // 右上から左下
  for (int i = 1; i < MAXN - 100; i++) {
    for (int j = 0; j < i + 10; j++) {
      table[i][j] += table[i - 1][j];
    }
  }
  // 左上から右下
  for (int i = 1; i < MAXN - 100; i++) {
    for (int j = 1; j < i + 10; j++) {
      table[i][j] += table[i - 1][j - 1];
    }
  }
  int ans = 0;
  for (int i = 0; i < MAXN; i++) {
    for (int j = 0; j < MAXN; j++) {
      if (table[i][j] > 0)
        ans++;
    }
  }
  cout << ans << endl;
  //    for (int i = 0; i < 10; i++) {
  //        for (int j = 0; j < 10; j++) {
  //            cout << table[i][j] << "\\t";
  //        }
  //        cout << endl;
  //    }
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int zari(int i, int j) { return (i * (i + 1) / 2 + j); }
int n, m;
int maps[5000 * 2505] = {0};
int main(void) {

  // 釘(i,j)をmaps[i*(i+1)/2+j]とする。
  scanf("%d%d", &n, &m);
  int i, j;
  int a, b, c;
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a += 1; // b--;
    maps[zari(a, b)] += 1;
    maps[zari(a, b + 1)] += -1;
    maps[zari(a + c + 1, b)] += -1;
    maps[zari(a + c + 2, b + 1)] += 1;
    maps[zari(a + c + 1, b + c + 2)] += 1;
    maps[zari(a + c + 2, b + c + 2)] += -1;
  }

  for (i = 1; i <= n + 3; i++) {
    for (j = 1; j <= i; j++) {
      maps[zari(i, j)] += maps[zari(i, j - 1)];
    }
  }

  for (j = 0; j <= n + 3; j++) {

    for (i = j; i <= n + 3; i++) {
      maps[zari(i, j)] += maps[zari(i - 1, j)];
    }
  }

  int ans = 0;
  for (i = 1; i <= n + 3; i++) {
    // for(j=0;j<i*2;i++)printf("\\x0020");
    for (j = 1; j <= i; j++) {
      maps[zari(i, j)] += maps[zari(i - 1, j - 1)];
      if (maps[zari(i, j)] != 0)
        ans++;
      // printf("%d\\x0020",maps[zari(i,j)]);
    }
    // printf("\\n");
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

int zari(int i, int j) { return (i * (i + 1) / 2 + j); }
int n, m;
int maps[5000 * 2505] = {0};
int main(void) {

  // 釘(i,j)をmaps[i*(i+1)/2+j]とする。
  scanf("%d%d", &n, &m);
  int i, j;
  int a, b, c;
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a += 1; // b--;
    maps[zari(a, b)] += 1;
    maps[zari(a, b + 1)] += -1;
    maps[zari(a + c + 1, b)] += -1;
    maps[zari(a + c + 2, b + 1)] += 1;
    maps[zari(a + c + 1, b + c + 2)] += 1;
    maps[zari(a + c + 2, b + c + 2)] += -1;
  }

  for (i = 1; i <= n + 5; i++) {
    for (j = 1; j <= i; j++) {
      maps[zari(i, j)] += maps[zari(i, j - 1)];
    }
  }

  for (j = 0; j <= n + 5; j++) {

    for (i = j; i <= n + 5; i++) {
      maps[zari(i, j)] += maps[zari(i - 1, j)];
    }
  }

  int ans = 0;
  for (i = 1; i <= n + 5; i++) {
    // for(j=0;j<i*2;i++)printf("\\x0020");
    for (j = 1; j <= i; j++) {
      maps[zari(i, j)] += maps[zari(i - 1, j - 1)];
      if (maps[zari(i, j)] != 0)
        ans++;
      // printf("%d\\x0020",maps[zari(i,j)]);
    }
    // printf("\\n");
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int a, b, x, n, m;
  static int field[5001][5001] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    field[a - 1][b - 1] = x + 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      field[i + 1][j] = max(field[i + 1][j], field[i][j] - 1);
      field[i + 1][j + 1] = max(field[i + 1][j + 1], field[i][j] - 1);
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (field[i][j])
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b, x, n, m, field[5001][5001] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    field[a - 1][b - 1] = x + 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      field[i + 1][j] = max(field[i + 1][j], field[i][j] - 1);
      field[i + 1][j + 1] = max(field[i + 1][j + 1], field[i][j] - 1);
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (field[i][j])
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

short dp[5010][5010];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> y >> x >> c;
    dp[y - 1][x - 1] = c + 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i + 1][j] = max<int>(dp[i + 1][j], dp[i][j] - 1);
      dp[i + 1][j + 1] = max<int>(dp[i + 1][j + 1], dp[i][j] - 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ans += !!dp[i][j];
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

short dp[5000][5000];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> y >> x >> c;
    dp[y - 1][x - 1] = c + 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i + 1][j] = max<int>(dp[i + 1][j], dp[i][j] - 1);
      dp[i + 1][j + 1] = max<int>(dp[i + 1][j + 1], dp[i][j] - 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ans += !!dp[i][j];
    }
  }
  cout << ans << endl;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
typedef pair<p, p> p2;
#define pu push
#define pb push_back
#define mp make_pair
short field[5005][5005] = {};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    field[a][b] = (short)c + 1;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= n; j++) {
      if (field[i][j] > 0) {
        field[i + 1][j] = max((int)field[i + 1][j], (int)field[i][j] - 1);
        if (j != n) {
          field[i + 1][j + 1] =
              max((int)field[i + 1][j + 1], (int)field[i][j] - 1);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (field[i][j] > 0) {
        ans++;
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
typedef pair<p, p> p2;
#define pu push
#define pb push_back
#define mp make_pair
int field[5005][5005] = {};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    field[a][b] = c + 1;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= n; j++) {
      if (field[i][j] > 0) {
        field[i + 1][j] = max(field[i + 1][j], field[i][j] - 1);
        if (j != n) {
          field[i + 1][j + 1] = max(field[i + 1][j + 1], field[i][j] - 1);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (field[i][j] > 0) {
        ans++;
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int dp[5000][5000];
int memo[5000];

int main() {
  int n = getInt();
  int m = getInt();
  REP(i, n) memo[i] = i * (i + 1) / 2;

  REP(i, m) {
    int a = getInt() - 1;
    int b = getInt() - 1;
    int x = getInt();

    REP(i, x + 1) { dp[b][a + i] = max(dp[b][a + i], i + 1); }
  }

  int ans = 0;
  REP(i, n) {
    int mx = 0;
    REP(j, i + 1) {
      mx = max(mx - 1, dp[j][i]);
      if (mx > 0)
        ans++;
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int dp[5000 * 5001 / 2];
int memo[5000];

inline int pos(int y, int x) { return memo[y] + x; }

int main() {
  int n = getInt();
  int m = getInt();
  REP(i, n) memo[i] = i * (i + 1) / 2;

  REP(i, m) {
    int a = getInt() - 1;
    int b = getInt() - 1;
    int x = getInt();

    int p = pos(a, b);
    REP(i, x + 1) {
      dp[p] = max(dp[p], i + 1);
      p += a + i + 1;
    }
  }

  int ans = 0;
  REP(i, n) {
    int mx = 0;
    REP(j, i + 1) {
      mx = max(mx - 1, dp[pos(i, j)]);
      if (mx > 0)
        ans++;
    }
  }

  printf("%d\\n", ans);

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

#include <iostream>

#define L 5005

using namespace std;

int N, M;
int m1[L * (L + 1) / 2] = {0}, m2[L * (L + 1) / 2] = {0};
int P(int y, int x) {
  ++y;
  return y * (y + 1) / 2 + x;
}
void Print() {
  for (int y = 0; y < N + 1; ++y) {
    for (int x = 0; x <= y + 1; ++x) {
      cout << m1[P(y, x)] << " ";
    }
    cout << endl;
  }
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int y, x, l;
    cin >> y >> x >> l;
    --x, --y, ++l;
    ++m1[P(y, x)], --m1[P(y + l, x)], --m1[P(y + 1, x + 1)],
        ++m1[P(y + l + 1, x + 1)];
    --m1[P(y, x + 1)], ++m1[P(y + l, x + l + 1)], ++m1[P(y + 1, x + 1)],
        --m1[P(y + l + 1, x + l + 1)];
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y + 1; ++x) {
      m1[P(y + 1, x + 1)] += m1[P(y, x)];
    }
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y + 1; ++x) {
      m1[P(y + 1, x)] += m1[P(y, x)];
    }
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y + 1; ++x) {
      m1[P(y, x + 1)] += m1[P(y, x)];
    }
  }
  int ans = 0;
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y; ++x) {
      if (m1[P(y, x)] > 0)
        ++ans;
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <iostream>

#define L 5005

using namespace std;

int m1[L * (L + 1) / 2] = {0}, m2[L * (L + 1) / 2] = {0};
int P(int y, int x) {
  ++y;
  return y * (y + 1) / 2 + x;
}
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int y, x, l;
    cin >> y >> x >> l;
    --x, --y, ++l;
    ++m1[P(y, x)], --m1[P(y + l, x)];
    --m2[P(y, x + 1)], ++m2[P(y + l, x + l + 1)];
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y; ++x) {
      m1[P(y + 1, x)] += m1[P(y, x)];
      m2[P(y + 1, x + 1)] += m2[P(y, x)];
    }
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y; ++x) {
      m1[P(y, x)] += m2[P(y, x)];
    }
  }
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y; ++x) {
      m1[P(y, x + 1)] += m1[P(y, x)];
    }
  }
  int ans = 0;
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x <= y; ++x) {
      if (m1[P(y, x)] > 0)
        ++ans;
    }
  }
  cout << ans << endl;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
short t[5000][5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, k;
  int i, j;
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      t[i][j] = -1;
  for (i = 0; i < k; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    t[a - 1][b - 1] = max(t[a - 1][b - 1], c);
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n; j++) {
      if (t[i][j] > 0) {
        t[i + 1][j] = max(t[i + 1][j], t[i][j] - 1);
        if (j < n - 1)
          t[i + 1][j + 1] = max(t[i + 1][j + 1], t[i][j] - 1);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (t[i][j] >= 0)
        ans++;
  printf("%d\\n", ans);
  return 0;
}


Error: MLE

Fixed submission:

#include <stdio.h>
int t[5000][5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, k;
  int i, j;
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      t[i][j] = -1;
  for (i = 0; i < k; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    t[a - 1][b - 1] = max(t[a - 1][b - 1], c);
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n; j++) {
      if (t[i][j] > 0) {
        t[i + 1][j] = max(t[i + 1][j], t[i][j] - 1);
        if (j < n - 1)
          t[i + 1][j + 1] = max(t[i + 1][j + 1], t[i][j] - 1);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (t[i][j] >= 0)
        ans++;
  printf("%d\\n", ans);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

short dp[5010][5010] = {};

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x, y, c;
    cin >> x >> y >> c;
    x--, y--, c++;
    dp[x][y] = c;
  }

  loop(i, 1, n) rep(j, i + 1) {
    int t = -100;
    if (j - 1 >= 0)
      t = max(t, dp[i - 1][j - 1] - 1);
    if (j < i + 1)
      t = max(t, dp[i - 1][j] - 1);
    dp[i][j] = max<short>(dp[i][j], t);
  }

  int ans = 0;
  rep(i, n) {
    rep(j, i + 1) {
      // cout << dp[i][j] << " ";
      if (dp[i][j])
        ans++;
    }
    // cout << endl;
  }

  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef pair<int, int> P;
int dp[2][5010] = {};
map<P, int> ps;

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x, y, c;
    cin >> x >> y >> c;
    x--, y--, c++;
    ps[P(x, y)] = c;
  }

  int ans = 0;

  int *cur = dp[0];
  int *prev = dp[1];
  loop(i, 0, n) {
    memset(cur, 0, sizeof(cur));
    rep(j, i + 1) {
      int t = ps[P(i, j)];
      if (j - 1 >= 0)
        t = max(t, prev[j - 1] - 1);
      if (j < i + 1)
        t = max(t, prev[j] - 1);
      ans += t != 0;
      cur[j] = t;
    }
    swap(cur, prev);
  }

  cout << ans << endl;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Triangle {
  int a, b, x;
  bool operator<(const Triangle &other) const { return a < other.a; }
};

bool update(int &a, int v) {
  if (a > v)
    return false;
  a = v;
  return true;
}

int dp[2][5000];
int main() {
  int N, M;
  cin >> N >> M;
  vector<Triangle> triangles(M);
  for (int i = 0; i < M; ++i) {
    Triangle &t = triangles[i];
    cin >> t.a >> t.b >> t.x;
  }
  sort(triangles.begin(), triangles.end());

  int ans = 0;
  int ptr = 0;
  vector<Triangle>::iterator it = triangles.begin();
  dp[ptr][0] = -1;
  for (int i = 0; i < N; ++i) {
    int np = 1 - ptr;
    while (it != triangles.end() && it->a - 1 == i) {
      update(dp[ptr][it->b - 1], it->x);
      ++it;
    }
    for (int j = 0; j < N; ++j) {
      dp[np][j] = -1;
    }
    for (int j = 0; j <= i; ++j) {
      if (dp[ptr][j] != -1) {
        ++ans;
        update(dp[np][j], dp[ptr][j] - 1);
        if (j + 1 < N)
          update(dp[np][j + 1], dp[ptr][j] - 1);
      }
    }
    ptr = np;
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int dp[5000][5000];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < M; ++i) {
    int A, B, X;
    cin >> A >> B >> X;
    dp[A - 1][B - 1] = X;
  }
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= i; ++j) {
      if (dp[i][j] != -1) {
        ++ans;
        if (i + 1 < N) {
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - 1);
          if (j + 1 < N)
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] - 1);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

// #define DEBUG
#define int long long
#define rep(i, n) for (int i = 1; i <= n; i++)
int n, m;
int f[5010][5010];

void debug() {
#ifdef DEBUG
  rep(i, n + 2) {
    rep(j, n + 2) cout << setw(2) << right << f[i - 1][j - 1] << " ";
    cout << endl;
  }
  cout << endl;
#endif
}

signed main() {
  cin >> n >> m;
  rep(_, m) {
    int a, b, x;
    cin >> a >> b >> x;
    f[a - 1][b - 1]--;
    f[a - 1][b]++;
    f[a + x][b - 1]++;
    f[a + x + 1][b]--;
    f[a + x][b + x + 1]--;
    f[a + x + 1][b + x + 1]++;
  }
  int ans = 0;
  debug();
  for (int k = n + 1; k >= 0; k--) {
    for (int i = n, j = k; i >= 0 && j >= 0; i--, j--) {
      f[i][j] += f[i + 1][j + 1];
    }
  }
  debug();
  rep(j, n + 2) {
    rep(i, n + 1) { f[i][j - 1] += f[i - 1][j - 1]; }
  }
  debug();
  rep(i, n) {
    rep(j, i) {
      f[i][j] += f[i][j - 1];
      if (f[i][j])
        ans++;
    }
  }
  debug();
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 1; i <= n; i++)
int n, m;
int f[5002][5002];

int main() {
  cin >> n >> m;
  rep(_, m) {
    int a, b, x;
    cin >> a >> b >> x;
    rep(i, x + 1) {
      f[a + i - 1][b]++;
      f[a + i - 1][b + i]--;
    }
  }
  int ans = 0;
  rep(i, n) {
    int s = 0;
    rep(j, i) {
      s += f[i][j];
      if (s)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}