Problem p03717 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define R register int
#define AC 330
#define ac 500
#define mod 1000000007
#define LL long long

int n, m;
int Head[AC], date[ac], Next[ac], len[ac], tot;
LL f[AC][AC][AC], ans;

inline int read() {
  int x = 0;
  char c = getchar();
  while (c > '9' || c < '0')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

inline void add(int f, int w, int S) {
  date[++tot] = w, Next[tot] = Head[f], Head[f] = tot, len[tot] = S;
}

inline void pre() {
  int a, b, c;
  n = read(), m = read();
  for (R i = 1; i <= m; i++) {
    a = read(), b = read(), c = read();
    add(b, a, c); // 以右端点为标准
  }
}

inline int Max(int x, int y, int z) {
  if (y > x)
    x = y;
  if (z > x)
    x = z;
  return x;
}

inline bool check(int x, int y, int z) {
  if (x && y && x == y)
    return false;
  if (x && z && x == z)
    return false;
  if (y && z && y == z)
    return false;
  int k = Max(x, y, z);
  for (R i = Head[k]; i; i = Next[i]) {
    int now = date[i], v = len[i], tmp = 0;
    if (x >= now)
      ++tmp;
    if (y >= now)
      ++tmp;
    if (z >= now)
      ++tmp;
    if (tmp != v)
      return false;
  }
  return true;
}

void work() {
  f[0][0][0] = 1;
  for (R i = 0; i <= n; i++)
    for (R j = 0; j <= n; j++)
      for (R l = 0; l <= n; l++) {
        int k = Max(i, j, l);
        if (!f[i][j][l])
          continue;
        // printf("(%d, %d, %d) = %lld\n", i, j, l, f[i][j][l]);
        if (!check(i, j, l))
          continue;
        LL t = f[i][j][l];
        f[k + 1][j][l] = (f[k + 1][j][l] + t) % mod;
        f[i][k + 1][l] = (f[i][k + 1][l] + t) % mod;
        f[i][j][k + 1] = (f[i][j][k + 1] + t) % mod;
        if (k == n)
          ans = (ans + f[i][j][l]) % mod; // 必须到排到了n
      }
  printf("%lld\n", ans);
}

int main() {
  pre();
  work();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define R register int
#define AC 302
#define ac 500
#define mod 1000000007
#define LL long long

int n, m;
int Head[AC], date[ac], Next[ac], len[ac], tot;
LL f[AC][AC][AC], ans;

inline int read() {
  int x = 0;
  char c = getchar();
  while (c > '9' || c < '0')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

inline void add(int f, int w, int S) {
  date[++tot] = w, Next[tot] = Head[f], Head[f] = tot, len[tot] = S;
}

inline void pre() {
  int a, b, c;
  n = read(), m = read();
  for (R i = 1; i <= m; i++) {
    a = read(), b = read(), c = read();
    add(b, a, c); // 以右端点为标准
  }
}

inline int Max(int x, int y, int z) {
  if (y > x)
    x = y;
  if (z > x)
    x = z;
  return x;
}

inline bool check(int x, int y, int z) {
  if (x && y && x == y)
    return false;
  if (x && z && x == z)
    return false;
  if (y && z && y == z)
    return false;
  int k = Max(x, y, z);
  for (R i = Head[k]; i; i = Next[i]) {
    int now = date[i], v = len[i], tmp = 0;
    if (x >= now)
      ++tmp;
    if (y >= now)
      ++tmp;
    if (z >= now)
      ++tmp;
    if (tmp != v)
      return false;
  }
  return true;
}

void work() {
  f[0][0][0] = 1;
  for (R i = 0; i <= n; i++)
    for (R j = 0; j <= n; j++)
      for (R l = 0; l <= n; l++) {
        int k = Max(i, j, l);
        if (!f[i][j][l])
          continue;
        // printf("(%d, %d, %d) = %lld\n", i, j, l, f[i][j][l]);
        if (!check(i, j, l))
          continue;
        LL t = f[i][j][l];
        f[k + 1][j][l] = (f[k + 1][j][l] + t) % mod;
        f[i][k + 1][l] = (f[i][k + 1][l] + t) % mod;
        f[i][j][k + 1] = (f[i][j][k + 1] + t) % mod;
        if (k == n)
          ans = (ans + f[i][j][l]) % mod; // 必须到排到了n
      }
  printf("%lld\n", ans);
}

int main() {
  pre();
  work();
  return 0;
}