Problem p03554 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

const int INF = 1000000000;

#define fr first
#define sc second

struct SEG {
  int siz = 1 << 18;
  int s[1 << 19], t[1 << 19];
  void init() {
    for (int i = 0; i < siz * 2 - 1; i++) {
      s[i] = INF;
      t[i] = 0;
    }
  }
  void add(int a, int b, int x, int k, int l, int r) {
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      t[k]++;
      return;
    }
    add(a, b, x, 2 * k + 1, l, (l + r) / 2);
    add(a, b, x, 2 * k + 2, (l + r) / 2, r);
    s[k] = min(s[2 * k + 1] + t[2 * k + 1], s[2 * k + 2] + t[2 * k + 2]);
  }
  void chmin(int a, int x) {
    int b = a + siz - 1;
    x -= t[b];
    while (b > 0) {
      b = (b - 1) / 2;
      x -= t[b];
    }
    a += siz - 1;
    s[a] = min(s[a], x);
    while (a > 0) {
      x += t[a];
      a = (a - 1) / 2;
      s[a] = min(s[a], x);
    }
  }
  int query(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return INF;
    if (a <= l && r <= b)
      return s[k] + t[k];
    int chl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int chr = query(a, b, 2 * k + 2, (l + r) / r, r);
    return min(chl, chr) + t[k];
  }
} dp;

int main() {
  static int n;
  static int b[200010];
  static int q;
  static int l[200010], r[200010];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &l[i], &r[i]);
  }

  vector<P> vec;
  for (int i = 1; i <= q; i++) {
    vec.push_back(P(l[i], r[i]));
  }
  sort(vec.begin(), vec.end());

  dp.init();
  dp.chmin(0, 0);
  int loc = 0;
  for (int i = 1; i <= n; i++) {
    while (loc < vec.size() && vec[loc].fr == i) {
      dp.chmin(vec[loc].sc, dp.query(0, vec[loc].sc + 1, 0, 0, dp.siz));
      loc++;
    }
    if (b[i] == 0) {
      dp.add(i, dp.siz, 1, 0, 0, dp.siz);
    } else {
      dp.add(0, i, 1, 0, 0, dp.siz);
    }
  }
  cout << dp.query(0, dp.siz, 0, 0, dp.siz) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

const int INF = 1000000000;

#define fr first
#define sc second

struct SEG {
  int siz = 1 << 18;
  int s[1 << 19], t[1 << 19];
  void init() {
    for (int i = 0; i < siz * 2 - 1; i++) {
      s[i] = INF;
      t[i] = 0;
    }
  }
  void add(int a, int b, int x, int k, int l, int r) {
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      t[k]++;
      return;
    }
    add(a, b, x, 2 * k + 1, l, (l + r) / 2);
    add(a, b, x, 2 * k + 2, (l + r) / 2, r);
    s[k] = min(s[2 * k + 1] + t[2 * k + 1], s[2 * k + 2] + t[2 * k + 2]);
  }
  void chmin(int a, int x) {
    int b = a + siz - 1;
    x -= t[b];
    while (b > 0) {
      b = (b - 1) / 2;
      x -= t[b];
    }
    a += siz - 1;
    s[a] = min(s[a], x);
    while (a > 0) {
      x += t[a];
      a = (a - 1) / 2;
      s[a] = min(s[a], x);
    }
  }
  int query(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return INF;
    if (a <= l && r <= b)
      return s[k] + t[k];
    int chl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int chr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(chl, chr) + t[k];
  }
} dp;

int main() {
  static int n;
  static int b[200010];
  static int q;
  static int l[200010], r[200010];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &l[i], &r[i]);
  }

  vector<P> vec;
  for (int i = 1; i <= q; i++) {
    vec.push_back(P(l[i], r[i]));
  }
  sort(vec.begin(), vec.end());

  dp.init();
  dp.chmin(0, 0);
  int loc = 0;
  for (int i = 1; i <= n; i++) {
    while (loc < vec.size() && vec[loc].fr == i) {
      dp.chmin(vec[loc].sc, dp.query(0, vec[loc].sc + 1, 0, 0, dp.siz));
      loc++;
    }
    if (b[i] == 0) {
      dp.add(i, dp.siz, 1, 0, 0, dp.siz);
    } else {
      dp.add(0, i, 1, 0, 0, dp.siz);
    }
  }
  cout << dp.query(0, dp.siz, 0, 0, dp.siz) << endl;
}