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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

class SEGMENTS {
private:
  struct seg {
    int l, r;
    bool operator<(seg obj) const {
      if (r != obj.r)
        return r < obj.r;
      else
        return l < obj.l;
    }
  };

  set<seg> dat;

  bool isCross(int l1, int r1, int l2, int r2) {
    return ((l1 <= l2 && l2 < r1) || (l2 <= l1 && l1 < r2));
  }
  bool isInclude(int li, int ri, int L, int R) { return (L <= li && ri <= R); }

public:
  void erase(int l, int r) {
    while (true) {
      auto it = dat.upper_bound(seg{(int)INF, l});
      if (it == dat.end())
        break;

      int li = it->l;
      int ri = it->r;
      if (!isCross(li, ri, l, r))
        break;

      if (isInclude(li, ri, l, r))
        dat.erase(it);
      else {
        int tl = li, tr = ri;
        if (isInclude(l, r, li, ri)) {
          dat.erase(it);
          if (li != l)
            dat.insert(seg{li, l});
          if (r != ri)
            dat.insert(seg{r, ri});
        } else {
          if (li <= l && l < ri)
            tr = l;
          if (l <= li && li < r)
            tl = r;
          dat.erase(it);
          if (tl != tr)
            dat.insert(seg{tl, tr});
        }
      }
    }
  }

  void insert(int l, int r) {
    while (true) {
      auto it = dat.upper_bound(seg{(int)INF, l});
      if (it == dat.end())
        break;

      int li = it->l;
      int ri = it->r;
      if (!isCross(li, ri, l, r))
        break;

      if (isInclude(li, ri, l, r))
        dat.erase(it);
      else {
        if (li < l)
          l = li;
        if (r < ri)
          r = ri;
      }
    }

    auto it = dat.lower_bound(seg{(int)-INF, l});
    if (it != dat.end() && it->r == l) {
      l = it->l;
      dat.erase(it);
    }
    it = dat.upper_bound(seg{(int)INF, r});
    if (it != dat.end() && it->l == r) {
      r = it->r;
      dat.erase(it);
    }

    if (l != r)
      dat.insert(seg{l, r});
  }

  int size() { return dat.size(); }

  void output() {
    for (auto e : dat)
      cout << e.l << " " << e.r << endl;
    cout << endl;
  }

  void assertion() {
    for (auto e : dat)
      assert(e.r - e.l > 0);
  }
};

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  SEGMENTS st;

  for (int i = 0; i < n;) {
    if (s[i] == '0')
      i++;
    else {
      int l = i;
      while (i < n && s[i] == '1')
        i++;
      st.insert(l, i);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int l, r, b;
    cin >> l >> r >> b;
    l--;

    if (b == 0)
      st.erase(l, r);
    else
      st.insert(l, r);

    cout << st.size() << endl;
    st.assertion();
    //        st.output();
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

class SEGMENTS {
private:
  struct seg {
    int l, r;
    bool operator<(seg obj) const {
      if (r != obj.r)
        return r < obj.r;
      else
        return l < obj.l;
    }
  };

  set<seg> dat;

  bool isCross(int l1, int r1, int l2, int r2) {
    return ((l1 <= l2 && l2 < r1) || (l2 <= l1 && l1 < r2));
  }
  bool isInclude(int li, int ri, int L, int R) { return (L <= li && ri <= R); }

public:
  void erase(int l, int r) {
    while (true) {
      auto it = dat.upper_bound(seg{(int)INF, l});
      if (it == dat.end())
        break;

      int li = it->l;
      int ri = it->r;
      if (!isCross(li, ri, l, r))
        break;

      if (isInclude(li, ri, l, r))
        dat.erase(it);
      else {
        int tl = li, tr = ri;
        if (isInclude(l, r, li, ri)) {
          dat.erase(it);
          if (li != l)
            dat.insert(seg{li, l});
          if (r != ri)
            dat.insert(seg{r, ri});
        } else {
          if (l < ri)
            tr = l;
          if (r < li)
            tl = r;
          dat.erase(it);
          if (tl != tr)
            dat.insert(seg{tl, tr});
        }
      }
    }
  }

  void insert(int l, int r) {
    while (true) {
      auto it = dat.upper_bound(seg{(int)INF, l});
      if (it == dat.end())
        break;

      int li = it->l;
      int ri = it->r;
      if (!isCross(li, ri, l, r))
        break;

      if (isInclude(li, ri, l, r))
        dat.erase(it);
      else {
        if (li < l)
          l = li;
        if (r < ri)
          r = ri;
      }
    }

    auto it = dat.lower_bound(seg{(int)-INF, l});
    if (it != dat.end() && it->r == l) {
      l = it->l;
      dat.erase(it);
    }
    it = dat.upper_bound(seg{(int)INF, r});
    if (it != dat.end() && it->l == r) {
      r = it->r;
      dat.erase(it);
    }

    if (l != r)
      dat.insert(seg{l, r});
  }

  int size() { return dat.size(); }

  void output() {
    for (auto e : dat)
      cout << e.l << " " << e.r << endl;
    cout << endl;
  }

  void assertion() {
    for (auto e : dat)
      assert(e.r - e.l > 0);
  }
};

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  SEGMENTS st;

  for (int i = 0; i < n;) {
    if (s[i] == '0')
      i++;
    else {
      int l = i;
      while (i < n && s[i] == '1')
        i++;
      st.insert(l, i);
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int l, r, b;
    cin >> l >> r >> b;
    l--;

    if (b == 0)
      st.erase(l, r);
    else
      st.insert(l, r);

    cout << st.size() << endl;
    st.assertion();
    //        st.output();
  }
}