Problem p01695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;

int main() {
  while (cin >> n && n) {
    bool f[100][100] = {};
    vector<int> vi;
    vector<string> vs;
    int pre = 0;
    vector<P> buf[2000];
    REP(i, n) {
      string s;
      cin >> s;
      int cnt = 0;
      for (int j = 0; j <= s.size(); j++) {
        if (s[j] == '.')
          cnt++;
        else
          break;
      }
      if (pre <= cnt) {
        if (cnt != 0)
          buf[cnt].push_back({i, i});
      } else {
        if (cnt != 0) {
          buf[cnt][buf[cnt].size() - 1].second = i;
          buf[cnt].push_back({i, i});
        }
      }
      pre = cnt;
      string t = s.substr(cnt, s.size() - cnt);
      string buf = "";
      REP(j, cnt) {
        if (j == cnt - 1)
          buf += "+";
        else
          buf += " ";
      }
      t = buf + t;
      vs.push_back(t);
      vi.push_back(cnt);
    }
    // REP(i, 5) {
    //	cout << i << endl;
    //	REP(j, buf[i].size()) {
    //		cout << "!" << buf[i][j].first << " "
    //			<< buf[i][j].second << " ";
    //	}
    //	cout << endl;
    // }
    FOR(i, 1, 2000) {
      REP(j, buf[i].size()) {
        P p = buf[i][j];
        for (int k = p.first + 1; k < p.second; k++) {
          vs[k][i - 1] = '|';
        }
      }
      // REP(i, vs.size()) {
      //	cout << vs[i] << endl;
      // }
      // cout << endl;
    }
    REP(i, vs.size()) { cout << vs[i] << endl; }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;

int main() {
  while (cin >> n && n) {
    bool f[100][100] = {};
    vector<int> vi;
    vector<string> vs;
    int pre = 0;
    vector<P> buf[2000];
    REP(i, n) {
      string s;
      cin >> s;
      int cnt = 0;
      for (int j = 0; j <= s.size(); j++) {
        if (s[j] == '.')
          cnt++;
        else
          break;
      }
      if (pre <= cnt) {
        pre = cnt;
        buf[cnt].push_back({i, i});
      } else {
        buf[cnt][buf[cnt].size() - 1].second = i;
        buf[cnt].push_back({i, i});
      }
      string t = s.substr(cnt, s.size() - cnt);
      string buf = "";
      REP(j, cnt) {
        if (j == cnt - 1)
          buf += "+";
        else
          buf += " ";
      }
      t = buf + t;
      vs.push_back(t);
      vi.push_back(cnt);
    }
    FOR(i, 1, 2000) {
      REP(j, buf[i].size()) {
        P p = buf[i][j];
        for (int k = p.first + 1; k < p.second; k++) {
          vs[k][i - 1] = '|';
        }
      }
    }
    REP(i, vs.size()) { cout << vs[i] << endl; }
  }
}