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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

int n;
char x[125252];
int q;

typedef pair<pii, int> piii;
#define L first.first
#define R first.second
#define C second

int f = 0, g = 0;

int main() {
  scanf("%d", &n);
  scanf("%s", x);
  scanf("%d", &q);
  set<piii> S;
  int bef = 0;
  int head = 0;
  REP(i, n) if (x[i] - '0' != bef) {
    S.insert(piii(pii(head, i - 1), bef));
    bef = x[i] - '0';
    head = i;
  }
  S.insert(piii(pii(head, n - 1), bef));
  // for(piii P : S){
  //   printf("%d %d %d\\n",P.L,P.R,P.C);
  // }

  REP(i, n) f += x[i] - '0';
  REP(i, n - 1) g += (x[i] - '0') * (x[i + 1] - '0');

  while (q--) {
    int l, r, b;
    scanf("%d%d%d", &l, &r, &b);
    --l;
    --r;
    piii add = piii(pii(l, r), b);
    // left
    {
      auto it = S.lower_bound(piii(pii(l, -1), -1));
      if (it != S.begin()) {
        --it;
        auto cell = *it;
        int cl = cell.L;
        int cr = cell.R;
        int cc = cell.C;
        if (l <= cr) {
          // cross
          if (b == cc) {
            // nobasu
            l = cl;
          } else {
            // cut
            if (cc == 1) {
              --g;
            }
            S.erase(it);
            auto cell2 = cell;
            cell.R = l - 1;
            cell2.L = l;
            S.insert(cell);
            S.insert(cell2);
          }
        }
      }
    }
    // absorb
    while (true) {
      auto it = S.lower_bound(piii(pii(l, -1), -1));
      if (it == S.end())
        break;
      if (it->R <= r) {
        auto cell = *it;
        S.erase(it);
        if (cell.C == 1) {
          int len = cell.R - cell.L + 1;
          f -= len;
          g -= len - 1;
        }
      } else {
        break;
      }
    }
    // tail
    {
      auto it = S.lower_bound(piii(pii(l, -1), -1));
      if (it != S.end()) {
        if (it->C == b) {
          if (it->C == 1) {
            int len = it->R - it->L + 1;
            f -= len;
            g -= len - 1;
          }
          r = it->R;
          S.erase(it);
        } else {
          if (it->C == 1) {
            int len = r - it->L + 1;
            f -= len;
            g -= len;
          }
          auto cell = *it;
          cell.L = r + 1;
          S.erase(it);
          S.insert(cell);
        }
      }
    }
    // head connect
    {
      auto it = S.lower_bound(piii(pii(l, -1), -1));
      if (it != S.begin()) {
        --it;
        if (it->C == b) {
          l = it->L;
          if (it->C == 1) {
            int len = it->R - it->L + 1;
            f -= len;
            g -= len - 1;
          }
          S.erase(it);
        }
      }
    }
    // tail connect
    {
      auto it = S.lower_bound(piii(pii(l, -1), -1));
      if (it != S.end()) {
        if (it->C == b) {
          r = it->R;
          if (it->C == 1) {
            int len = it->R - it->L + 1;
            f -= len;
            g -= len - 1;
          }
          S.erase(it);
        }
      }
    }
    S.insert(piii(pii(l, r), b));
    if (b == 1) {
      int len = r - l + 1;
      f += len;
      g += len - 1;
    }
    // for(piii P : S){
    //   printf("%d %d %d\\n",P.L,P.R,P.C);
    // }

    //
    printf("%d\\n", f - g);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

int n;
char x[125252];
int q;

int main() {
  scanf("%d", &n);
  scanf("%s", x);
  set<pair<pii, int>> S;
  {
    int head = 0;
    int col = x[0] - '0';
    FOR(i, 1, n) {
      if (col == x[i] - '0') {
        // continue;
      } else {
        // cut
        S.insert(make_pair(pii(head, i - 1), col));
        head = i;
        col = x[i] - '0';
      }
    }
    S.insert(make_pair(pii(head, n - 1), col));
  }
  int f = 0, g = 0;
  // calc f,g
  REP(i, n) f += x[i] - '0';
  REP(i, n - 1) g += (x[i] - '0') * (x[i + 1] - '0');

  if (false) {
    for (auto P : S) {
      printf("[%d,%d], %d\\n", P.first.first, P.first.second, P.second);
    }
  }
  // query
  scanf("%d", &q);
  while (q--) {
    int l, r, b;
    scanf("%d%d%d", &l, &r, &b);
    --l;
    --r;
    auto it = S.upper_bound(make_pair(pii(l, 1e8), 2));
    it--;
    // cut & merge
    auto cell = *it;
    if (cell.second == b) {
      // merge only
      int len = cell.first.second - cell.first.first + 1;
      // merge all
      if (cell.second == 1) {
        g -= len - 1;
        f -= len;
      }
      l = cell.first.first;
      if (cell.first.second > r) {
        r = cell.first.second;
      }
    } else {
      // cut
      int len = cell.first.second - cell.first.first + 1;
      int cr = cell.first.second;
      if (cell.first.first == l) {
        // erase all
        if (cell.second == 1) {
          g -= len - 1;
          f -= len;
        }
      } else {
        // erase sub
        if (cell.second == 1) {
          g -= len;
          f -= len;
        }
        cell.first.second = l - 1;
        S.insert(cell);
      }
      if (cr > r) {
        // DEBUG("HERE");
        S.insert(make_pair(pii(r + 1, cr), cell.second));
        if (cell.second == 1) {
          int len = cell.first.second - (r + 1) + 1;
          g += len - 1;
          f += len;
        }
      }
    }
    S.erase(it++);
    // for(auto P : S){
    //   printf("[%d,%d], %d\\n",P.first.first,P.first.second,P.second);
    // }
    {
      // merge to head
      auto it2 = S.lower_bound(make_pair(pii(l, r), b));
      while (it2 != S.begin()) {
        it2--;
        if (it2->second == b && it2->first.second + 1 == l) {
          // merge em all
          auto cell = *it2;
          S.erase(it2++);
          if (b == 1) {
            int len = cell.first.second - cell.first.first + 1;
            g -= len - 1;
            f -= len;
          }
          l = cell.first.first;
        } else {
          it = it2;
          it++;
          break;
        }
      }
    }
    // for(auto P : S){
    //   printf("[%d,%d], %d\\n",P.first.first,P.first.second,P.second);
    // }
    // absorb
    it = S.lower_bound(make_pair(pii(l, r), b));
    while (it != S.end() && it->first.second < r) {
      auto cell = *it;
      S.erase(it++);
      if (cell.second == 1) {
        // erase all
        int len = cell.first.second - cell.first.first + 1;
        g -= len - 1;
        f -= len;
      }
    }
    // for(auto P : S){
    //   printf("[%d,%d], %d\\n",P.first.first,P.first.second,P.second);
    // }
    // tail
    // cut or merge
    auto it2 = S.upper_bound(make_pair(pii(l, r), b));
    it2--;
    if (it != S.end()) {
      cell = *it;
      if (cell.first.first <= r) {
        if (cell.second == b) {
          // merge only
          int len = cell.first.second - cell.first.first + 1;
          if (b == 1) {
            g -= len - 1;
            f -= len;
          }
          r = cell.first.second;
        } else {
          // cut
          int len = cell.first.second - cell.first.first + 1;
          if (cell.first.second == r) {
            // erase all
            if (cell.second == 1) {
              g -= len - 1;
              f -= len;
            }
          } else {
            // erase sub
            if (cell.second == 1) {
              g -= len;
              f -= len;
            }
            cell.first.first = r + 1;
            S.insert(cell);
          }
        }
        S.erase(it++);
      }
    }
    {
      // merge to tail
      auto it2 = S.upper_bound(make_pair(pii(l, r), b));
      it2--;
      while (it2 != S.end()) {
        it2++;
        if (it2 == S.end())
          break;
        // DEBUG((it2->first.first));
        // DEBUG((it2->first.second));
        if (it2->second == b && it2->first.first - 1 == r) {
          // merge em all
          auto cell = *it2;
          S.erase(it2--);
          if (b == 1) {
            int len = cell.first.second - cell.first.first + 1;
            g -= len - 1;
            f -= len;
          }
          r = cell.first.second;
        } else {
          break;
        }
      }
    }
    S.insert(make_pair(pii(l, r), b));
    if (b == 1) {
      int len = r - l + 1;
      f += len;
      g += len - 1;
    }
    printf("%d\\n", f - g);
    // for(auto P : S){
    //   printf("[%d,%d], %d\\n",P.first.first,P.first.second,P.second);
    // }
  }
  return 0;
}