Problem p03717 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

const int maxn = 310;
const int mod = 1000000007;

vector<int> ter[maxn];
int f[maxn][maxn][maxn];
set<int> s;
int l[maxn], r[maxn], x[maxn];

bool check(int r, int g, int b) {
  if ((r == g && r != 0) || (g == b && g != 0) || (r == b && r != 0))
    return false;
  int k = max(r, max(g, b));
  s.clear();
  s.insert(r);
  s.insert(g);
  s.insert(b);
  s.erase(k);
  int t1 = *s.begin();
  s.erase(t1);
  int t2 = *s.begin();

  for (int i = 0; i < (int)ter[k].size(); i++) {
    if (l[ter[k][i]] <= k && l[ter[k][i]] >= max(t1, t2) + 1) {
      if (x[ter[k][i]] != 1)
        return false;
    }
    if (l[ter[k][i]] <= max(t1, t2) && l[ter[k][i]] > min(t1, t2)) {
      if (x[ter[k][i]] != 2)
        return false;
    }
    if (l[ter[k][i]] <= min(t1, t2)) {
      if (x[ter[k][i]] != 3)
        return false;
    }
  }
  return true;
}

int main() {
  // freopen("E.in","r",stdin);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &l[i], &r[i], &x[i]);
    ter[r[i]].push_back(i);
  }

  f[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 (!check(r, g, b)) {
          f[r][g][b] = 0;
          continue;
        }
        // cout<<r<<" "<<g<<" "<<b<<" "<<f[r][g][b]<<endl;
        int k = max(r, max(g, b));
        // R
        f[k + 1][g][b] = (f[k + 1][g][b] + f[r][g][b]) % mod;
        // G
        f[r][k + 1][b] = (f[r][k + 1][b] + f[r][g][b]) % mod;
        // B
        f[r][g][k + 1] = (f[r][g][k + 1] + f[r][g][b]) % mod;
      }
    }
  }

  int sum = 0;
  for (int r = 0; r <= n; r++) {
    for (int g = 0; g <= n; g++) {
      for (int b = 0; b <= n; b++)
        if (max(r, max(g, b)) == n)
          sum = (sum + f[r][g][b]) % mod;
    }
  }
  printf("%d\n", sum);
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

const int maxn = 310;
const int mod = 1000000007;

vector<int> ter[maxn];
int f[maxn][maxn][maxn];
set<int> s;
int l[maxn], r[maxn], x[maxn];

bool check(int r, int g, int b) {
  if ((r == g && r != 0) || (g == b && g != 0) || (r == b && r != 0))
    return false;
  int k = max(r, max(g, b));
  int t1, t2;
  if (k == r)
    t1 = g, t2 = b;
  if (k == g)
    t1 = r, t2 = b;
  if (k == b)
    t1 = r, t2 = g;

  for (int i = 0; i < (int)ter[k].size(); i++) {
    if (l[ter[k][i]] <= k && l[ter[k][i]] >= max(t1, t2) + 1) {
      if (x[ter[k][i]] != 1)
        return false;
    }
    if (l[ter[k][i]] <= max(t1, t2) && l[ter[k][i]] > min(t1, t2)) {
      if (x[ter[k][i]] != 2)
        return false;
    }
    if (l[ter[k][i]] <= min(t1, t2)) {
      if (x[ter[k][i]] != 3)
        return false;
    }
  }
  return true;
}

int main() {
  // freopen("E.in","r",stdin);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &l[i], &r[i], &x[i]);
    ter[r[i]].push_back(i);
  }

  f[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 (!check(r, g, b)) {
          f[r][g][b] = 0;
          continue;
        }
        // cout<<r<<" "<<g<<" "<<b<<" "<<f[r][g][b]<<endl;
        int k = max(r, max(g, b));
        // R
        f[k + 1][g][b] = (f[k + 1][g][b] + f[r][g][b]) % mod;
        // G
        f[r][k + 1][b] = (f[r][k + 1][b] + f[r][g][b]) % mod;
        // B
        f[r][g][k + 1] = (f[r][g][k + 1] + f[r][g][b]) % mod;
      }
    }
  }

  int sum = 0;
  for (int r = 0; r <= n; r++) {
    for (int g = 0; g <= n; g++) {
      for (int b = 0; b <= n; b++)
        if (max(r, max(g, b)) == n)
          sum = (sum + f[r][g][b]) % mod;
    }
  }
  printf("%d\n", sum);
  return 0;
}