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

#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll, ll>

const int oo = 100000000, N = 1e6 + 10;

int n, cntOpn[N], cntCls[N], idx[N], crrCntOpn, crrCntCls;
string s;

bool cmpCntCls(int i, int j) { return cntCls[i] < cntCls[j]; }

int getCntOpn() {
  int crrCntOpn = 0;
  for (int i = 1, j; i <= n; i++) {
    j = idx[i];
    if (cntOpn[j] >= cntCls[j]) {
      if (crrCntOpn >= cntCls[j])
        crrCntOpn += cntOpn[j] - cntCls[j];
      else {
        return -1;
      }
    }
  }
  return crrCntOpn;
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    cntOpn[i] = 0;
    cntCls[i] = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        cntOpn[i]++;
      else if (cntOpn[i] > 0)
        cntOpn[i]--;
      else
        cntCls[i]++;
    }
    idx[i] = i;
  }
  sort(idx + 1, idx + n + 1, cmpCntCls);
  crrCntOpn = getCntOpn();
  if (crrCntOpn == -1) {
    cout << "No\\n";
    return;
  }
  for (int i = 1, j; i <= n; i++) {
    j = idx[i];
    swap(cntOpn[j], cntCls[j]);
  }
  sort(idx + 1, idx + n + 1, cmpCntCls);
  crrCntCls = getCntOpn();
  if (crrCntCls == -1) {
    cout << "No\\n";
    return;
  }
  if (crrCntOpn == crrCntCls)
    cout << "Yes\\n";
  else
    cout << "No\\n";
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef localProject
  freopen("in.txt", "r", stdin);
#endif
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll, ll>

const int oo = 100000000, N = 1e6 + 10;

int n, cntOpn[N], cntCls[N], idx[N], dff[N], crrCntOpn, dffIdx;
string s;

template <class Ty> class sgmntTr {
private:
  struct node {
    int s;
    int e;
    int rght;
    int lft;
    int prnt;
    int idx;
    Ty v;
  };

private:
  vector<node> tr;

private:
  vector<int> ind;

private:
  static const int rngMin = 0, rngMax = 1, rngSum = 2;

private:
  int code;

private:
  int calc(int i) {
    if (tr[i].s == tr[i].e)
      return tr[i].s;
    int t = calc(tr[i].lft);
    int t2 = calc(tr[i].rght);
    if (code == rngMax) {
      if (dff[t] > dff[t2]) {
        tr[i].idx = t;
        tr[i].v = dff[t];
      } else {
        tr[i].idx = t2;
        tr[i].v = dff[t2];
      }
    }
    return tr[i].v;
  }

public:
  void bldTr(int rngCode, Ty a[], int startIdx, int endIdx) {
    code = rngCode;
    ind.resize(endIdx - startIdx + 10);
    tr.clear();
    node tmp;
    tmp.s = startIdx;
    tmp.e = endIdx;
    tmp.prnt = -1;
    tr.push_back(tmp);
    for (int i = 0, s, e, mid; i < tr.size(); i++) {
      s = tr[i].s;
      e = tr[i].e;
      if (s == e) {
        ind[s] = i;
        tr[i].v = a[s];
        tr[i].idx = s;
        continue;
      }
      mid = (s + e) >> 1;
      tmp.prnt = i;
      tmp.s = s;
      tmp.e = mid;
      tr.push_back(tmp);
      tr[i].lft = tr.size() - 1;
      tmp.s = mid + 1;
      tmp.e = e;
      tr.push_back(tmp);
      tr[i].rght = tr.size() - 1;
    }
    tr[0].v = calc(0);
  }

public:
  void updt(int i, Ty v) {
    int curr = ind[i];
    tr[curr].v = v;
    tr[curr].idx = i;
    curr = tr[curr].prnt;
    while (curr != -1) {
      if (code == rngMax) {
        if (tr[tr[curr].lft].v > tr[tr[curr].rght].v) {
          tr[curr].v = tr[tr[curr].lft].v;
          tr[curr].idx = tr[tr[curr].lft].idx;
        } else {
          tr[curr].v = tr[tr[curr].rght].v;
          tr[curr].idx = tr[tr[curr].rght].idx;
        }
      }
      curr = tr[curr].prnt;
    }
  }

private:
  int qry2(int frst, int lst, int i) {
    if (tr[i].s == tr[i].e)
      return tr[i].idx;
    if (frst == tr[i].s && lst == tr[i].e)
      return tr[i].idx;
    int mid = (tr[i].s + tr[i].e) >> 1;
    if (lst <= mid)
      return qry2(frst, lst, tr[i].lft);
    else if (frst > mid)
      return qry2(frst, lst, tr[i].rght);
    else if (code == rngMax) {
      int qryVlLft = qry2(frst, mid, tr[i].lft);
      int qryVlRght = qry2(mid + 1, lst, tr[i].rght);
      if (dff[qryVlLft] > dff[qryVlRght]) {
        return qryVlLft;
      } else {
        return qryVlRght;
      }
    }
  }

public:
  Ty qry(int frst, int lst) {
    if (frst > lst)
      return 0;
    return qry2(frst, lst, 0);
  }
};

sgmntTr<int> dffSgmntTr;

bool cmp(int i, int j) { return cntCls[i] < cntCls[j]; }

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    cntOpn[i] = 0;
    cntCls[i] = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        cntOpn[i]++;
      else if (cntOpn[i] > 0)
        cntOpn[i]--;
      else
        cntCls[i]++;
    }
    idx[i] = i;
  }
  sort(idx + 1, idx + n + 1, cmp);
  for (int i = 1, j; i <= n; i++) {
    j = idx[i];
    dff[i] = cntOpn[j] - cntCls[j];
  }
  dffSgmntTr.bldTr(1, dff, 1, n);
  crrCntOpn = 0;
  cntCls[0] = 0;
  for (int i = 1, j; i <= n; i++) {
    dffIdx = upper_bound(idx + 1, idx + n + 1, 0, cmp) - idx;
    dffIdx--;
    j = dffSgmntTr.qry(1, dffIdx);
    if (j == 0 || dff[j] == -oo || crrCntOpn + dff[j] < 0) {
      cout << "No\\n";
      return;
    }
    crrCntOpn += dff[j];
    cntCls[0] = crrCntOpn;
    dffSgmntTr.updt(j, -oo);
    dff[j] = -oo;
  }
  if (crrCntOpn == 0)
    cout << "Yes\\n";
  else
    cout << "No\\n";
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef localProject
  freopen("in.txt", "r", stdin);
#endif
  solve();
  return 0;
}