prithivMLmods/Coder-Stat huggingface dataset:
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < ((ll)(n)); i++)
#define reg(i, a, b) for (ll i = ((ll)(a)); i <= ((ll)(b)); i++)
#define irep(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (ll i = ((ll)(b)); i >= ((ll)(a)); i--)
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

/*
 */

ll n, m;
ll a[100010], b[100010], c[100010];
ll dp[100010];
ll sum = 0, tmp = 0, ans = 1e18;

int main(void) {
  cin >> n;
  reg(i, 1, n) cin >> a[i];
  reg(i, 1, n) sum += a[i];
  reg(i, 1, n) {
    ans = min(ans, abs(sum - 2 * tmp));
    tmp += a[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < ((ll)(n)); i++)
#define reg(i, a, b) for (ll i = ((ll)(a)); i <= ((ll)(b)); i++)
#define irep(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (ll i = ((ll)(b)); i >= ((ll)(a)); i--)
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;

/*
 */

ll n, m;
ll a[200010];
ll sum = 0, tmp = 0, ans = 1e18;

int main(void) {
  cin >> n;
  reg(i, 1, n) cin >> a[i];
  reg(i, 1, n) sum += a[i];
  reg(i, 1, n) {
    ans = min(ans, abs(sum - 2 * tmp));
    tmp += a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;

long long int Sum(vector<int> A, int i, int j) {
  long long int sum = 0;
  for (int k = i; k < j; k++) {
    sum += A[k];
  }
  return sum;
}

long long int FIND(vector<int> A, long long int sum) {
  double inf = INFINITY;
  long long int left = 0;
  long long int min = inf;
  for (int i = 1; i < A.size(); i++) {
    left = Sum(A, 0, i);
    if (min >= llabs(sum - 2 * left)) {
      min = llabs(sum - 2 * left);
    } else {
      break;
    }
  }
  return min;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long int sum = 0;
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < A.size(); i++) {
    cin >> A[i];
    sum += A[i];
  }
  cout << FIND(A, sum) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;

long long int Sum(vector<int> A, int i, int j) {
  long long int sum = 0;
  for (int k = i; k < j; k++) {
    sum += A[k];
  }
  return sum;
}

long long int FIND(vector<int> A, long long int sum) {
  double inf = INFINITY;
  long long int left = 0;
  long long int min = inf;
  for (int i = 0; i < A.size(); i++) {
    left += A[i];
    if (min >= llabs(sum - 2 * left)) {
      min = llabs(sum - 2 * left);
    } else {
      break;
    }
  }
  return min;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long int sum = 0;
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < A.size(); i++) {
    cin >> A[i];
    sum += A[i];
  }
  cout << FIND(A, sum) << endl;
  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define mst(a, b) memset(a, b, sizeof(a))
#define scd(a) scanf("%d", &a)
#define scdd(a, b) scanf("%d%d", &a, &b)
#define scddd(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define dbg(a) cout << "* " << #a << " : " << a << endl
#define fr first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define lowbit(x) x & (-x)
#define ac cout << ans << endl
// #define DEBUG 0
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ull hashp = 1e7 + 19;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 100;
ll a[maxn];
void solve() {
  int n;
  scd(n);
  ll ans = INF;
  ll sum = 0;
  rep1(i, n) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  ll now = 0;
  rep1(i, n - 1) {
    now += a[i];
    ans = min(ans, abs(sum - 2 * now));
  }
  ac;
}
int main() {
  // IOS;
  // freopen("D://data.txt","r",stdin);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define mst(a, b) memset(a, b, sizeof(a))
#define scd(a) scanf("%d", &a)
#define scdd(a, b) scanf("%d%d", &a, &b)
#define scddd(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define dbg(a) cout << "* " << #a << " : " << a << endl
#define fr first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define lowbit(x) x & (-x)
#define ac cout << ans << endl
// #define DEBUG 0
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ull hashp = 1e7 + 19;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 100;
ll a[maxn];
void solve() {
  int n;
  scd(n);
  ll ans = INF;
  ll sum = 0;
  rep1(i, n) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  ll now = 0;
  rep1(i, n - 1) {
    now += a[i];
    ans = min(ans, abs(sum - 2 * now));
  }
  ac;
}
int main() {
  // IOS;
  // freopen("D://data.txt","r",stdin);
  solve();
  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

int main() {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#else
  //    freopen("zalupa.in","r",stdin);
  //    freopen("zalupa.out","w",stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);
  cout << fixed;
  ll sum = 0;
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  int part = 0;
  int part2 = 0;
  int i = 0;
  while (part * 2 < sum) {
    if ((part + a[i]) * 2 >= sum) {
      part2 = part;
    }
    part += a[i];
    ++i;
  }
  cout << min(2 * part - sum, sum - 2 * part2);
  return 0;
}

Fixed submission:
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

int main() {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#else
  //    freopen("zalupa.in","r",stdin);
  //    freopen("zalupa.out","w",stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);
  cout << fixed;
  ll sum = 0;
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  ll part = 0;
  ll part2 = 0;
  int i = 0;
  while (part * 2 < sum) {
    if ((part + a[i]) * 2 >= sum) {
      part2 = part;
    }
    part += a[i];
    ++i;
  }
  cout << min(2 * part - sum, sum - 2 * part2);
  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
int main() {
  int n;
  cin >> n;
  ll sum = 0;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll sum1 = 0;
  ll ans = sum;
  for (int i = 0; i < n; i++) {
    sum1 += a[i];
    ans = min(ans, abs(sum - sum1 * 2));
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200005];
int main() {
  int n;
  cin >> n;
  ll sum = 0;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll sum1 = 0;
  ll ans = sum;
  for (int i = 0; i < n; i++) {
    sum1 += a[i];
    ans = min(ans, abs(sum - sum1 * 2));
  }
  cout << ans << '\n';
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

void solve() {
  int n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  vl acc(n + 1);
  acc[0] = 0;
  rep(i, n) acc[i + 1] = acc[i] + a[i];
  dump(a, acc);
  ll l, r;
  int m;
  l = a[0];
  r = acc[n] - a[0];
  rep(i, n - 1) {
    if (l >= r) {
      m = i;
      break;
    }
    l += a[i + 1];
    r -= a[i + 1];
    dump(l, r);
  }
  dump(m);
  ll d1 = LINF;
  ll d2 = LINF;
  l = acc[m + 1];
  r = acc[n] - acc[m + 1];
  d1 = abs(l - r);
  dump(l, r, d1);
  if (m > 0) {
    l = acc[m];
    r = acc[n] - acc[m];
    d2 = abs(l - r);
    dump(l, r, d2);
  }
  cout << min(d1, d2) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

void solve() {
  int n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  vl acc(n + 1);
  acc[0] = 0;
  rep(i, n) acc[i + 1] = acc[i] + a[i];
  dump(a, acc);
  ll l, r;
  int m = 0;
  l = a[0];
  r = acc[n] - a[0];
  rep(i, n - 1) {
    if (l >= r) {
      m = i;
      break;
    }
    l += a[i + 1];
    r -= a[i + 1];
    dump(l, r);
  }
  dump(m);
  ll d1 = LINF;
  ll d2 = LINF;
  l = acc[m + 1];
  r = acc[n] - acc[m + 1];
  d1 = abs(l - r);
  dump(l, r, d1);
  if (m > 0) {
    l = acc[m];
    r = acc[n] - acc[m];
    d2 = abs(l - r);
    dump(l, r, d2);
  }
  cout << min(d1, d2) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
signed main() {
  int n;
  cin >> n;
  int a[123456];
  a[0] = 0;
  for1(i, n) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  int ans = a[n] * 2;
  for1(i, n) ans = min(ans, abs(2 * a[i] - a[n]));
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
signed main() {
  int n;
  cin >> n;
  int a[223456];
  a[0] = 0;
  for1(i, n) {
    cin >> a[i];
    a[i] += a[i - 1];
  }
  int ans = a[n] * 2;
  for1(i, n) ans = min(ans, abs(2 * a[i] - a[n]));
  cout << ans << endl;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  vector<int64_t> B(N + 1);
  B.at(0) = 0;
  for (int i = 0; i <= N; i++)
    B.at(i) = B.at(i - 1) + A.at(i - 1);
  int64_t a = B.at(N) / 2;
  auto itr = lower_bound(B.begin(), B.end(), a);
  int k = distance(B.begin(), itr);
  int64_t ans = max(B.at(k - 1), B.at(N) - B.at(k));
  ans = B.at(N) - 2 * ans;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  vector<int64_t> B(N + 1);
  B.at(0) = 0;
  for (int i = 1; i <= N; i++)
    B.at(i) = B.at(i - 1) + A.at(i - 1);
  int64_t a = B.at(N) / 2;
  auto itr = lower_bound(B.begin(), B.end(), a);
  int k = distance(B.begin(), itr);
  int64_t ans = max(B.at(k - 1), B.at(N) - B.at(k));
  ans = B.at(N) - 2 * ans;
  cout << ans << endl;

  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = (int)1e5 + 5;
int n, a[N], sum[N];

void init() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  sum[0] = 0;
  for (int i = 1; i <= n; ++i)
    sum[i] = sum[i - 1] + a[i];
}
void solve() {
  int ans = 1e18;
  for (int i = 1; i < n; ++i) {
    ans = min(ans, abs(sum[i] - (sum[n] - sum[i])));
  }
  cout << ans << '\n';
}

int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  init();
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = (int)2e5 + 5;
int n, a[N], sum[N];

void init() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  sum[0] = 0;
  for (int i = 1; i <= n; ++i)
    sum[i] = sum[i - 1] + a[i];
}
void solve() {
  int ans = 1e18;
  for (int i = 1; i < n; ++i) {
    ans = min(ans, abs(sum[i] - (sum[n] - sum[i])));
  }
  cout << ans << '\n';
}

int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  init();
  solve();
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int inf = 1e9 + 7;
const ll INF = 1e18;
ll n, i, s, S, M;
ll a[20010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i], s += a[i];
  M = INF;
  for (i = 0; i < n; i++)
    S += a[i], M = min(M, abs(s - 2 * S));
  cout << M << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int inf = 1e9 + 7;
const ll INF = 1e18;
ll n, i, s, S, M;
ll a[200010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i], s += a[i];
  M = INF;
  for (i = 0; i < n; i++)
    S += a[i], M = min(M, abs(s - 2 * S));
  cout << M << endl;
  return 0;
}
Problem p02854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> b(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    b[i + 1] = b[i];
    b[i + 1] += a[i];
  }
  long long ans = 90000000000;
  long long min;
  for (int i = 0; i <= n - 1; i++) {
    min = abs((b[n] - b[i + 1]) - (b[i + 1] - b[0]));
    if (min < ans)
      ans = min;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    b[i + 1] = b[i];
    b[i + 1] += a[i];
  }
  long long ans = 90000000000;
  long long min;
  for (int i = 0; i <= n - 1; i++) {
    min = abs((b[n] - b[i + 1]) - (b[i + 1] - b[0]));
    if (min < ans)
      ans = min;
  }

  cout << ans << endl;
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, k;
  cin >> h >> w >> k;

  vector<vector<char>> a(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a.at(i).at(j);
    }
  }

  vector<vector<int>> x(h, vector<int>(w));
  vector<bool> exist(h, false);
  int now = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (exist.at(i) && a.at(i).at(j) == '#')
        x.at(i).at(j) = 1;
      if (a.at(i).at(j) == '#')
        exist.at(i) = true;
    }
  }

  for (int i = 0; i < h; i++) {
    if (exist.at(i)) {
      x.at(i).at(0) = now;
      for (int j = 1; j < w; j++) {
        x.at(i).at(j) += x.at(i).at(j - 1);
      }
      now = x.at(i).at(w - 1) + 1;
    }
  }

  for (int i = 1; i < h; i++) {
    if (exist.at(i - 1) == true && exist.at(i) == false) {
      for (int j = 0; j < w; j++) {
        x.at(i).at(j) = x.at(i - 1).at(j);
      }
      exist.at(i) = true;
    }
  }
  for (int i = h - 1; i <= 0; i--) {
    if (exist.at(i + 1) == true && exist.at(i) == false) {
      for (int j = 0; j < w; j++) {
        x.at(i).at(j) = x.at(i + 1).at(j);
      }
      exist.at(i) = true;
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j)
        cout << " ";
      cout << x.at(i).at(j);
    }
    cout << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, k;
  cin >> h >> w >> k;

  vector<vector<char>> a(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a.at(i).at(j);
    }
  }

  vector<vector<int>> x(h, vector<int>(w));
  vector<bool> exist(h, false);
  int now = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (exist.at(i) && a.at(i).at(j) == '#')
        x.at(i).at(j) = 1;
      if (a.at(i).at(j) == '#')
        exist.at(i) = true;
    }
  }

  for (int i = 0; i < h; i++) {
    if (exist.at(i)) {
      x.at(i).at(0) = now;
      for (int j = 1; j < w; j++) {
        x.at(i).at(j) += x.at(i).at(j - 1);
      }
      now = x.at(i).at(w - 1) + 1;
    }
  }

  for (int i = 1; i < h; i++) {
    if (exist.at(i - 1) == true && exist.at(i) == false) {
      for (int j = 0; j < w; j++) {
        x.at(i).at(j) = x.at(i - 1).at(j);
      }
      exist.at(i) = true;
    }
  }
  for (int i = h - 2; i >= 0; i--) {
    if (exist.at(i + 1) == true && exist.at(i) == false) {
      for (int j = 0; j < w; j++) {
        x.at(i).at(j) = x.at(i + 1).at(j);
      }
      exist.at(i) = true;
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j)
        cout << " ";
      cout << x.at(i).at(j);
    }
    cout << "\n";
  }
}
Problem p02855 (Python)

Buggy submission (status: Runtime Error):
H, W, K = map(int, input().split())
c = []
cnt = [0] * W
num = 0
A = [[0] * W for _ in range(H)]

for i in range(H):
    row = list(input())
    c.append(row)
    cnt[i] = row.count("#")


def solve(cl, cr):
    global num
    P = []
    for i in range(cl, cr + 1):
        for j in range(W):
            if c[i][j] == "#":
                P.append(j)
    P = sorted(P)
    for i in range(len(P)):
        v1 = 0
        v2 = W - 1
        if i >= 1:
            v1 = P[i - 1] + 1
        if i < len(P) - 1:
            v2 = P[i]
        num += 1
        for j in range(cl, cr + 1):
            for k in range(v1, v2 + 1):
                A[j][k] = num


vec = []
for i in range(H):
    if cnt[i] >= 1:
        vec.append(i)
for i in range(len(vec)):
    v1 = 0
    v2 = H - 1
    if i >= 1:
        v1 = vec[i - 1] + 1
    if i < len(vec) - 1:
        v2 = vec[i]
    solve(v1, v2)
for i in range(H):
    print(*A[i])


Fixed submission:
H, W, K = map(int, input().split())
c = []
cnt = [0] * H
num = 0
A = [[0] * W for _ in range(H)]

for i in range(H):
    row = list(input())
    c.append(row)
    cnt[i] = row.count("#")


def solve(cl, cr):
    global num
    P = []
    for i in range(cl, cr + 1):
        for j in range(W):
            if c[i][j] == "#":
                P.append(j)
    P = sorted(P)
    for i in range(len(P)):
        v1 = 0
        v2 = W - 1
        if i >= 1:
            v1 = P[i - 1] + 1
        if i < len(P) - 1:
            v2 = P[i]
        num += 1
        for j in range(cl, cr + 1):
            for k in range(v1, v2 + 1):
                A[j][k] = num


vec = []
for i in range(H):
    if cnt[i] >= 1:
        vec.append(i)
for i in range(len(vec)):
    v1 = 0
    v2 = H - 1
    if i >= 1:
        v1 = vec[i - 1] + 1
    if i < len(vec) - 1:
        v2 = vec[i]
    solve(v1, v2)
for i in range(H):
    print(*A[i])
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  int h, w, k, f = 0;
  bool flag;
  cin >> h >> w >> k;
  vector<vector<int>> sss(h, vector<int>(w));
  vector<int> a(k), b(k);
  char x;
  rep(i, h) {
    flag = false;
    rep(j, w) {
      cin >> x;
      if (x == '#') {
        a[f] = i;
        b[f] = j;
        f++;
        if (!flag) {
          rep(t, j) sss[i][t] = f;
        }
        flag = true;
      }
      if (flag) {
        sss[i][j] = f;
      }
    }
  }
  for (int i = a[0] + 1; i < h; i++) {
    rep(j, w) {
      if (sss[i][j] == 0)
        sss[i][j] = sss[i - 1][j];
    }
  }
  for (int i = a[0] - 1; i >= 0; i--) {
    rep(j, w) {
      if (sss[i][j] == 0)
        sss[i][j] = sss[i - 1][j];
    }
  }
  rep(i, h) {
    rep(j, w) cout << sss[i][j] << ' ';
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  int h, w, k, f = 0;
  bool flag;
  cin >> h >> w >> k;
  vector<vector<int>> sss(h, vector<int>(w));
  vector<int> a(k), b(k);
  char x;
  rep(i, h) {
    flag = false;
    rep(j, w) {
      cin >> x;
      if (x == '#') {
        a[f] = i;
        b[f] = j;
        f++;
        if (!flag) {
          rep(t, j) sss[i][t] = f;
        }
        flag = true;
      }
      if (flag) {
        sss[i][j] = f;
      }
    }
  }
  for (int i = a[0] + 1; i < h; i++) {
    rep(j, w) {
      if (sss[i][j] == 0)
        sss[i][j] = sss[i - 1][j];
    }
  }
  for (int i = a[0] - 1; i >= 0; i--) {
    rep(j, w) {
      if (sss[i][j] == 0)
        sss[i][j] = sss[i + 1][j];
    }
  }
  rep(i, h) {
    rep(j, w) cout << sss[i][j] << ' ';
    cout << endl;
  }
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;

const ll MAX = 1e9;

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  V<string> c(h);
  rep(i, h) cin >> c[i];

  V<V<int>> ans(h, V<int>(w, 0));
  int s = 0;
  rep(i, h) {
    if (c[i].find("#") != c[i].npos) {
      s = i;
      break;
    }
  }
  int n = 0;
  for (int i = s; i < h; i++) {
    if (c[i].find("#") == c[i].npos) {
      c[i] = c[i - 1];
      continue;
    }
    n++;
    int f = c[i].find("#");
    rep(j, f + 1) ans[i][j] = n;
    for (int j = f + 1; j < w; j++) {
      if (c[i][j] == '#')
        n++;
      ans[i][j] = n;
    }
  }
  for (int i = h - 1; i >= 0; i--) {
    if (ans[i][0] == 0)
      ans[i] = ans[i + 1];
  }

  rep(i, h) {
    rep(j, w) printf("%d ", ans[i][j]);
    printf("\n");
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;

const ll MAX = 1e9;

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  V<string> c(h);
  rep(i, h) cin >> c[i];

  V<V<int>> ans(h, V<int>(w, 0));
  int s = 0;
  rep(i, h) {
    if (c[i].find("#") != c[i].npos) {
      s = i;
      break;
    }
  }
  int n = 0;
  for (int i = s; i < h; i++) {
    if (c[i].find("#") == c[i].npos) {
      ans[i] = ans[i - 1];
      continue;
    }
    n++;
    int f = c[i].find("#");
    rep(j, f + 1) ans[i][j] = n;
    for (int j = f + 1; j < w; j++) {
      if (c[i][j] == '#')
        n++;
      ans[i][j] = n;
    }
  }
  for (int i = h - 1; i >= 0; i--) {
    if (ans[i][0] == 0)
      ans[i] = ans[i + 1];
  }

  rep(i, h) {
    rep(j, w) printf("%d ", ans[i][j]);
    printf("\n");
  }
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<int>> num(h, vector<int>(w, 0));
  int now = 1, okheight;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        break;
      if (j == w - 1) {
        for (int k = 0; k < w; k++) {
          num[i][k] = -1;
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (num[i][j] == -1)
        break;
      okheight = i;
      while (s[i][j] == '.' && j < w) {
        num[i][j] = now;
        j++;
      }
      if (j != w && s[i][j] == '#') {
        num[i][j] = now;
        j++;
      }
      while (s[i][j] == '.' && j < w) {
        num[i][j] = now;
        j++;
      }
      j--;
      now++;
    }
  }
  int count = 1;
  while (count > 0) {
    count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (num[i][j] != -1)
          break;
        count++;
        if (i != okheight) {
          num[i][j] = num[i + 1][j];
        } else {
          num[i][j] = num[i - 1][j];
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      cout << num[i][j] << ' ';
    }
    cout << num[i][w - 1] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<int>> num(h, vector<int>(w, 0));
  int now = 1, okheight;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        break;
      if (j == w - 1) {
        for (int k = 0; k < w; k++) {
          num[i][k] = -1;
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (num[i][j] == -1)
        break;
      okheight = i;
      while (s[i][j] == '.' && j < w) {
        num[i][j] = now;
        j++;
      }
      if (j != w && s[i][j] == '#') {
        num[i][j] = now;
        j++;
      }
      while (s[i][j] == '.' && j < w) {
        num[i][j] = now;
        j++;
      }
      j--;
      now++;
    }
  }
  int count = 1;
  while (count > 0) {
    count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (num[i][j] != -1)
          break;
        count++;
        if (i < okheight) {
          num[i][j] = num[i + 1][j];
        } else {
          num[i][j] = num[i - 1][j];
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w - 1; j++) {
      cout << num[i][j] << ' ';
    }
    cout << num[i][w - 1] << endl;
  }
}
Problem p02855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 305;
int h, w, k;
char mat[N][N];
int ans[N][N];

int main() {

  scanf("%d %d %d", &h, &w, &k);

  for (int i = 0; i < h; i++)
    scanf(" %s", mat[i]);

  int cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int tmp = j;
      while (tmp < w and mat[i][tmp] != '#')
        tmp++;
      if (tmp == w) {
        if (j == 0)
          break;
        while (j < w)
          ans[i][j++] = cnt;
      } else {
        cnt++;
        while (j <= tmp)
          ans[i][j++] = cnt;
        j = tmp;
      }
    }
  }

  while (1) {
    bool any = false;
    for (int i = 0; i < h; i++) {
      if (ans[i][0] == 0) {
        any = true;
        int add = i + 1 < h ? 1 : -1;
        for (int j = 0; j < w; j++) {
          ans[i][j] = ans[i + add][j];
        }
      }
    }
    if (!any)
      break;
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      printf("%d%c", ans[i][j], " \n"[j + 1 == w]);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 305;
int h, w, k;
char mat[N][N];
int ans[N][N];

int main() {

  scanf("%d %d %d", &h, &w, &k);

  for (int i = 0; i < h; i++)
    scanf(" %s", mat[i]);

  int cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int tmp = j;
      while (tmp < w and mat[i][tmp] != '#')
        tmp++;
      if (tmp == w) {
        if (j == 0)
          break;
        while (j < w)
          ans[i][j++] = cnt;
      } else {
        cnt++;
        while (j <= tmp)
          ans[i][j++] = cnt;
        j = tmp;
      }
    }
  }

  while (1) {
    bool any = false;
    for (int i = 0; i < h; i++) {
      if (ans[i][0] == 0) {
        any = true;
        int add = i + 1 < h ? 1 : -1;
        if (ans[i + add][0] == 0) {
          add *= -1;
          if (i + add < 0 or i + add >= h)
            continue;
          if (ans[i + add][0] == 0)
            continue;
        }
        for (int j = 0; j < w; j++) {
          ans[i][j] = ans[i + add][j];
        }
      }
    }
    if (!any)
      break;
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      printf("%d%c", ans[i][j], " \n"[j + 1 == w]);
    }
  }
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int contains(string s) {
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '#') {
      return 1;
    }
  }
  return 0;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<int>> res(H, vector<int>(W));
  vector<string> s(H);
  int y = 0;
  for (int i = 0; i < H; ++i) {
    cin >> s[i];
    if (contains(s[i])) {
      ++y;
    }
    bool flag = 0;
    for (int j = 0; j < W; ++j) {
      if (s[i][j] == '#') {
        if (flag) {
          ++y;
        }
        flag = true;
      }
      res[i][j] = y;
    }
  }
  for (int i = 1; i < H; ++i) {
    if (!contains(s[i])) {
      res[i] = res[i - 1];
    }
  }
  for (int i = H - 1; i >= 0; --i) {
    if (!contains(s[i])) {
      res[i] = res[i + 1];
    }
  }
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int contains(string s) {
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '#') {
      return 1;
    }
  }
  return 0;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<int>> res(H, vector<int>(W));
  vector<string> s(H);
  int y = 0;
  for (int i = 0; i < H; ++i) {
    cin >> s[i];
    if (contains(s[i])) {
      ++y;
    }
    bool flag = 0;
    for (int j = 0; j < W; ++j) {
      if (s[i][j] == '#') {
        if (flag) {
          ++y;
        }
        flag = true;
      }
      res[i][j] = y;
    }
  }
  for (int i = 1; i < H; ++i) {
    if (!contains(s[i])) {
      res[i] = res[i - 1];
    }
  }
  for (int i = H - 2; i >= 0; --i) {
    if (!contains(s[i])) {
      res[i] = res[i + 1];
    }
  }
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> s(H);
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  int num = 0;
  vector<vector<int>> ans(H, vector<int>(K, 0));
  for (int i = 0; i < H; i++) {
    vector<int> v;
    for (int j = 0; j < W; j++) {
      if (s[i][j] == '#') {
        v.push_back(j);
      }
    }

    if (v.empty()) {
      if (i == 0 || ans[i - 1][0] == 0)
        continue;
      for (int j = 0; j < W; j++) {
        ans[i][j] = ans[i - 1][j];
      }
    } else {
      int j = 0;
      for (int idx = 0; idx < v.size(); idx++) {
        num++;
        while (j <= v[idx]) {
          ans[i][j] = num;
          j++;
        }
      }
      while (j < W) {
        ans[i][j] = num;
        j++;
      }
    }
  }

  for (int i = H - 2; i >= 0; i--) {
    if (ans[i][0] == 0) {
      for (int j = 0; j < W; j++) {
        ans[i][j] = ans[i + 1][j];
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << ans[i][j] << " \n"[j + 1 == W];
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> s(H);
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  int num = 0;
  vector<vector<int>> ans(H, vector<int>(W, 0));
  for (int i = 0; i < H; i++) {
    vector<int> v;
    for (int j = 0; j < W; j++) {
      if (s[i][j] == '#') {
        v.push_back(j);
      }
    }

    if (v.empty()) {
      if (i == 0 || ans[i - 1][0] == 0)
        continue;
      for (int j = 0; j < W; j++) {
        ans[i][j] = ans[i - 1][j];
      }
    } else {
      int j = 0;
      for (int idx = 0; idx < v.size(); idx++) {
        num++;
        while (j <= v[idx]) {
          ans[i][j] = num;
          j++;
        }
      }
      while (j < W) {
        ans[i][j] = num;
        j++;
      }
    }
  }

  for (int i = H - 2; i >= 0; i--) {
    if (ans[i][0] == 0) {
      for (int j = 0; j < W; j++) {
        ans[i][j] = ans[i + 1][j];
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << ans[i][j] << " \n"[j + 1 == W];
    }
  }
  return 0;
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char Grid[110][110];
int Ans[110][110], P;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> Grid[i][j];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Grid[i][j] == '#') {
        P++;
        Ans[i][j] = P;
        for (int k = i - 1; k >= 0 && Ans[k][j] == 0 && Grid[k][j] != '#'; k--)
          Ans[k][j] = P;
        for (int k = i + 1; k < H && Ans[k][j] == 0 && Grid[k][j] != '#'; k++)
          Ans[k][j] = P;
      }
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Ans[i][j] == 0) {
        int k;
        for (k = j - 1; k >= 0 && Ans[i][k] == 0; k--)
          ;
        //				cout<<i<<" "<<j<<" "<<k<<endl;
        if (k != -1)
          Ans[i][j] = Ans[i][k];
        else {
          for (k = j + 1; k < W && Ans[i][k] == 0; k++)
            ;
          Ans[i][j] = Ans[i][k];
        }
      }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cout << Ans[i][j] << " ";
    puts("");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char Grid[310][310];
int Ans[310][310], P;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> Grid[i][j];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Grid[i][j] == '#') {
        P++;
        Ans[i][j] = P;
        for (int k = i - 1; k >= 0 && Ans[k][j] == 0 && Grid[k][j] != '#'; k--)
          Ans[k][j] = P;
        for (int k = i + 1; k < H && Ans[k][j] == 0 && Grid[k][j] != '#'; k++)
          Ans[k][j] = P;
      }
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Ans[i][j] == 0) {
        int k;
        for (k = j - 1; k >= 0 && Ans[i][k] == 0; k--)
          ;
        //				cout<<i<<" "<<j<<" "<<k<<endl;
        if (k != -1)
          Ans[i][j] = Ans[i][k];
        else {
          for (k = j + 1; k < W && Ans[i][k] == 0; k++)
            ;
          Ans[i][j] = Ans[i][k];
        }
      }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cout << Ans[i][j] << " ";
    puts("");
  }
  return 0;
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<string> s(H);
  for (int64_t i = 0; i < H; i++) {
    cin >> s[i];
  }

  vector<vector<int64_t>> ans(H, vector<int64_t>(W, -1));
  int64_t index = 1;
  for (int64_t i = 0; i < H; i++) {
    int64_t num = 0;
    for (int64_t j = 0; j < W; j++) {
      if (s[i][j] == '#') {
        num++;
      }
    }

    if (num == 0) {
      continue;
    }

    for (int64_t j = 0; j < W; j++) {
      ans[i][j] = index;
      if (s[i][j] == '#') {
        if (--num > 0) {
          index++;
        }
      }
    }
    index++;
  }

  for (int64_t i = 0; i < H; i++) {
    if (ans[i][0] == -1) {
      (i == 0 ? ans[i] = ans[i + 1] : ans[i] = ans[i - 1]);
    }
  }

  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      cout << ans[i][j] << " \n"[j == W - 1];
      assert(1 <= ans[i][j] && ans[i][j] <= K);
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<string> s(H);
  for (int64_t i = 0; i < H; i++) {
    cin >> s[i];
  }

  vector<vector<int64_t>> ans(H, vector<int64_t>(W, -1));
  int64_t index = 1;
  for (int64_t i = 0; i < H; i++) {
    int64_t num = 0;
    for (int64_t j = 0; j < W; j++) {
      if (s[i][j] == '#') {
        num++;
      }
    }

    if (num == 0) {
      continue;
    }

    for (int64_t j = 0; j < W; j++) {
      ans[i][j] = index;
      if (s[i][j] == '#') {
        if (--num > 0) {
          index++;
        }
      }
    }
    index++;
  }

  for (int64_t i = 0; i < H; i++) {
    if (ans[i][0] == -1) {
      continue;
    }
    for (int64_t ii = i - 1; ii >= 0; ii--) {
      if (ans[ii][0] == -1) {
        ans[ii] = ans[i];
      } else {
        break;
      }
    }
    for (int64_t ii = i + 1; ii < H; ii++) {
      if (ans[ii][0] == -1) {
        ans[ii] = ans[i];
      } else {
        break;
      }
    }
  }

  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      cout << ans[i][j] << " \n"[j == W - 1];
      assert(1 <= ans[i][j] && ans[i][j] <= K);
    }
  }
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

  int h, w, k;
  cin >> h >> w >> k;

  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }

  vector<int> cnt(h);

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        cnt[i]++;
    }
  }

  vector<vector<int>> ans(h);

  int idx = 1;
  bool one = false;

  for (int i = 0; i < h; ++i) {
    if (cnt[i] == 0)
      continue;

    for (int j = 0; j < w; ++j) {
      int k = j;
      while (k < w) {
        if (one && s[i][k] == '#') {
          break;
        } else if (!one && s[i][k] == '#') {
          ans[i].push_back(idx);
          one = true;
        } else {
          ans[i].push_back(idx);
        }

        ++k;
      }

      j = k - 1;
      one = false;
      ++idx;
    }
  }

  for (int i = 0; i < h; ++i) {
    int tmp = i;

    while (ans[tmp].empty() && tmp < h) {
      ++tmp;
    }

    if (tmp == h) {
      while (ans[tmp].empty() && tmp > 0) {
        --tmp;
      }
    }

    for (int j = 0; j < w; ++j) {
      cout << ans[tmp][j] << " ";
    }

    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

  int h, w, k;
  cin >> h >> w >> k;

  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }

  vector<int> cnt(h);

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        cnt[i]++;
    }
  }

  vector<vector<int>> ans(h);

  int idx = 1;
  bool one = false;

  for (int i = 0; i < h; ++i) {
    if (cnt[i] == 0)
      continue;

    for (int j = 0; j < w; ++j) {
      int k = j;
      while (k < w) {
        if (one && s[i][k] == '#') {
          break;
        } else if (!one && s[i][k] == '#') {
          ans[i].push_back(idx);
          one = true;
        } else {
          ans[i].push_back(idx);
        }

        ++k;
      }

      j = k - 1;
      one = false;
      ++idx;
    }
  }

  for (int i = 0; i < h; ++i) {
    int tmp = i;

    while (ans[tmp].empty() && tmp < h) {
      ++tmp;
    }

    if (tmp == h) {
      --tmp;

      while (ans[tmp].empty() && tmp > 0) {
        --tmp;
      }
    }

    for (int j = 0; j < w; ++j) {
      cout << ans[tmp][j] << " ";
    }

    cout << endl;
  }
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  rep(i, 0, H) cin >> S[i];
  vii A(H, vi(W));
  int memo = 1;
  vector<bool> F(H, 1);
  rep(i, 0, H) {
    int count = 0;
    rep(j, 0, W) {
      A[i][j] = memo;
      if (S[i][j] == '#') {
        memo++;
        F[i] = 0;
      }
    }
    rep(j, 1, W) {
      if (A[i][j] == memo && S[i][W - 1] == '.')
        A[i][j] = A[i][j - 1];
    }
  }
  rep(_, 0, H) {
    rep(i, 0, H) {
      rep(j, 0, W) {
        if (F[i]) {
          if (i == 0 || (i > 0 && F[i - 1])) {
            A[i][j] = A[i + 1][j];
          } else
            A[i][j] = A[i - 1][j];
        }
      }
    }
  }
  rep(i, 0, H) {
    rep(j, 0, W) cout << A[i][j] << " ";
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  rep(i, 0, H) cin >> S[i];
  vii A(H, vi(W));
  int memo = 1;
  vector<bool> F(H, 1);
  rep(i, 0, H) {
    int count = 0;
    rep(j, 0, W) {
      A[i][j] = memo;
      if (S[i][j] == '#') {
        memo++;
        F[i] = 0;
      }
    }
    rep(j, 1, W) {
      if (A[i][j] == memo && S[i][W - 1] == '.')
        A[i][j] = A[i][j - 1];
    }
  }
  rep(_, 0, H) {
    rep(i, 0, H) {
      if (F[i]) {
        if (i > 0 && !F[i - 1]) {
          rep(j, 0, W) A[i][j] = A[i - 1][j];
          F[i] = 0;
        } else if (i < H - 1 && !F[i + 1]) {
          rep(j, 0, W) A[i][j] = A[i + 1][j];
          F[i] = 0;
        }
      }
    }
  }
  rep(i, 0, H) {
    rep(j, 0, W) cout << A[i][j] << " ";
    cout << endl;
  }
}
Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sz(x) ll(x.size())
using namespace std;

int main() {
  ll h, w, k, kind = 1;
  char s;
  cin >> h >> w >> k;
  vector<vector<ll>> a(h, vector<ll>(w));
  rep(i, h) rep(j, w) {
    cin >> s;
    if (s == '#') {
      a.at(i).at(j) = kind;
      kind++;
    }
  }
  rep(i, h) {
    ll ind = 0;
    rep(j, w) {
      if (a.at(i).at(j) != 0) {
        for (ll k = ind; k < j; k++)
          a.at(i).at(k) = a.at(i).at(j);
        ind = j + 1;
      } else if (j == w - 1) {
        for (ll k = max((ll)0, ind - 1); k < w; k++)
          a.at(i).at(k) = a.at(i).at(max((ll)0, ind - 1));
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (a.at(i).at(j) == 0 && i != 0)
        a.at(i).at(j) = a.at(i - 1).at(j);
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (a.at(h - 1 - i).at(j) == 0 && h - 1 - i != h - 1)
        a.at(h - 1 - i).at(j) = a.at(h - 2 - i).at(j);
    }
  }
  rep(i, h) {
    rep(j, w) cout << a.at(i).at(j) << " ";
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define sz(x) ll(x.size())
using namespace std;

int main() {
  ll h, w, k, kind = 1;
  char s;
  cin >> h >> w >> k;
  vector<vector<ll>> a(h, vector<ll>(w));
  rep(i, h) rep(j, w) {
    cin >> s;
    if (s == '#') {
      a.at(i).at(j) = kind;
      kind++;
    }
  }
  rep(i, h) {
    ll ind = 0;
    rep(j, w) {
      if (a.at(i).at(j) != 0) {
        for (ll k = ind; k < j; k++)
          a.at(i).at(k) = a.at(i).at(j);
        ind = j + 1;
      } else if (j == w - 1) {
        for (ll k = max((ll)0, ind - 1); k < w; k++)
          a.at(i).at(k) = a.at(i).at(max((ll)0, ind - 1));
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (a.at(i).at(j) == 0 && i != 0)
        a.at(i).at(j) = a.at(i - 1).at(j);
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (a.at(h - 1 - i).at(j) == 0 && h - 1 - i != h - 1)
        a.at(h - 1 - i).at(j) = a.at(h - i).at(j);
    }
  }
  rep(i, h) {
    rep(j, w) cout << a.at(i).at(j) << " ";
    cout << endl;
  }
}