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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

bool check(const vector<pair<int, int>> &v) {
  int cur = 0;
  for (auto p : v) {
    if (cur + p.first < 0) {
      return false;
    }
    cur += p.second;
  }
  return true;
}

void solve() {
  int n;
  cin >> n;

  int total = 0;
  vector<pair<int, int>> up, down;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    int cur = 0;
    int mini = INF;
    for (char c : s) {
      if (c == '(')
        ++cur;
      else
        --cur;
      chmin(mini, cur);
    }
    if (cur >= 0) {
      up.push_back({mini, cur});
    } else {
      down.push_back({mini - cur, -cur});
    }
    total += cur;
  }
  sort(up.rbegin(), up.rend());
  sort(down.rbegin(), down.rend());
  if (check(up) && check(down) && total == 0)
    cout << "Yes" << '\\n';
  else
    cout << "No" << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

template <class T> class SegmentTree {
  using Func = std::function<T(T, T)>;

private:
  const Func F;
  const T DEF;
  int N;
  std::vector<T> dat;

public:
  SegmentTree(int n, const Func f, const T def) : F(f), DEF(def) {
    N = 1;
    while (N < n)
      N *= 2;
    dat.assign(2 * N, DEF);
  }
  // a is 0-indexed
  void set(int a, const T v) { dat.at(a + N) = v; }
  void build() {
    for (int i = N - 1; i > 0; --i)
      dat.at(i) = F(dat.at(2 * i), dat.at(2 * i + 1));
  }
  // a is 0-indexed
  void update(int a, const T v) {
    int cur = a + N;
    dat.at(cur) = v;
    while ((cur /= 2) > 0)
      dat.at(cur) = F(dat.at(2 * cur), dat.at(2 * cur + 1));
  }
  // return value of [a, b)
  // a and b are 0-indexed
  T get(int a, int b) { return get(a, b, 1, 0, N); }
  T get(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return DEF;
    if (a <= l && r <= b)
      return dat.at(k);
    T vleft = get(a, b, 2 * k, l, (l + r) / 2);
    T vright = get(a, b, 2 * k + 1, (l + r) / 2, r);
    return F(vleft, vright);
  }
  // a is 0-indexed
  T at(int a) { return dat.at(a + N); }
};

void solve() {
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    cin >> s.at(i);
  }

  vector<pair<int, int>> tmp(n);
  for (int i = 0; i < n; ++i) {
    int mini = INF;
    int cur = 0;
    for (char c : s.at(i)) {
      if (c == '(')
        ++cur;
      else
        --cur;
      chmin(mini, cur);
    }
    tmp.at(i).first = mini;
    tmp.at(i).second = cur;
  }
  sort(tmp.begin(), tmp.end());

  vector<int> cnt(n), dif(n);
  for (int i = 0; i < n; ++i) {
    cnt.at(i) = tmp.at(i).first;
    dif.at(i) = tmp.at(i).second;
  }

  SegmentTree<pair<int, int>> st(n,
                                 [](pair<int, int> a, pair<int, int> b) {
                                   if (a.first != b.first)
                                     return max(a, b);
                                   else if (a.second < b.second)
                                     return a;
                                   else
                                     return b;
                                 },
                                 {-INF, INF});
  for (int i = 0; i < n; ++i)
    st.set(i, {dif.at(i), i});
  st.build();

  int cur = 0;
  for (int i = 0; i < n; ++i) {
    int pla = lower_bound(cnt.begin(), cnt.end(), -cur) - cnt.begin();
    if (pla == n) {
      cout << "No" << '\\n';
      return;
    }
    auto p = st.get(pla, n);
    if (p.first == -INF) {
      assert(false);
      cout << "No" << '\\n';
      return;
    }
    cur += p.first;
    st.update(p.second, {-INF, p.second});
  }
  if (cur == 0)
    cout << "Yes" << '\\n';
  else
    cout << "No" << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}