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

#include <bits/stdc++.h>

#define ll long long
#define rep(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vv vector

#define fi first
#define se second
#define all(A) A.begin(), A.end()

void mxin(ll &a, ll b) {
  if (a < b)
    a = b;
}

using namespace std;

/////////////////////////////////////////////////////

ll N;
ll i, j, k;

ll mk[256];

ll ati(char c) { return c - 'a'; }

void calc(ll I, ll l, ll r, vv<string> &S, vv<vv<ll>> &smk) {
  // printf("%lld,%lld,%lld,%lld\\n", l, r, I, mk['c' - 'a']);
  rep(l, l, r + 1) {
    if (I != S[l].size())
      break;
  }
  if (l > r)
    return;

  ll i, j = l, k, ret = 0;
  rep(i, l, r + 2) {
    if (i <= r && I < S[i].size() && S[j][I] == S[i][I]) {
      // printf("b%lld,%lld,%c,%lld\\n", i,I, S[i][I], mk[S[i][I] - 'a']);

    } else {
      rep(k, j, i) {
        if (I + 2 == S[k].size()) {
          // printf("a%lld,%c\\n", I,k, S[k][I + 1]);
          ++mk[ati(S[k][I + 1])];
        }
      }
      calc(I + 1, j, i - 1, S, smk);
      rep(k, j, i) {
        if (I + 2 == S[k].size()) {
          --mk[ati(S[k][I + 1])];
        }
      }
      j = i;
    }
    if (i <= r && I < S[i].size())
      mxin(smk[i][ati(S[i][I])], mk[ati(S[i][I])]);
  }
}

int main() {
  cin >> N;
  vv<string> S(N);
  rep(i, 0, N) {
    cin >> S[i];
    // cout << S[i]<<endl;
    reverse(all(S[i]));
  }
  sort(all(S));
  S.push_back("");
  vv<vv<ll>> smk(N + 5, vv<ll>(30));
  rep(i, 0, N) {
    // cout << S[i] <<S[i].size()<< endl;
    if (S[i].size() == 1) {
      ++mk[ati(S[i][0])];
    }
  }
  calc(0, 0, N - 1, S, smk);
  ll ans = -N;
  j = 0;
  rep(i, 0, N + 1) {
    ll a = 0;
    rep(k, 0, 27) {
      a += smk[i][k];
      // printf("%lld,%lld:%lld\\n", i, j, smk[i][j]);
    }
    // printf("%lld,%lld ", i, a);
    ans += a;

    // printf("%lld,%lld\\n", i, ans);
    if (S[i] != S[j]) {
      // printf(" %lld ", i - j);
      ans -= (i - j) * (i - j - 1) / 2;
      j = i;
    }
    // printf("%lld,%lld\\n", i, ans);
  }
  cout << ans << endl;
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define rep(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vv vector

#define fi first
#define se second
#define all(A) A.begin(), A.end()

void mxin(ll &a, ll b) {
  if (a < b)
    a = b;
}

using namespace std;

/////////////////////////////////////////////////////

ll N;
ll i, j, k;

ll mk[256];

void calc(ll I, ll l, ll r, vv<string> &S, vv<vv<ll>> &smk) {
  // printf("%lld,%lld,%lld,%lld\\n", l, r, I, mk['c' - 'a']);
  rep(l, l, r + 1) {
    if (I != S[l].size())
      break;
  }
  if (l > r)
    return;

  ll i, j = l, k, ret = 0;
  rep(i, l, r + 2) {
    if (i <= r && I < S[i].size() && S[j][I] == S[i][I]) {
      // printf("b%lld,%lld,%c,%lld\\n", i,I, S[i][I], mk[S[i][I] - 'a']);

    } else {
      rep(k, j, i) {
        if (I + 2 == S[k].size()) {
          // printf("a%lld,%c\\n", I,k, S[k][I + 1]);
          ++mk[S[k][I + 1] - 'a'];
        }
      }
      calc(I + 1, j, i - 1, S, smk);
      rep(k, j, i) {
        if (I + 2 == S[k].size()) {
          --mk[S[k][I + 1] - 'a'];
        }
      }
      j = i;
    }
    mxin(smk[i][S[i][I] - 'a'], mk[S[i][I] - 'a']);
  }
}

int main() {
  cin >> N;
  vv<string> S(N);
  rep(i, 0, N) {
    cin >> S[i];
    // cout << S[i]<<endl;
    reverse(all(S[i]));
  }
  sort(all(S));
  S.push_back("");
  vv<vv<ll>> smk(N + 5, vv<ll>(30));
  rep(i, 0, N) {
    // cout << S[i] <<S[i].size()<< endl;
    if (S[i].size() == 1) {
      ++mk[S[i][0] - 'a'];
    }
  }
  calc(0, 0, N - 1, S, smk);
  ll ans = -N;
  j = 0;
  rep(i, 0, N + 1) {
    ll a = 0;
    rep(k, 0, 27) {
      a += smk[i][k];
      // printf("%lld,%lld:%lld\\n", i, j, smk[i][j]);
    }
    // printf("%lld,%lld ", i, a);
    ans += a;

    // printf("%lld,%lld\\n", i, ans);
    if (S[i] != S[j]) {
      // printf(" %lld ", i - j);
      ans -= (i - j) * (i - j - 1) / 2;
      j = i;
    }
    // printf("%lld,%lld\\n", i, ans);
  }
  cout << ans << endl;
  return 0;
}