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

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

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

using namespace std;

int main(void) {
  int N, M;
  int A, B, X;

  cin >> N >> M;

  // vector<vector<int> > pos(N + 3,vector<int>(N + 3));

  vector<vector<int>> pos(N + 5);
  rep(i, N + 5) pos[i].assign(i + 5, 0);

  rep(i, M) {
    cin >> A >> B >> X;

    pos[A][B]++;
    pos[A][B + 1]--;
    pos[A + X + 2][B + 1]++;
    pos[A + X + 1][B]--;
    pos[A + X + 1][B + X + 2]++;
    pos[A + X + 2][B + X + 2]--;
  }

  rec(y, 1, N + 1) rep(x, y + 2) if (x > 1) pos[y][x] += pos[y][x - 1];

  rec(x, 1, N + 2) rec(y, x, N + 2) if (y != x) pos[y][x] += pos[y - 1][x];

  rep(i, N + 2) for (int j = 0; i + j < N + 2; ++j) if (j > 1 && j + i > 1)
      pos[i + j][j] += pos[i + j - 1][j - 1];

  int ans = 0;

  rep(i, N + 1) rep(j, i + 1) if (pos[i][j] > 0) ans++;

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

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

using namespace std;

int main(void) {
  int N, M;
  int A, B, X;

  cin >> N >> M;

  vector<vector<int>> pos(N + 3, vector<int>(N + 3));

  rep(i, M) {
    cin >> A >> B >> X;

    pos[A][B]++;
    pos[A][B + 1]--;
    pos[A + X + 2][B + 1]++;
    pos[A + X + 1][B]--;
    pos[A + X + 1][B + X + 2]++;
    pos[A + X + 2][B + X + 2]--;
  }

  rec(y, 1, N + 1) rep(x, y + 2) if (x > 1) pos[y][x] += pos[y][x - 1];

  rec(x, 1, N + 2) rec(y, x, N + 2) if (y != x) pos[y][x] += pos[y - 1][x];

  rep(i, N + 2) for (int j = 0; i + j < N + 2; ++j) if (j > 1 && j + i > 1)
      pos[i + j][j] += pos[i + j - 1][j - 1];

  int ans = 0;

  rep(i, N + 1) rep(j, N + 1) if (pos[i][j] > 0) ans++;

  cout << ans << endl;

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

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

#define N 5100

vector<int> x[N];

int n, m, a, b, c, cnt;
int V[N];

int main() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 5; j++) {
      x[i].push_back(0);
      V[i] = j;
    }
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    c++;
    x[a][b]++;
    x[a + c][b]--;
    x[a + c + 1][b + 1]++;
    x[a][b + 1]--;
    x[a + c][b + c + 1]++;
    x[a + c + 1][b + c + 1]--;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (j < V[i]) {
        x[i][j] += x[i][j - 1];
      }
    }
  }
  for (int j = 0; j < N; j++) {
    for (int i = 1; i < N; i++) {
      if (j < V[i]) {
        x[i][j] += x[i - 1][j];
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n - i; j++) {
      if (j + 1 < V[i + j + 1]) {
        x[i + j + 1][j + 1] += x[i + j][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j < V[i]) {
        if (x[i][j] >= 1) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define N 2020

vector<int> x[N];

int n, m, a, b, c, cnt;
int V[N];

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

#include <cstdio>
#define S 5010
using namespace std;
int l[S][S] = {0};

int main() {
  int N, M, a, b, x, i, j;
  scanf("%d %d", &N, &M);
  for (i = 0; i < M; i++) {
    scanf("%d %d %d", &a, &b, &x);
    l[a][b]++;
    l[a][b + 1]--;
    l[a + x + 1][b]--;
    l[a + x + 2][b + 1]++;
    l[a + x + 2][b + x + 2]--;
    l[a + x + 1][b + x + 2]++;
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      l[i][j] += l[i][j - 1];
    }
  }
  for (j = 1; j < N + 4; j++) {
    for (i = 1; i < N + 4; i++) {
      l[i][j] += l[i - 1][j];
    }
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      if (i + j - 1 < S) {
        l[i + j - 1][j] += l[i + j - 2][j - 1];
      }
    }
  }
  int cnt = 0;
  for (i = 0; i < N + 4; i++) {
    for (j = 0; j < N + 4; j++) {
      if (l[i][j] > 0) {
        cnt++;
      }
    }
  }
  printf("%d\\n", cnt);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#define S 5002
using namespace std;
int l[S][S] = {0};

int main() {
  int N, M, a, b, x, i, j;
  scanf("%d %d", &N, &M);
  for (i = 0; i < M; i++) {
    scanf("%d %d %d", &a, &b, &x);
    l[a][b]++;
    l[a][b + 1]--;
    l[a + x + 1][b]--;
    l[a + x + 2][b + 1]++;
    l[a + x + 2][b + x + 2]--;
    l[a + x + 1][b + x + 2]++;
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      l[i][j] += l[i][j - 1];
    }
  }
  for (j = 1; j < N + 4; j++) {
    for (i = 1; i < N + 4; i++) {
      l[i][j] += l[i - 1][j];
    }
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      if (i + j - 1 < S) {
        l[i + j - 1][j] += l[i + j - 2][j - 1];
      }
    }
  }
  int cnt = 0;
  for (i = 0; i < N + 4; i++) {
    for (j = 0; j < N + 4; j++) {
      if (l[i][j] > 0) {
        cnt++;
      }
    }
  }
  printf("%d\\n", cnt);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<vector<pair<int, int>>> pluss(N, vector<pair<int, int>>(N + 1));
  vector<vector<int>> aas(N, vector<int>(N + 1));
  for (int i = 0; i < M; ++i) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    pluss[a + x][b].first++;
    pluss[a + x][b + x + 1].second++;
    if (a) {

      aas[a - 1][b]++;
    }
  }
  for (int h = N - 1; h >= 0; --h) {
    for (int w = 0; w <= h + 1; ++w) {
      if (h != N - 1) {

        pluss[h][w].first += pluss[h + 1][w].first;
        pluss[h][w].second += pluss[h + 1][w + 1].second;
      }
      pluss[h][w].first -= aas[h][w];
      pluss[h][w].second -= aas[h][w];
    }
  }
  int ans = 0;
  for (int h = N - 1; h >= 0; --h) {
    int sum = 0;
    for (int w = 0; w <= h; ++w) {
      sum += pluss[h][w].first - pluss[h][w].second;
      if (sum > 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<vector<pair<int, int>>> pluss(N, vector<pair<int, int>>(N + 1));
  vector<vector<int>> aas(N, vector<int>(N + 1));
  for (int i = 0; i < M; ++i) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    pluss[a + x][b].first++;
    pluss[a + x][b + x + 1].second++;
    aas[a - 1][b]++;
  }
  for (int h = N - 1; h >= 0; --h) {
    for (int w = 0; w <= h + 1; ++w) {
      if (h != N - 1) {

        pluss[h][w].first += pluss[h + 1][w].first;
        pluss[h][w].second += pluss[h + 1][w + 1].second;
      }
      pluss[h][w].first -= aas[h][w];
      pluss[h][w].second -= aas[h][w];
    }
  }
  int ans = 0;
  for (int h = N - 1; h >= 0; --h) {
    int sum = 0;
    for (int w = 0; w <= h; ++w) {
      sum += pluss[h][w].first - pluss[h][w].second;
      if (sum > 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> f[5005];
int main(void) {
  cin >> n >> m;
  for (int i = 0; i <= n + 5; i++) {
    f[i].resize(i + 5, 0);
  }

  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    f[a][b]++;
    f[a][b + 1]--;
    f[a + x + 1][b]--;
    f[a + x + 2][b + 1]++;
    f[a + x + 1][b + x + 2]++;
    f[a + x + 2][b + x + 2]--;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      f[j][i] += f[j - 1][i];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      f[i][j] += f[i][j - 1];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      f[i][j] += f[i - 1][j - 1];
    }
  }

  int res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (f[i][j] > 0)
        res++;
    }
  }
  cout << res << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int n, m;
int f[5005][5005];
int main(void) {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    f[a][b]++;
    f[a][b + 1]--;
    f[a + x + 1][b]--;
    f[a + x + 2][b + 1]++;
    f[a + x + 1][b + x + 2]++;
    f[a + x + 2][b + x + 2]--;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[j][i] += f[j - 1][i];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] += f[i][j - 1];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] += f[i - 1][j - 1];
    }
  }

  int res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (f[i][j] > 0)
        res++;
    }
  }
  cout << res << endl;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int N, M;
vector<int> G[5100];

int main() {
  for (int i = 0; i < 5100; i++)
    G[i].resize(i + 10, 0);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b, x;
    scanf("%d%d%d", &a, &b, &x);
    G[a][b]++;
    G[a][b + 1]--;
    G[a + x + 1][b]--;
    G[a + x + 1][b + x + 2]++;
    G[a + x + 2][b + 1]++;
    G[a + x + 2][b + x + 2]--;
  }

  for (int i = 0; i <= N + 5; i++)
    for (int j = 0; j <= i; j++)
      G[i][j + 1] += G[i][j];
  for (int i = 0; i <= N + 5; i++)
    for (int j = i; j <= N + 5; j++)
      G[j + 1][i] += G[j][i];
  for (int i = 0; i <= N + 5; i++)
    for (int j = 0; j <= i; j++)
      G[i + 1][j + 1] += G[i][j];
  int res = 0;
  for (int i = 0; i <= N + 5; i++)
    for (int j = 0; j <= i; j++)
      if (G[i][j])
        res++;
  printf("%d\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int N, M;
vector<int> G[5005];

int main() {
  for (int i = 0; i < 5005; i++)
    G[i].resize(i + 1, 0);
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b, x;
    scanf("%d%d%d", &a, &b, &x);
    G[a][b] = 1;
    G[a][b + 1] = -1;
    G[a + x + 1][b] = -1;
    G[a + x + 1][b + x + 2] = 1;
    G[a + x + 2][b + 1] = 1;
    G[a + x + 2][b + x + 2] = -1;
  }

  for (int i = 0; i <= N + 3; i++)
    for (int j = 0; j <= i; j++)
      G[i][j + 1] += G[i][j];
  for (int i = 0; i <= N + 3; i++)
    for (int j = i; j <= N + 3; j++)
      G[j + 1][i] += G[j][i];
  for (int i = 0; i <= N + 3; i++)
    for (int j = 0; j <= i; j++)
      G[i + 1][j + 1] += G[i][j];
  int res = 0;
  for (int i = 0; i <= N + 1; i++)
    for (int j = 0; j <= i; j++)
      if (G[i][j])
        res++;
  printf("%d\\n", res);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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)

int N, M;
vector<vector<int>> g(5002);

int main() {
  rep(i, 5002) {
    g[i].resize(i + 2);
    fill(g[i].begin(), g[i].end(), 0);
  }
  //  memset(g, 0, sizeof(g));
  scanf("%d%d", &N, &M);

  int A, B, X;
  rep(i, M) {
    scanf("%d%d%d", &A, &B, &X);
    A--;
    B--;
    X++;
    g[A][B] += 1;
    g[A][B + 1] += -1;
    g[A + X][B] += -1;
    g[A + X + 1][B + 1] += 1;
    g[A + X][B + X + 1] += 1;
    g[A + X + 1][B + X + 1] += -1;
  }

  rep(i, 5001) rep(j, i + 1) g[i][j + 1] += g[i][j];
  rep(j, 5001) REP(i, j, 5000) g[i + 1][j] += g[i][j];
  rep(i, 5001) rep(j, i + 1) g[i + 1][j + 1] += g[i][j];

  int res = 0;
  rep(i, 5001) rep(j, i + 1) res += (g[i][j] >= 1);
  printf("%d\\n", res);
  return 0;
}

Error: MLE

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 SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MOD 1000000007

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

int g[5002][5002];
int N, M;

int main() {
  memset(g, 0, sizeof(g));
  scanf("%d%d", &N, &M);

  int A, B, X;
  rep(i, M) {
    scanf("%d%d%d", &A, &B, &X);
    A--;
    B--;
    X++;
    g[A][B] += 1;
    g[A][B + 1] += -1;
    g[A + X][B] += -1;
    g[A + X + 1][B + 1] += 1;
    g[A + X][B + X + 1] += 1;
    g[A + X + 1][B + X + 1] += -1;
  }

  /*  rep(i, N+1){
    rep(j, N+1) printf("%d ", g[i][j]);
    puts("");
    }*/

  rep(i, 5001) rep(j, 5000) g[i][j + 1] += g[i][j];

  /*  rep(i, N+1){
    rep(j, N+1) printf("%d ", g[i][j]);
    puts("");
    }*/

  rep(j, 5001) rep(i, 5000) g[i + 1][j] += g[i][j];

  /*  rep(i, N+1){
    rep(j, N+1) printf("%d ", g[i][j]);
    puts("");
    }*/

  rep(i, 5001) rep(j, 5001) g[i + 1][j + 1] += g[i][j];

  /*  rep(i, N+1){
    rep(j, N+1) printf("%d ", g[i][j]);
    puts("");
    }*/

  int res = 0;
  rep(i, 5001) rep(j, 5001) res += (g[i][j] >= 1);
  printf("%d\\n", res);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
short flag[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);
    flag[A][B] = max<short>(flag[A][B], C + 1);
  }
  int ret = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      flag[i][j] =
          max<short>(flag[i][j], max(flag[i - 1][j - 1], flag[i - 1][j]) - 1);
      if (flag[i][j] > 0)
        ret++;
    }
  }
  cout << ret << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
short flag[5000][5000];
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);
    flag[A][B] = max<short>(flag[A][B], C + 1);
  }
  int ret = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      flag[i][j] =
          max<short>(flag[i][j], max(flag[i - 1][j - 1], flag[i - 1][j]) - 1);
      if (flag[i][j] > 0)
        ret++;
    }
  }
  cout << ret << endl;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int n, m, ans = 0;
// int tri[5005][5005];
vector<int> tri[5005];

int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= 5003; i++) {
    for (int j = 0; j <= i + 3; j++)
      tri[i].push_back(0);
  }
  for (int i = 0; i < m; i++) {
    int x, y, size;
    scanf("%d%d%d", &y, &x, &size);
    size++;
    tri[y][x] += 1;
    tri[y][x + 1] -= 1;
    tri[y + size][x] -= 1;
    tri[y + size + 1][x + 1] += 1;
    tri[y + size][x + size + 1] += 1;
    tri[y + size + 1][x + size + 1] -= 1;
  }
  /*
  for(int i=1;i<=n+3;i++){
      for(int j=1;j<=i;j++){
          printf("%d%c",tri[i][j],i==j?'\\n':' ');
      }
  }
  printf("o\\n");
  */
  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= i; j++) {
      tri[i][j] += tri[i][j - 1];
    }
  }
  /*
  for(int i=1;i<=n;i++){
      for(int j=1;j<=i;j++){
          printf("%d%c",tri[i][j],i==j?'\\n':' ');
      }
  }
  printf("o\\n");
  */
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      tri[j][i] += tri[j - 1][i];
    }
  }
  /*
  printf("o\\n");
  for(int i=1;i<=n;i++){
      for(int j=1;j<=i;j++){
          printf("%d%c",tri[i][j],i==j?'\\n':' ');
      }
  }
  */
  for (int i = 1; i <= n; i++) {
    int y = i + 1, x = 2;
    while (y <= n) {
      tri[y][x] += tri[y - 1][x - 1];
      y++, x++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (tri[i][j] >= 1)
        ans++;
      // printf("%d%c",tri[i][j],i==j?'\\n':' ');
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int n, m, ans = 0;
int tri[5005][5005];

int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y, size;
    scanf("%d%d%d", &y, &x, &size);
    size++;
    tri[x][y] += 1;
    tri[x + 1][y] -= 1;
    tri[x][y + size] -= 1;
    tri[x + 1][y + size + 1] += 1;
    tri[x + size + 1][y + size] += 1;
    tri[x + size + 1][y + size + 1] -= 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 2; j <= i; j++) {
      tri[j][i] += tri[j - 1][i];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      tri[i][j] += tri[i][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    int y = i + 1, x = 2;
    while (y <= n) {
      tri[x][y] += tri[x - 1][y - 1];
      y++, x++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (tri[j][i] >= 1)
        ans++;
      // printf("%d%c",tri[j][i],j==i?'\\n':' ');
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

short max(int a, int b) {
  if (a > b)
    return a;
  return b;
}

short nails[5001][5001];

int main(void) {

  int n, m, a, b, x;

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    nails[a][b] = x + 1;
  }

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

  int cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i; j++)
      if (nails[i][j] > 0)
        cnt++;

  cout << cnt << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

int nails[5001][5001];

int main(void) {

  int n, m, a, b, x;

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    nails[a][b] = x + 1;
  }

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

  int cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i; j++)
      if (nails[i][j] > 0)
        cnt++;

  cout << cnt << endl;

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

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

short int D[5000][5000];

int main() {
  int N, M;
  int i, j;
  int ans = 0;
  cin >> N >> M;
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      D[i][j] = -1;
    }
  }
  for (i = 0; i < M; i++) {
    short int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    D[a][b] = max(D[a][b], c);
  }
  for (i = 0; i < N - 1; i++) {
    for (j = 0; j < i + 1; j++) {
      D[i + 1][j] = max((short int)(D[i][j] - 1), D[i + 1][j]);
      D[i + 1][j + 1] = max((short int)(D[i][j] - 1), D[i + 1][j + 1]);
    }
  }
  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      if (D[i][j] >= 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
#define repn(i, m, n) for (i = m; i < n; i++)
// #define DEBUG DEBUG

short imos[5000][5001];
int n, m;

int main() {
  int i, j, a, b, size;
  cin >> n >> m;
  rep(i, m) {
    cin >> a >> b >> size;
    a--;
    b--;
    repn(j, a, a + size + 1) {
      int count;
      count = j - a + 1;
      imos[j][b]++;
      imos[j][b + count]--;
    }
  }
  rep(i, n) {
    int jyu;
    jyu = 0;
    rep(j, n) {
      jyu += imos[i][j];
      imos[i][j] = jyu;
    }
  }
#ifdef DEBUG
  rep(i, n) {
    rep(j, n) cout << imos[i][j];
    cout << endl;
  }
#endif
  int sum = 0;
  rep(i, n) {
    rep(j, n) {
      if (imos[i][j] >= 1)
        sum++;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
typedef pair<int, int> pr;
typedef pair<pr, int> ppr;
typedef vector<pr> vpr;
int maps[2][5001];
typedef priority_queue<ppr, vector<ppr>, greater<ppr>> pq;
// inline int max(int a,int b){return (a<b)?b:a;}
int main() {
  int N, M, x, y, size;
  pq nail;

  cin >> N >> M;
  REP(i, M) {
    scanf("%d%d%d", &x, &y, &size);
    nail.push(ppr(pr(x, y), size + 1));
  }
  int sum = 0;
  REP(i, N + 1) {
    REP(j, N + 1)
    maps[(i + 1) & 1][j] = 0;
    while (!nail.empty() && nail.top().first.first == i) {
      maps[i & 1][nail.top().first.second] =
          max(maps[i & 1][nail.top().first.second], nail.top().second);
      nail.pop();
    }
    REP(j, N + 1) {
      maps[(i + 1) & 1][j + 1] =
          max(maps[(i + 1) & 1][j + 1], maps[i & 1][j] - 1);
      maps[(i + 1) & 1][j] = max(maps[(i + 1) & 1][j], maps[i & 1][j] - 1);
      if (maps[i & 1][j])
        sum++;
    }
  }
  cout << sum << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
int maps[5001][5001];
inline int max(int a, int b) { return (a < b) ? b : a; }
int main() {
  int N, M, x, y, size;
  cin >> N >> M;
  REP(i, M) {
    scanf("%d%d%d", &x, &y, &size);
    maps[x][y] = max(size + 1, maps[x][y]);
  }
  int sum = 0;
  REP(i, N + 1)
  REP(j, i + 1) {

    maps[i + 1][j + 1] = max(maps[i + 1][j + 1], maps[i][j] - 1);
    maps[i + 1][j] = max(maps[i + 1][j], maps[i][j] - 1);
    if (maps[i][j])
      sum++;
  }
  cout << sum << endl;
  return 0;
}