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

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("avx")
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template <class T> T abs(T x) { re x > 0 ? x : -x; }
template <class T> T gcd(T a, T b) { re a ? gcd(b % a, a) : b; }
template <class T> T sqr(T a) { re a *a; }
template <class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

const double pi = acos(-1.0);

#define filename ""

int n;

const int M = 1e6 + 10;
set<ii, greater<ii>> w[M];
ii t[4 * M];

ii upd(int x, int lc, int rc, int pos) {
  if (pos < lc || pos > rc)
    return t[x];
  if (lc == rc) {
    if (sz(w[pos])) {
      int cur = w[pos].begin()->fi;
      return t[x] = mp(cur, pos);
    } else {
      return t[x] = mp((int)-1e9, -1);
    }
  }
  int mc = (lc + rc) / 2;
  t[x] = max(upd(2 * x + 1, lc, mc, pos), upd(2 * x + 2, mc + 1, rc, pos));
  return t[x];
}

ii get(int x, int lc, int rc, int r) {
  if (lc > r)
    return mp(-1e9, -1);
  if (rc <= r)
    return t[x];
  int mc = (lc + rc) / 2;
  return max(get(2 * x + 1, lc, mc, r), get(2 * x + 2, mc + 1, rc, r));
}

int main() {
  for (auto &x : t)
    x = mp(-1e9, -1);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int bal = 0, m = 0;
    for (char c : s) {
      bal += c == '(';
      bal -= c == ')';
      m = min(bal, m);
    }
    m = abs(m);
    w[m].insert(mp(bal == -m ? -M : bal, i));
    upd(0, 0, M, m);
  }
  int bal = 0;
  for (int i = 0; i < n; i++) {
    ii cur = get(0, 0, M, bal);
    if (cur.fi == -M)
      cur.fi = -cur.se;
    bal += cur.fi;

    if (bal < 0) {
      cout << "No" << endl;
      re 0;
    }
    w[cur.se].erase(w[cur.se].begin());
    upd(0, 0, M, cur.se);
  }
  if (bal == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  //    cerr << clock () << endl;
  return 0;
}


Error: -6
7b9af40f-1ccc-469f-bae3-e188b6e8f528.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02686/C++/s501380569.cpp:95: int main(): Assertion `false' failed.


Fixed submission:

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("avx")
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template <class T> T abs(T x) { re x > 0 ? x : -x; }
template <class T> T gcd(T a, T b) { re a ? gcd(b % a, a) : b; }
template <class T> T sqr(T a) { re a *a; }
template <class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

const double pi = acos(-1.0);

#define filename ""

int n;

const int M = 1e6 + 10;
set<ii, greater<ii>> w[M];
ii t[4 * M];

ii upd(int x, int lc, int rc, int pos) {
  if (pos < lc || pos > rc)
    return t[x];
  if (lc == rc) {
    return t[x] = sz(w[pos]) ? mp(w[pos].begin()->fi, pos) : mp((int)-1e9, -1);
  }
  int mc = (lc + rc) / 2;
  t[x] = max(upd(2 * x + 1, lc, mc, pos), upd(2 * x + 2, mc + 1, rc, pos));
  return t[x];
}

ii get(int x, int lc, int rc, int r) {
  if (lc > r)
    return mp(-1e9, -1);
  if (rc <= r)
    return t[x];
  int mc = (lc + rc) / 2;
  return max(get(2 * x + 1, lc, mc, r), get(2 * x + 2, mc + 1, rc, r));
}

int main() {
  for (auto &x : t)
    x = mp(-1e9, -1);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int bal = 0, m = 0;
    for (char c : s) {
      bal += c == '(';
      bal -= c == ')';
      m = min(bal, m);
    }
    m = abs(m);
    w[m].insert(mp(bal, i));
    upd(0, 0, M, m);
  }
  int bal = 0;
  for (int i = 0; i < n; i++) {
    ii cur = get(0, 0, M, bal);
    bal += cur.fi;
    if (bal < 0) {
      cout << "No" << endl;
      re 0;
    }
    w[cur.se].erase(w[cur.se].begin());
    upd(0, 0, M, cur.se);
  }
  if (bal == 0) {
    cout << "Yes" << endl;
    assert(false);
  } else {
    cout << "No" << endl;
  }

  //    cerr << clock () << endl;
  return 0;
}