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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void solve() {
  int n;
  cin >> n;
  VS s(n);
  REP(i, 0, n) cin >> s[i];
  int bal = 0;
  REP(i, 0, n) for (char c : s[i]) bal += (c == '(' ? 1 : -1);
  if (bal != 0) {
    cout << "No" << endl;
    return;
  }
  VI needs(n, 1000000 + 1), b(n);
  REP(i, 0, n) {
    int bal = 0;
    for (char c : s[i]) {
      bal += (c == '(' ? 1 : -1);
      chmin(needs[i], bal);
    }
    b[i] = bal;
  }

  priority_queue<PII, VP, less<PII>> l, r;
  REP(i, 0, n) {
    if (b[i] >= 0)
      l.emplace(needs[i], b[i]);
    else
      r.emplace(needs[i] - b[i], -b[i]);
  }

  int h = 0;
  while (!l.empty()) {
    auto p = l.top();
    l.pop();
    if (h + p.first < 0) {
      cout << "No" << endl;
      return;
    }
    h += p.second;
  }
  h = 0;
  while (!r.empty()) {
    auto p = r.top();
    r.pop();
    if (h + p.first < 0) {
      cout << "No" << endl;
      return;
    }
    h += p.second;
  }
  cout << "Yes" << endl;
}

signed main() {
  solve();

  return 0;
}


Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T = PII> class SegmentTree {
public:
  std::vector<T> ary;
  int n;
  SegmentTree(int n) {
    this->n = 1;
    while (this->n < n)
      this->n *= 2;
    ary.resize(this->n * 2 - 1,
               MP(1 << 30, -(1 << 30))); // sum -> 0, min -> INF, 0?
  }
  T ope(T l, T r) {
    if (l.second != r.second) {
      if (l.second < r.second)
        return r;
      else
        return l;
    } else {
      if (l.first < r.first)
        return r;
      else
        return l;
    }
  }
  void update(int k, T d) {
    k += n - 1;
    ary[k] = d;
    while (k > 0) {
      k = (k - 1) / 2;
      ary[k] = ope(ary[2 * k + 1], ary[2 * k + 2]);
    }
  }
  T query(int a, int b) { return query(a, b, 0, n, 0); }
  T query(int a, int b, int l, int r, int i) {
    // cerr << a << ", " << b << ": " << l << ", " << r << endl;

    if (a <= l && b >= r)
      return ary[i];
    // if (a >= r || b <= l) return 0; // sum
    if (a >= r || b <= l)
      return MP(1 << 30, -(1 << 30)); // min
    int m = (l + r) / 2;
    T vl = query(a, b, l, m, 2 * i + 1);
    T vr = query(a, b, m, r, 2 * i + 2);
    return ope(vl, vr);
  }
};

void solve() {
  int n;
  cin >> n;
  VS s(n);
  REP(i, 0, n) cin >> s[i];
  int bal = 0;
  REP(i, 0, n) for (char c : s[i]) bal += (c == '(' ? 1 : -1);
  if (bal != 0) {
    cout << "No" << endl;
    return;
  }
  VI needs(n, 1000000 + 1), b(n);
  REP(i, 0, n) {
    int bal = 0;
    for (char c : s[i]) {
      bal += (c == '(' ? 1 : -1);
      chmin(needs[i], bal);
    }
    b[i] = bal;
    needs[i] *= -1;
    // cerr << b[i] << ", " << needs[i] << endl;
  }

  int fin = -1, finbal;
  REP(i, 0, n) {
    if (s[i][s[i].size() - 1] != ')')
      continue;
    if (-needs[i] != b[i])
      continue;
    if (fin == -1) {
      fin = i;
      finbal = b[i];
    } else if (finbal > b[i]) {
      fin = i;
      finbal = b[i];
    }
  }
  dump(fin);
  if (fin == -1) {
    cout << "No" << endl;
    return;
  }

  priority_queue<PII, VP, less<PII>> q;
  VP ps;
  REP(i, 0, n) if (i != fin) ps.pb(needs[i], b[i]);
  sort(all(ps), [](auto &l, auto &r) { return l.first < r.first; });

  {
    SegmentTree<> seg(n - 1);
    REP(i, 0, n - 1) {
      seg.update(i, ps[i]);
      // cerr << ps[i].first << ", " << ps[i].second << endl;
    }
    int bal = 0;
    REP(i, 0, n - 1) {
      auto it = upper_bound(all(ps), MP(bal, 1 << 30));
      int d = distance(ps.begin(), it) - 1;
      if (d < 0) {
        cout << "No" << endl;
        // dump(i);
        // dump("d<0");
        return;
      }
      auto ret = seg.query(0, d + 1);
      if (ret.second == -(1 << 30)) {
        cout << "No" << endl;
        // dump(i);
        // cerr << ret.first << ", " << ret.second << endl;
        // dump(d);
        return;
      }

      it = lower_bound(all(ps), ret);
      d = distance(ps.begin(), it) - 1;
      bal += ret.second;
      ps[d].second = -(1 << 30);
      seg.update(d, ps[d]);
    }
    cout << "Yes" << endl;
  }
}

signed main() {
  solve();

  return 0;
}