Problem p02686 (C++)

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

using namespace std;

const int M = 1e5;
vector<pair<int, int>> negg, tot;
pair<int, int> seg[4 * M];
int small = -1e9;

pair<int, int> build(int l, int r, int i) {
  if (l == r)
    seg[i] = tot[l];
  else
    seg[i] = max(build(l, (l + r) / 2, i * 2 + 1),
                 build((l + r) / 2 + 1, r, i * 2 + 2));
  return seg[i];
}
pair<int, int> update(int l, int r, int i, int x) {
  if (x < l || x > r)
    return seg[i];
  if (l == r)
    seg[i] = tot[l];
  else
    seg[i] = max(update(l, (l + r) / 2, i * 2 + 1, x),
                 update((l + r) / 2 + 1, r, i * 2 + 2, x));
  return seg[i];
}
pair<int, int> get(int l, int r, int L, int R, int i) {
  if (R < l || L > r)
    return make_pair(small, small);
  if (L >= l && R <= r)
    return seg[i];
  return max(get(l, r, L, (L + R) / 2, i * 2 + 1),
             get(l, r, (L + R) / 2 + 1, R, i * 2 + 2));
}

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

  int n, pos = 0, neg = 0;
  cin >> n;

  while (n--) {
    string s;
    cin >> s;
    stack<char> st;
    int curPos = 0, curNeg = 0;

    for (char c : s)
      if (!st.empty() && st.top() == '(' && c == ')')
        st.pop();
      else
        st.push(c);

    while (!st.empty())
      st.top() == '(' ? curPos++ : curNeg--, st.pop();
    if (curPos && curNeg)
      negg.push_back(make_pair(-curNeg, curPos + curNeg));
    else
      pos += curPos, neg += curNeg;
  }

  if (!negg.empty()) {
    sort(negg.begin(), negg.end());
    for (int i = 0; i < negg.size(); i++)
      tot.push_back(make_pair(negg[i].second, i));
    build(0, tot.size() - 1, 0);

    for (int i = 0; i < negg.size(); i++) {
      int last = upper_bound(negg.begin(), negg.end(), make_pair(pos, -small)) -
                 negg.begin() - 1;
      if (last == -1)
        return cout << "No\n", 0;

      pair<int, int> cur = get(0, last, 0, tot.size() - 1, 0);
      pos += cur.first;
      tot[cur.second] = make_pair(small, cur.second);
      update(0, tot.size() - 1, 0, cur.second);
    }
  }

  cout << (pos + neg == 0 ? "Yes\n" : "No\n");

  return 0;
}

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

using namespace std;

const int M = 1e6;
vector<pair<int, int>> negg, tot;
pair<int, int> seg[4 * M];
int small = -1e9;

pair<int, int> build(int l, int r, int i) {
  if (l == r)
    seg[i] = tot[l];
  else
    seg[i] = max(build(l, (l + r) / 2, i * 2 + 1),
                 build((l + r) / 2 + 1, r, i * 2 + 2));
  return seg[i];
}
pair<int, int> update(int l, int r, int i, int x) {
  if (x < l || x > r)
    return seg[i];
  if (l == r)
    seg[i] = tot[l];
  else
    seg[i] = max(update(l, (l + r) / 2, i * 2 + 1, x),
                 update((l + r) / 2 + 1, r, i * 2 + 2, x));
  return seg[i];
}
pair<int, int> get(int l, int r, int L, int R, int i) {
  if (R < l || L > r)
    return make_pair(small, small);
  if (L >= l && R <= r)
    return seg[i];
  return max(get(l, r, L, (L + R) / 2, i * 2 + 1),
             get(l, r, (L + R) / 2 + 1, R, i * 2 + 2));
}

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

  int n, pos = 0, neg = 0;
  cin >> n;

  while (n--) {
    string s;
    cin >> s;
    stack<char> st;
    int curPos = 0, curNeg = 0;

    for (char c : s)
      if (!st.empty() && st.top() == '(' && c == ')')
        st.pop();
      else
        st.push(c);

    while (!st.empty())
      st.top() == '(' ? curPos++ : curNeg--, st.pop();
    if (curPos && curNeg)
      negg.push_back(make_pair(-curNeg, curPos + curNeg));
    else
      pos += curPos, neg += curNeg;
  }

  if (!negg.empty()) {
    sort(negg.begin(), negg.end());
    for (int i = 0; i < negg.size(); i++)
      tot.push_back(make_pair(negg[i].second, i));
    build(0, tot.size() - 1, 0);

    for (int i = 0; i < negg.size(); i++) {
      int last = upper_bound(negg.begin(), negg.end(), make_pair(pos, -small)) -
                 negg.begin() - 1;
      if (last == -1)
        return cout << "No\n", 0;

      pair<int, int> cur = get(0, last, 0, tot.size() - 1, 0);
      pos += cur.first;
      tot[cur.second] = make_pair(small, cur.second);
      update(0, tot.size() - 1, 0, cur.second);
    }
  }

  cout << (pos + neg == 0 ? "Yes\n" : "No\n");

  return 0;
}