Problem p01920 (C++) — buggy submission, verdict: Time Limit Exceeded

#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)

#define Left second
#define Right first
class SEGMENTS {
private:
  map<int, int> dat;

  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(l);
      if (it == dat.end() || r <= it->Left)
        break;

      int li = it->Left;
      int ri = it->Right;

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

  void insert(int l, int r) {
    while (true) {
      auto it = dat.upper_bound(l);
      if (it == dat.end() || r < it->Left) {
        if (dat.count(l)) {
          dat[r] = dat[l];
          dat.erase(l);
        } else
          dat[r] = l;
        break;
      }

      if (isInclude(it->Left, it->Right, l, r))
        dat.erase(it);
      else {
        l = min(l, it->Left);
        r = max(r, it->Right);
      }
    }
  }

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

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

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;
  }
}

Error: TLE

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)

#define Left second
#define Right first
class SEGMENTS {
private:
  map<int, int> dat;

  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(l);
      if (it == dat.end() || r <= it->Left)
        break;

      int li = it->Left;
      int ri = it->Right;

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

  void insert(int l, int r) {
    while (true) {
      auto it = dat.upper_bound(l);
      if (it == dat.end() || r < it->Left) {
        if (dat.count(l)) {
          dat[r] = dat[l];
          dat.erase(l);
        } else
          dat[r] = l;
        break;
      }

      if (isInclude(it->Left, it->Right, l, r))
        dat.erase(it);
      else {
        l = min(l, it->Left);
        r = max(r, it->Right);
      }
    }
  }

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

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

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;
  }
}