Problem p03717 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int N = 333;
typedef pair<int, int> Pairs;

inline char get(void) {
  static char buf[100000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T)
      return EOF;
  }
  return *S++;
}
template <typename T> inline void read(T &x) {
  static char c;
  x = 0;
  int sgn = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    if (c == '-')
      sgn = 1;
  for (; c >= '0' && c <= '9'; c = get())
    x = x * 10 + c - '0';
  if (sgn)
    x = -x;
}

int n, m, x, y, z, w, ans;
vector<Pairs> v[N];
int dp[N][N][N];
int ban[N][N][N];

inline void Add(int &x, int a) { x = (x + a) % MOD; }
inline void Update(int r, int g, int b, int w) {
  if (ban[r][g][b])
    return;
  Add(dp[r][g][b], w);
}
inline void Check(int r, int g, int b) {
  int k = max(r, max(g, b));
  ban[r][g][b] = 0;
  for (auto x : v[k])
    if ((r >= x.first) + (g >= x.first) + (b >= x.first) != x.second)
      return (void)(ban[r][g][b] = 1);
}

int main(void) {
  read(n);
  read(m);
  for (int i = 0; i < m; i++) {
    read(x);
    read(y);
    read(z);
    v[y].push_back(Pairs(x, z));
  }
  for (int r = 0; r <= n; r++)
    for (int g = 0; g <= n; g++)
      for (int b = 0; b <= n; b++)
        Check(r, g, b);
  dp[0][0][0] = 1;
  for (int r = 0; r <= n; r++)
    for (int g = 0; g <= n; g++)
      for (int b = 0; b <= n; b++)
        if (w = dp[r][g][b]) {
          int k = max(r, max(g, b));
          Update(k + 1, g, b, w);
          Update(r, k + 1, b, w);
          Update(r, g, k + 1, w);
          if (k == n)
            Add(ans, w);
        }
  cout << ans << endl;
  return 0;
}


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

const int MOD = 1000000007;
const int N = 305;
typedef pair<int, int> Pairs;

inline char get(void) {
  static char buf[100000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T)
      return EOF;
  }
  return *S++;
}
template <typename T> inline void read(T &x) {
  static char c;
  x = 0;
  int sgn = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    if (c == '-')
      sgn = 1;
  for (; c >= '0' && c <= '9'; c = get())
    x = x * 10 + c - '0';
  if (sgn)
    x = -x;
}

int n, m, x, y, z, w, ans;
vector<Pairs> v[N];
int dp[N][N][N];
int ban[N][N][N];

inline void Add(int &x, int a) { x = (x + a) % MOD; }
inline void Update(int r, int g, int b, int w) {
  if (ban[r][g][b])
    return;
  Add(dp[r][g][b], w);
}
inline void Check(int r, int g, int b) {
  int k = max(r, max(g, b));
  ban[r][g][b] = 0;
  for (auto x : v[k])
    if ((r >= x.first) + (g >= x.first) + (b >= x.first) != x.second)
      return (void)(ban[r][g][b] = 1);
}

int main(void) {
  read(n);
  read(m);
  for (int i = 0; i < m; i++) {
    read(x);
    read(y);
    read(z);
    v[y].push_back(Pairs(x, z));
  }
  for (int r = 0; r <= n; r++)
    for (int g = 0; g <= n; g++)
      for (int b = 0; b <= n; b++)
        Check(r, g, b);
  dp[0][0][0] = 1;
  for (int r = 0; r <= n; r++)
    for (int g = 0; g <= n; g++)
      for (int b = 0; b <= n; b++)
        if (w = dp[r][g][b]) {
          int k = max(r, max(g, b));
          Update(k + 1, g, b, w);
          Update(r, k + 1, b, w);
          Update(r, g, k + 1, w);
          if (k == n)
            Add(ans, w);
        }
  cout << ans << endl;
  return 0;
}