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

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi = acos(-1);
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define reset(x, v) memset(x, v, sizeof(x))

#define fi first
#define se second
#define endl "\\n"
#define debug(x) (cerr << #x << ": " << x << "\\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p % m * p % m) % m;
  if (n % 2)
    return (p % m * a % m) % m;
  else
    return p;
}
const double EPS = 1e-9;
const ll MOD = 998244353;
const ll hell = MOD;
const int INF = 1e9;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

const int N = 1000005;
pair<int, int> vl[N];
int st[4 * N];

void constructtree(int i, int l, int r) {
  if (l == r) {
    st[i] = l;
    return;
  }
  int m = (l + r) / 2;
  constructtree(2 * i + 1, l, m);
  constructtree(2 * i + 2, m + 1, r);
  st[i] = (vl[st[2 * i + 1]].se > vl[st[2 * i + 2]].se) ? st[2 * i + 1]
                                                        : st[2 * i + 2];
}
void update(int i, int l, int r, int in, int x) {
  if (in > r || in < l)
    return;
  if (l == r) {
    vl[l].se = x;
    return;
  }
  int m = (l + r) / 2;
  update(2 * i + 1, l, m, in, x);
  update(2 * i + 2, m + 1, r, in, x);
  st[i] = (vl[st[2 * i + 1]].se > vl[st[2 * i + 2]].se) ? st[2 * i + 1]
                                                        : st[2 * i + 2];
}
int query(int i, int l, int r, int qs, int qe) {
  if (qs > r || qe < l)
    return N - 1;
  if (qs <= l && qe >= r)
    return st[i];
  int m = (l + r) / 2;
  int i1 = query(2 * i + 1, l, m, qs, qe);
  int i2 = query(2 * i + 2, m + 1, r, qs, qe);
  return (vl[i1].se > vl[i2].se) ? i1 : i2;
}

void solve() {
  int n;
  cin >> n;
  string s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];
  // pair<int,int> vl[n];
  for (int i = 0; i < n; i++) {
    vl[i].fi = 0;
    vl[i].se = 0;
  }
  for (int i = 0; i < n; i++) {
    stack<int> st;
    for (int j = 0; j < s[i].length(); j++) {
      if (s[i][j] == '(')
        st.push(1);
      else {
        if (st.empty()) {
          vl[i].fi += 1;

        } else {
          st.pop();
        }
      }
    }
    while (!st.empty()) {
      vl[i].se += 1;
      st.pop();
    }
  }
  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) {
    s1 += vl[i].fi;
    s2 += vl[i].se;
  }
  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }
  int op = 0;
  /*
  for(int i=0;i<n;i++)
  {
      vl[i].se=vl[i].se;
  }
   */
  for (int i = n; i < N; i++) {
    vl[i].fi = INF;
    vl[i].se = -INF;
  }
  sort(vl, vl + n);
  vi ve(n);
  // for(int i=0;i<N;i++)
  //   ve.pb(INF);
  for (int i = 0; i < n; i++)
    ve[i] = vl[i].fi;
  constructtree(0, 0, n - 1);
  for (int i = 0; i < n; i++) {
    int in = upper_bound(ve.begin(), ve.end(), op) - ve.begin();
    in--;
    if (in < 0) {
      cout << "No" << endl;
      return;
    }
    int j = query(0, 0, n - 1, 0, in);
    op += vl[j].se - vl[j].fi;
    if (op < 0) {
      cout << "No" << endl;
      return;
    }
    update(0, 0, n - 1, j, -INF);
  }
  if (op != 0) {
    cout << "No" << endl;
    return;
  }
  cout << "Yes" << endl;
}

int32_t main() {
  IOS;
  // pre();
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi = acos(-1);
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int>> matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define reset(x, v) memset(x, v, sizeof(x))

#define fi first
#define se second
#define endl "\\n"
#define debug(x) (cerr << #x << ": " << x << "\\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p % m * p % m) % m;
  if (n % 2)
    return (p % m * a % m) % m;
  else
    return p;
}
const double EPS = 1e-9;
const ll MOD = 998244353;
const ll hell = MOD;
const int INF = 1e17;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

const int N = 200005;
pair<int, int> vl[N];
int st[4 * N];

void constructtree(int i, int l, int r) {
  if (l == r) {
    st[i] = l;
    return;
  }
  int m = (l + r) / 2;
  constructtree(2 * i + 1, l, m);
  constructtree(2 * i + 2, m + 1, r);
  st[i] = (vl[st[2 * i + 1]].se > vl[st[2 * i + 2]].se) ? st[2 * i + 1]
                                                        : st[2 * i + 2];
}
void update(int i, int l, int r, int in, int x) {
  if (in > r || in < l)
    return;
  if (l == r) {
    vl[l].se = x;
    return;
  }
  int m = (l + r) / 2;
  update(2 * i + 1, l, m, in, x);
  update(2 * i + 2, m + 1, r, in, x);
  st[i] = (vl[st[2 * i + 1]].se > vl[st[2 * i + 2]].se) ? st[2 * i + 1]
                                                        : st[2 * i + 2];
}
int query(int i, int l, int r, int qs, int qe) {
  if (qs > r || qe < l)
    return N - 1;
  if (qs <= l && qe >= r)
    return st[i];
  int m = (l + r) / 2;
  int i1 = query(2 * i + 1, l, m, qs, qe);
  int i2 = query(2 * i + 2, m + 1, r, qs, qe);
  return (vl[i1].se > vl[i2].se) ? i1 : i2;
}

void solve() {
  int n;
  cin >> n;
  string s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];
  // pair<int,int> vl[n];
  for (int i = 0; i < n; i++) {
    vl[i].fi = 0;
    vl[i].se = 0;
  }
  for (int i = 0; i < n; i++) {
    stack<int> st;
    for (int j = 0; j < s[i].length(); j++) {
      if (s[i][j] == '(')
        st.push(1);
      else {
        if (st.empty()) {
          vl[i].fi += 1;

        } else {
          st.pop();
        }
      }
    }
    while (!st.empty()) {
      vl[i].se += 1;
      st.pop();
    }
  }
  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) {
    s1 += vl[i].fi;
    s2 += vl[i].se;
  }
  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }
  int op = 0;
  for (int i = 0; i < n; i++) {
    vl[i].se = vl[i].se - vl[i].fi;
  }
  for (int i = n; i < N; i++) {
    vl[i].fi = INF;
    vl[i].se = -INF;
  }
  sort(vl, vl + n);
  vi ve(n);
  // for(int i=0;i<N;i++)
  //   ve.pb(INF);
  for (int i = 0; i < n; i++)
    ve[i] = vl[i].fi;
  constructtree(0, 0, n - 1);
  for (int i = 0; i < n; i++) {
    int in = upper_bound(ve.begin(), ve.end(), op) - ve.begin();
    in--;
    if (in < 0) {
      cout << "No" << endl;
      return;
    }
    int j = query(0, 0, n - 1, 0, in);
    op += vl[j].se;
    if (op < 0) {
      cout << "No" << endl;
      return;
    }
    update(0, 0, n - 1, j, -INF);
  }
  if (op != 0) {
    cout << "No" << endl;
    return;
  }
  cout << "Yes" << endl;
}

int32_t main() {
  IOS;
  // pre();
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}