Problem p03028 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#ifdef LOCAL
#define show(x)                                                                \
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

int N;
bool A[2010][2010];
bool L[2010][2010];
bool R[2010][2010];

using BS = bitset<2010>;
V<BS> Rix(2010);
V<BS> Lxi(2010);
V<BS> Aix(2010);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !!
  cout << fixed << setprecision(20);

  cin >> N;
  {
    V<string> s(N);
    rep1(i, N - 1) cin >> s[i];
    rep(i, N) rep(j, N) {
      if (i > j)
        A[i][j] = s[i][j] == '1';
      if (j > i)
        A[i][j] = s[j][i] == '0';
    }
  }
  if (false) {
    rep(i, N) {
      rep(j, N) cout << A[i][j] << " ";
      cout << endl;
    }
  }
  rep(i, N) { rep(j, N) if (A[i][j]) Aix[i].set(j); }

  for (int d = 1; d <= N; d++) {
    for (int l = 0; l + d - 1 < N; l++) {
      int r = l + d - 1;
      if (d == 1) {
        Lxi[r].set(l);
        Rix[l].set(r);
        L[l][r] = true;
        R[l][r] = true;
      } else {
        { // L
          // for(int x=l+1;x<=r;x++){
          // 	if(A[l][x] && R[l+1][x] && L[x][r]){
          // 		L[l][r] = true;
          // 		break;
          // 	}
          // }
          auto tmp = Aix[l] & Rix[l + 1] & Lxi[r];
          if (tmp.any())
            Lxi[r].set(l);
        }
        { // R
          for (int x = l; x < r; x++) {
            if (A[r][x] && L[x][r - 1] && R[l][x]) {
              R[l][r] = true;
              break;
            }
          }
          auto tmp = Aix[r] & Rix[l] & Lxi[r - 1];
          if (tmp.any())
            Rix[l].set(r);
        }
      }
    }
  }
  int ans = 0;
  rep(i, N) if (Rix[0][i] && Lxi[N - 1][i]) {
    ans++;
    show(i);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#ifdef LOCAL
#define show(x)                                                                \
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

int N;
bool A[2010][2010];
bool L[2010][2010];
bool R[2010][2010];

using BS = bitset<2010>;
V<BS> Rix(2010);
V<BS> Lxi(2010);
V<BS> Aix(2010);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !!
  cout << fixed << setprecision(20);

  cin >> N;
  {
    V<string> s(N);
    rep1(i, N - 1) cin >> s[i];
    rep(i, N) rep(j, N) {
      if (i > j)
        A[i][j] = s[i][j] == '1';
      if (j > i)
        A[i][j] = s[j][i] == '0';
    }
  }
  if (false) {
    rep(i, N) {
      rep(j, N) cout << A[i][j] << " ";
      cout << endl;
    }
  }
  rep(i, N) { rep(j, N) if (A[i][j]) Aix[i].set(j); }

  for (int d = 1; d <= N; d++) {
    for (int l = 0; l + d - 1 < N; l++) {
      int r = l + d - 1;
      if (d == 1) {
        Lxi[r].set(l);
        Rix[l].set(r);
        L[l][r] = true;
        R[l][r] = true;
      } else {
        { // L
          // for(int x=l+1;x<=r;x++){
          // 	if(A[l][x] && R[l+1][x] && L[x][r]){
          // 		L[l][r] = true;
          // 		break;
          // 	}
          // }
          auto tmp = Aix[l] & Rix[l + 1] & Lxi[r];
          if (tmp.any())
            Lxi[r].set(l);
        }
        { // R
          // for(int x=l;x<r;x++){
          // 	if(A[r][x] && L[x][r-1] && R[l][x]){
          // 		R[l][r] = true;
          // 		break;
          // 	}
          // }
          auto tmp = Aix[r] & Rix[l] & Lxi[r - 1];
          if (tmp.any())
            Rix[l].set(r);
        }
      }
    }
  }
  int ans = 0;
  rep(i, N) if (Rix[0][i] && Lxi[N - 1][i]) {
    ans++;
    show(i);
  }
  cout << ans << endl;
}