Problem p03028 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000003;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;

bool b[2000][2000];

bool le[2000][2000], ri[2000][2000];
void solve() {
  int n;
  cin >> n;
  rep1(i, n - 1) {
    rep(j, i) {
      char t;
      cin >> t;
      if (t == '1') {
        b[i][j] = true;
      } else {
        b[j][i] = true;
      }
    }
  }
  rep(i, n) le[i][i] = ri[i][i] = true;

  int ans = 0;
  Rep1(i, 2, n) {
    rep(j, n - i + 1) {
      int l = j;
      int r = j + i - 1;
      Rep1(k, l + 1, r) {
        if (b[l][k] && ri[l + 1][k] && le[r][k]) {
          le[r][l] = true;
          break;
        }
      }
      Rep(k, l, r) {
        if (b[r][k] && ri[l][k] && le[r - 1][k]) {
          ri[l][r] = true;
          break;
        }
      }
    }
  }
  rep(i, n) {
    if (ri[0][i] && le[n - 1][i])
      ans++;
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(5);
  // while (cin >> n)solve();
  solve();
  // stop
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000003;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;

bool b[2000][2000];

bool le[2000][2000], ri[2000][2000];
void solve() {
  int n;
  cin >> n;
  rep1(i, n - 1) {
    rep(j, i) {
      char t;
      cin >> t;
      if (t == '1') {
        b[i][j] = true;
      } else {
        b[j][i] = true;
      }
    }
  }
  rep(i, n) le[i][i] = ri[i][i] = true;

  int ans = 0;
  Rep1(i, 2, n) {
    rep(j, n - i + 1) {
      int l = j;
      int r = j + i - 1;
      bool fl = false, fr = false;
      Rep1(k, l + 1, r) { fl |= b[l][k] & ri[l + 1][k] & le[r][k]; }
      Rep(k, l, r) { fr |= b[r][k] & ri[l][k] & le[r - 1][k]; }
      le[r][l] = fl, ri[l][r] = fr;
    }
  }
  rep(i, n) {
    if (ri[0][i] && le[n - 1][i])
      ans++;
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(5);
  // while (cin >> n)solve();
  solve();
  // stop
  return 0;
}