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

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

using ll = long long;

bool check(vector<pair<int, int>> a) {
  int cur = 0;
  for (auto i : a) {
    if (cur + i.first < 0)
      return 0;
    cur += i.second;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, total = 0;
  cin >> n;
  vector<pair<int, int>> ls, rs;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int mn = 0, cur = 0;
    for (char &i : s) {
      cur += (i == '(' ? 1 : -1);
      mn = min(mn, cur);
    }
    total += cur;
    if (cur > 0) {
      ls.emplace_back(mn, cur);
    } else {
      rs.emplace_back(mn - cur, -cur);
    }
  }
  sort(ls.rbegin(), ls.rend());
  sort(rs.rbegin(), rs.rend());
  if (!total && check(ls) && check(rs)) {
    cout << "Yes";
  } else {
    cout << "No";
  }
  return 0;
}


Error: WA

Fixed submission:

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

using ll = long long;
const int Bad = -1e8;

struct segtree {

  vector<pair<int, int>> tree;
  int size;

  void init(int n) {
    size = 1;
    while (size < n) {
      size <<= 1;
    }
    tree.assign(size << 1, {Bad, 0});
  }

  int son(int v, int num) { return (v << 1) + (num - 1); }

  pair<int, int> combine(pair<int, int> x, pair<int, int> y) {
    if (x.first >= y.first)
      return x;
    return y;
  }

  void build(int v, int lv, int rv, vector<pair<int, int>> &a) {
    if (lv == rv) {
      if (lv <= (int)a.size()) {
        tree[v] = {a[lv].second, lv};
      } else {
        tree[v] = {Bad, lv};
      }
      return;
    }
    int mid = (lv + rv) >> 1;
    build(son(v, 1), lv, mid, a);
    build(son(v, 2), mid + 1, rv, a);
    tree[v] = combine(tree[son(v, 1)], tree[son(v, 2)]);
  }

  void build(vector<pair<int, int>> &a) { build(1, 1, size, a); }

  void set(int v, int lv, int rv, int i) {
    if (lv == rv) {
      tree[v] = {Bad, lv};
      return;
    }
    int mid = (lv + rv) >> 1;
    if (mid >= i) {
      set(son(v, 1), lv, mid, i);
    } else {
      set(son(v, 2), mid + 1, rv, i);
    }
    tree[v] = combine(tree[son(v, 1)], tree[son(v, 2)]);
  }

  void set(int i) { set(1, 1, size, i); }

  pair<int, int> get(int v, int lv, int rv, int l, int r) {
    if (l > r || lv > rv || lv > r || rv < l) {
      return {Bad, 0};
    }
    if (lv >= l && rv <= r) {
      return tree[v];
    }
    int mid = (lv + rv) >> 1;
    pair<int, int> res1 = get(son(v, 1), lv, mid, l, min(r, mid));
    pair<int, int> res2 = get(son(v, 2), mid + 1, rv, max(mid + 1, l), r);
    return combine(res1, res2);
  }

  pair<int, int> get(int l, int r) { return get(1, 1, size, l, r); }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<string> s(n + 1);
  vector<pair<int, int>> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  segtree t;
  t.init(n);
  for (int i = 1; i <= n; i++) {
    int x = 0, mn = 0;
    for (int j = 0; j < (int)s[i].size(); j++) {
      x += (s[i][j] == '(' ? 1 : -1);
      mn = min(mn, x);
    }
    a[i] = {mn, x};
  }
  a[0] = {Bad, 0};
  sort(a.begin(), a.end());
  int cur = 0;
  for (int i = n; i >= 1; i--) {
    if (a[i].first < 0 && cur < abs(a[i].first)) {
      cout << "No";
      return 1;
    }
    cur += a[i].second;
  }
  if (cur) {
    cout << "No";
  } else {
    cout << "Yes";
  }
  return 0;
}