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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\\n')
#define cps CLOCKS_PER_SEC
#define tests(t)                                                               \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 1000000007
#define maxn 200005

using namespace std;

using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// All indexing is 0-based
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<ll, ll> par;

int dif, N;
char cad[maxn];

struct dos {
  int men, bal;
  bool operator<(const dos &h) const { return men > h.men; }
};
vector<dos> a, b;

bool cumpl(vector<dos> &v) {
  sort(all(v));

  int sum = 0;
  for (auto a : v) {
    if (sum + a.men < 0)
      return 0;
    sum += a.bal;
  }
  return 1;
}

int main() {
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> cad;
    int l = strlen(cad), men = 0, c = 0;
    for (int j = 0; j < l; j++) {
      if (cad[j] == '(')
        c++, dif++;
      else
        c--, dif--;
      men = min(men, c);
    }
    if (c >= 0)
      a.pb({men, c});
    else {
      men = c = 0;
      for (int j = l - 1; j >= 0; j--) {
        if (cad[j] == ')')
          c++;
        else
          c--;
        men = min(men, c);
      }
      b.pb({men, c});
    }
  }
  if (dif) {
    cout << "No\\n";
    return 0;
  }

  if (cumpl(a) && cumpl(b))
    cout << "Yes\\n";
  else
    cout << "No\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\\n')
#define cps CLOCKS_PER_SEC
#define tests(t)                                                               \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 998244353
#define maxn 200005

using namespace std;

using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// All indexing is 0-based
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<ll, ll> par;

string s1;
ll N, a, b, cont, B[maxn];

struct dos {
  ll a, b;
  bool operator<(const dos &h) const {
    if (a == h.a)
      return b < h.b;
    return a < h.a;
  }
} A[maxn];

struct dos1 {
  ll b, id;
  bool operator<(const dos1 &h) const {
    if (b == h.b)
      return id < h.id;
    return b < h.b;
  }
};

struct t {};

multiset<dos1> T[maxn];

void built(int nod, int ini, int fin) {
  if (ini == fin) {
    T[nod].insert({A[ini].b, ini});
    return;
  }

  int mid = (ini + fin) / 2;
  built(2 * nod, ini, mid);
  built(2 * nod + 1, mid + 1, fin);

  for (auto a : T[2 * nod])
    T[nod].insert(a);
  for (auto a : T[2 * nod + 1])
    T[nod].insert(a);
}

int query(int nod, int ini, int fin, int a, int b) {
  if (fin < a || ini > b)
    return -1;
  if (a <= ini && fin <= b) {
    if (T[nod].empty())
      return -1;
    return prev(T[nod].end())->id;
  }

  int mid = (ini + fin) / 2;

  int p1 = query(2 * nod, ini, mid, a, b);
  int p2 = query(2 * nod + 1, mid + 1, fin, a, b);

  if (p1 == -1 && p2 == -1)
    return -1;
  if (p1 == -1 || p2 == -1)
    return p1 + p2 + 1;
  if (A[p1].b > A[p2].b)
    return p1;
  return p2;
}

void update(int nod, int ini, int fin, int pos) {
  if (fin < pos || ini > pos)
    return;
  T[nod].erase(T[nod].find({A[pos].b, pos}));
  if (ini == fin && pos == ini) {
    return;
  }

  int mid = (ini + fin) / 2;
  update(2 * nod, ini, mid, pos);
  update(2 * nod + 1, mid + 1, fin, pos);
}
int main() {
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> s1;
    a = 1 << 30;
    for (auto h : s1) {
      if (h == '(')
        cont++;
      else
        cont--;

      a = min(cont, a);
    }
    b = cont;
    A[i] = {a, b};
    cont = 0;
  }

  sort(A + 1, A + 1 + N);

  for (int i = 1; i <= N; i++)
    B[i] = A[i].a;

  built(1, 1, N);

  int pos, sum = 0, p;
  for (int i = 1; i <= N; i++) {
    pos = lower_bound(B + 1, B + 1 + N, sum * -1) - B;
    p = query(1, 1, N, pos, N);
    if (sum + A[p].a < 0 || p == -1) {
      cout << "No\\n";
      return 0;
    }
    sum += A[p].b;
    update(1, 1, N, p);
  }

  cout << "Yes\\n";
}