Problem p03717 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 310;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
int f[N][N][N];
int L[N][4], R[N][4];
inline bool check(int r, int i, int j, int k) {
  for (int t = 1; t <= 3; t++)
    if (R[r][t]) {
      if ((i >= R[r][t]) + (j >= R[r][t]) + (k >= R[r][t]) < t)
        return false;
      if ((i >= L[r][t]) + (j >= L[r][t]) + (k >= L[r][t]) > t)
        return false;
    }
  return true;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("arc74c.in", "r", stdin);
  freopen("arc74c.out", "w", stdout);
#endif
  int n = gi(), m = gi(), i, j, k, t, l, r, x, ans = 0;
  const int mod = 1e9 + 7;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= 3; j++)
      L[i][j] = 1 << 30, R[i][j] = 0;
  while (m--) {
    l = gi(), r = gi(), x = gi();
    if (r == 1 && x != 1)
      return puts("0"), 0;
    L[r][x] = min(L[r][x], l);
    R[r][x] = max(R[r][x], l);
  }
  f[1][0][0] = 3;
  for (i = 1; i <= n; i++)
    for (j = 0; j < i; j++)
      for (k = 0; k < i; k++)
        if (t = f[i][j][k]) {
          if (check(i + 1, i + 1, j, k))
            (f[i + 1][j][k] += t) %= mod;
          if (check(i + 1, i, i + 1, k))
            (f[i + 1][i][k] += t) %= mod;
          if (check(i + 1, i, j, i + 1))
            (f[i + 1][i][j] += t) %= mod;
        }
  for (j = 0; j < n; j++)
    for (k = 0; k < n; k++)
      (ans += f[n][j][k]) %= mod;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 310;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
int f[N][N][N];
int L[N][4], R[N][4];
inline bool check(int r, int i, int j, int k) {
  for (int t = 1; t <= 3; t++)
    if (R[r][t]) {
      if ((i >= R[r][t]) + (j >= R[r][t]) + (k >= R[r][t]) < t)
        return false;
      if ((i >= L[r][t]) + (j >= L[r][t]) + (k >= L[r][t]) > t)
        return false;
    }
  return true;
}
int main() {
  int n = gi(), m = gi(), i, j, k, t, l, r, x, ans = 0;
  const int mod = 1e9 + 7;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= 3; j++)
      L[i][j] = 1 << 30, R[i][j] = 0;
  while (m--) {
    l = gi(), r = gi(), x = gi();
    if (r == 1 && x != 1)
      return puts("0"), 0;
    L[r][x] = min(L[r][x], l);
    R[r][x] = max(R[r][x], l);
  }
  f[1][0][0] = 3;
  for (i = 1; i <= n; i++)
    for (j = 0; j < i; j++)
      for (k = 0; k < i; k++)
        if (t = f[i][j][k]) {
          if (check(i + 1, i + 1, j, k))
            (f[i + 1][j][k] += t) %= mod;
          if (check(i + 1, i, i + 1, k))
            (f[i + 1][i][k] += t) %= mod;
          if (check(i + 1, i, j, i + 1))
            (f[i + 1][i][j] += t) %= mod;
        }
  for (j = 0; j < n; j++)
    for (k = 0; k < n; k++)
      (ans += f[n][j][k]) %= mod;
  printf("%d\n", ans);
  return 0;
}