Problem p03580 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <set>
#include <vector>
#define ll long long
#define pr(x) cerr << #x << "=" << x << endl
using namespace std;
struct node {
  int l, r;
} b[510000];
bool cmp(node x, node y) {
  if (x.r == y.r)
    return x.l < y.l;
  return x.r < y.r;
}
int n, i, cnt, a[510000], nowl, nowr, tot, now, dp[510000];
char ch[510000];
int main() {
  scanf("%d", &n);
  scanf("%s", ch);
  for (i = 1; i <= n; i++)
    a[i] = ch[i - 1] - '0';
  i = 2;
  a[0] = a[n + 1] = -1;
  while (i < n) {
    if (a[i - 1] == 1 && a[i] == 0 && a[i + 1] == 1) {
      now = i;
      i = i + 1;
      while (a[i] == 1) {
        b[++cnt].l = now - 1;
        b[cnt].r = i;
        i++;
      }
    } else
      i++;
  }
  i = n - 1;
  while (i > 1) {
    if (a[i + 1] == 1 && a[i] == 0 && a[i - 1] == 1) {
      now = i;
      i = i - 1;
      while (a[i] == 1) {
        b[++cnt].r = now + 1;
        b[cnt].l = i;
        i--;
      }
    } else
      i--;
  }
  sort(b + 1, b + 1 + cnt, cmp);
  // for (i=1;i<=cnt;i++) printf("%d %d\n",b[i].l,b[i].r);
  // cout<<endl;
  for (i = 1; i <= cnt; i++) {
    if (nowl != b[i].l || nowr != b[i].r) {
      b[++tot] = b[i];
      nowl = b[i].l;
      nowr = b[i].r;
    }
  }
  // for (i=1;i<=tot;i++) printf("%d %d\n",b[i].l,b[i].r);
  now = 1;
  for (i = 1; i <= n; i++) {
    dp[i] = max(dp[i], dp[i - 1]);
    while (b[now].r == i) {
      dp[i] = max(dp[i], dp[b[now].l - 1] + b[now].r - b[now].l - 1);
      now++;
    }
  }
  // for (i=1;i<=n;i++) printf("dp[%d]=%d\n",i,dp[i]);
  printf("%d\n", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <set>
#include <vector>
#define ll long long
#define pr(x) cerr << #x << "=" << x << endl
using namespace std;
struct node {
  int l, r;
} b[5100000];
bool cmp(node x, node y) {
  if (x.r == y.r)
    return x.l < y.l;
  return x.r < y.r;
}
int n, i, cnt, a[510000], nowl, nowr, tot, now, dp[510000];
char ch[510000];
int main() {
  scanf("%d", &n);
  scanf("%s", ch);
  for (i = 1; i <= n; i++)
    a[i] = ch[i - 1] - '0';
  i = 2;
  a[0] = a[n + 1] = -1;
  while (i < n) {
    if (a[i - 1] == 1 && a[i] == 0 && a[i + 1] == 1) {
      now = i;
      i = i + 1;
      while (a[i] == 1) {
        b[++cnt].l = now - 1;
        b[cnt].r = i;
        i++;
      }
    } else
      i++;
  }
  i = n - 1;
  while (i > 1) {
    if (a[i + 1] == 1 && a[i] == 0 && a[i - 1] == 1) {
      now = i;
      i = i - 1;
      while (a[i] == 1) {
        b[++cnt].r = now + 1;
        b[cnt].l = i;
        i--;
      }
    } else
      i--;
  }
  sort(b + 1, b + 1 + cnt, cmp);
  // for (i=1;i<=cnt;i++) printf("%d %d\n",b[i].l,b[i].r);
  // cout<<endl;
  for (i = 1; i <= cnt; i++) {
    if (nowl != b[i].l || nowr != b[i].r) {
      b[++tot] = b[i];
      nowl = b[i].l;
      nowr = b[i].r;
    }
  }
  // for (i=1;i<=tot;i++) printf("%d %d\n",b[i].l,b[i].r);
  now = 1;
  for (i = 1; i <= n; i++) {
    dp[i] = max(dp[i], dp[i - 1]);
    while (b[now].r == i) {
      dp[i] = max(dp[i], dp[b[now].l - 1] + b[now].r - b[now].l - 1);
      now++;
    }
  }
  // for (i=1;i<=n;i++) printf("dp[%d]=%d\n",i,dp[i]);
  printf("%d\n", dp[n]);
  return 0;
}