Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep0(i, n) for (int i = 0; i <= (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using Graph = vector<vi>; // vi <=> vl
using vb = vector<bool>;
using boolGraph = vector<vb>;
using P = pair<int, int>; // int <=> ll
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const double EPS = 1e-9;

template <typename T> struct is_vector : false_type {};

template <typename T> struct is_vector<vector<T>> : true_type {};

template <typename T> ostream &operator<<(ostream &ostr, const vector<T> &vec) {
  if (vec.empty()) {
    return ostr;
  }
  ostr << vec.front();
  for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) {
    if (is_vector<T>())
      ostr << "\n" << *itr;
    else
      ostr << ' ' << *itr;
  }
  return ostr;
}

void solve() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> G(h);
  rep(i, h) cin >> G[i];

  Graph ans(h, vi(w));
  int idx = 1;
  vi vec_h;
  rep(i, h) {
    int prev = 0;
    rep(j, w) {
      if (G[i][j] == '#') {
        while (prev <= j) {
          ans[i][prev] = idx;
          for (auto p : vec_h) {
            ans[p][prev] = idx;
          }
          ++prev;
        }
        ++idx;
      }
    }
    if (prev == 0) {
      vec_h.push_back(h);
      continue;
    }
    while (prev < w) {
      ans[i][prev] = ans[i][prev - 1];
      for (auto p : vec_h) {
        ans[p][prev] = ans[p][prev - 1];
      }
      ++prev;
    }
    vec_h.clear();
  }
  rep(j, w) {
    for (auto p : vec_h) {
      ans[p][j] = ans[p - 1][j];
    }
  }

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep0(i, n) for (int i = 0; i <= (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using Graph = vector<vi>; // vi <=> vl
using vb = vector<bool>;
using boolGraph = vector<vb>;
using P = pair<int, int>; // int <=> ll
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const double EPS = 1e-9;

template <typename T> struct is_vector : false_type {};

template <typename T> struct is_vector<vector<T>> : true_type {};

template <typename T> ostream &operator<<(ostream &ostr, const vector<T> &vec) {
  if (vec.empty()) {
    return ostr;
  }
  ostr << vec.front();
  for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) {
    if (is_vector<T>())
      ostr << "\n" << *itr;
    else
      ostr << ' ' << *itr;
  }
  return ostr;
}

void solve() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> G(h);
  rep(i, h) cin >> G[i];

  Graph ans(h, vi(w));
  int idx = 1;
  vi vec_h;
  rep(i, h) {
    int prev = 0;
    rep(j, w) {
      if (G[i][j] == '#') {
        while (prev <= j) {
          ans[i][prev] = idx;
          for (auto p : vec_h) {
            ans[p][prev] = idx;
          }
          ++prev;
        }
        ++idx;
      }
    }
    if (prev == 0) {
      vec_h.push_back(i);
      continue;
    }
    while (prev < w) {
      ans[i][prev] = ans[i][prev - 1];
      for (auto p : vec_h) {
        ans[p][prev] = ans[p][prev - 1];
      }
      ++prev;
    }
    vec_h.clear();
  }
  rep(j, w) {
    for (auto p : vec_h) {
      ans[p][j] = ans[p - 1][j];
    }
  }

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  solve();
  return 0;
}