Problem p03554 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define For(i, j, k) for (int i = j; i <= k; i++)

#ifdef __LINUX__
#define getchar getchar_unlocked
#endif

inline int Read() {
  char c = getchar();
  while (c > '9' || c < '0')
    c = getchar();
  int x = c - '0';
  c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

using namespace std;

const int N = 2e5 + 10;

struct Segment_Tree {

  int Min[N << 2];

#define lc (o << 1)
#define rc (o << 1 | 1)
#define M ((L + R) >> 1)

  Segment_Tree() { memset(Min, 0x3f, sizeof Min); }

  void update(int o, int L, int R, int x, int v) {
    Min[o] = min(Min[o], v);
    if (L ^ R) {
      if (x <= M)
        update(lc, L, M, x, v);
      else
        update(rc, M + 1, R, x, v);
    }
  }

  int query(int o, int L, int R, int l, int r) {
    if (l <= L && R <= r)
      return Min[o];
    else {
      int ret = 1e9;
      if (l <= M)
        ret = query(lc, L, M, l, r);
      if (r > M)
        ret = min(ret, query(rc, M + 1, R, l, r));
      return ret;
    }
  }
} T;

struct Interval {
  int l, r;

  bool operator<(Interval &A) const { return l < A.l; }
} A[N];

int n, m;
int S[N];
int cnt0[N], cnt1[N];
int pre[N];

int main() {
  freopen("hamming.in", "r", stdin);
  freopen("hamming.out", "w", stdout);

  n = Read();
  For(i, 1, n) S[i] = Read(), cnt0[i] = cnt0[i - 1] + (!S[i]),
               cnt1[i] = i - cnt0[i];
  m = Read();
  For(i, 1, m) A[i].l = Read(), A[i].r = Read();
  sort(A + 1, A + m + 1);

  int ans = cnt1[n];
  For(i, 1, m) {
    int L = A[i].l - 1, R = A[i].r;
    For(j, A[i - 1].l, L) pre[j] = min(pre[j], pre[j - 1]);

    int ret = cnt1[L] + cnt0[R] - cnt0[L] + pre[L];
    ret = min(ret, cnt0[R] + T.query(1, 1, n, L + 1, R));

    T.update(1, 1, n, R, ret - cnt0[R]);
    pre[R] = min(pre[R], ret - cnt1[R]);

    ans = min(ans, ret + cnt1[n] - cnt1[R]);
  }
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define For(i, j, k) for (int i = j; i <= k; i++)

#ifdef __LINUX__
#define getchar getchar_unlocked
#endif

inline int Read() {
  char c = getchar();
  while (c > '9' || c < '0')
    c = getchar();
  int x = c - '0';
  c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}

using namespace std;

const int N = 2e5 + 10;

struct Segment_Tree {

  int Min[N << 2];

#define lc (o << 1)
#define rc (o << 1 | 1)
#define M ((L + R) >> 1)

  Segment_Tree() { memset(Min, 0x3f, sizeof Min); }

  void update(int o, int L, int R, int x, int v) {
    Min[o] = min(Min[o], v);
    if (L ^ R) {
      if (x <= M)
        update(lc, L, M, x, v);
      else
        update(rc, M + 1, R, x, v);
    }
  }

  int query(int o, int L, int R, int l, int r) {
    if (l <= L && R <= r)
      return Min[o];
    else {
      int ret = 1e9;
      if (l <= M)
        ret = query(lc, L, M, l, r);
      if (r > M)
        ret = min(ret, query(rc, M + 1, R, l, r));
      return ret;
    }
  }
} T;

struct Interval {
  int l, r;

  bool operator<(Interval &A) const { return l < A.l; }
} A[N];

int n, m;
int S[N];
int cnt0[N], cnt1[N];
int pre[N];

int main() {
  n = Read();
  For(i, 1, n) S[i] = Read(), cnt0[i] = cnt0[i - 1] + (!S[i]),
               cnt1[i] = i - cnt0[i];
  m = Read();
  For(i, 1, m) A[i].l = Read(), A[i].r = Read();
  sort(A + 1, A + m + 1);

  int ans = cnt1[n];
  For(i, 1, m) {
    int L = A[i].l - 1, R = A[i].r;
    For(j, A[i - 1].l, L) pre[j] = min(pre[j], pre[j - 1]);

    int ret = cnt1[L] + cnt0[R] - cnt0[L] + pre[L];
    ret = min(ret, cnt0[R] + T.query(1, 1, n, L + 1, R));

    T.update(1, 1, n, R, ret - cnt0[R]);
    pre[R] = min(pre[R], ret - cnt1[R]);

    ans = min(ans, ret + cnt1[n] - cnt1[R]);
  }
  printf("%d\n", ans);

  return 0;
}