Problem p03954 (C++)

Buggy submission (status: Runtime Error):
/*----------------by syr----------------*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9 + 7;

int n, m;
int a[100005], b[100005];

bool check(int x) {
  //	printf("%d\n",x);
  for (int i = 1; i <= n; i++)
    if (a[i] >= x)
      b[i] = 1;
    else
      b[i] = 0;
  //	for(int i=1;i<=n;i++) printf("%d ",b[i]);
  //	puts("");
  int l = 1, r = 1;
  if (b[1] == b[2]) {
    while (true) {
      while (b[r] == b[r + 1] && r + 1 <= n)
        ++r;
      //			printf("l=%d r=%d\n",l,r);
      if (l <= m && r >= m) {
        if (b[r] == 1)
          return 1;
        else
          return 0;
      }
      l = r;
      while (b[r] != b[r + 1] && r + 1 <= n)
        ++r;
      if (l <= m && r >= m) {
        if (m <= (l + r) >> 1)
          return b[l];
        else
          return b[r];
      }
      l = r;
    }
  } else {
    while (true) {
      while (b[r] != b[r + 1] && r + 1 <= n) {
        ++r;
      }
      //			printf("l=%d r=%d\n",l,r);
      if (l <= m && r >= m) {
        if (m <= (l + r) >> 1)
          return b[l];
        else
          return b[r];
      }
      l = r;
      while (b[r] == b[r + 1] && r + 1 <= n)
        ++r;
      if (l <= m && r >= m) {
        if (b[r] == 1)
          return 1;
        else
          return 0;
      }
      l = r;
    }
  }
  return false;
}

int main() {
  scanf("%d", &m);
  n = (m << 1) - 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  int lb = 1, rb = n + 1;
  while (lb + 1 < rb) {
    int md = (lb + rb) >> 1;
    if (check(md))
      lb = md;
    else
      rb = md;
  }
  printf("%d\n", lb);
  return 0;
}


Fixed submission:
/*----------------by syr----------------*/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1e9 + 7;

int n, m;
int a[200005], b[200005];

bool check(int x) {
  //	printf("%d\n",x);
  for (int i = 1; i <= n; i++)
    if (a[i] >= x)
      b[i] = 1;
    else
      b[i] = 0;
  //	for(int i=1;i<=n;i++) printf("%d ",b[i]);
  //	puts("");
  int l = 1, r = 1;
  if (b[1] == b[2]) {
    while (true) {
      while (b[r] == b[r + 1] && r + 1 <= n)
        ++r;
      //			printf("l=%d r=%d\n",l,r);
      if (l <= m && r >= m) {
        if (b[r] == 1)
          return 1;
        else
          return 0;
      }
      l = r;
      while (b[r] != b[r + 1] && r + 1 <= n)
        ++r;
      if (l <= m && r >= m) {
        if (m <= (l + r) >> 1)
          return b[l];
        else
          return b[r];
      }
      l = r;
    }
  } else {
    while (true) {
      while (b[r] != b[r + 1] && r + 1 <= n) {
        ++r;
      }
      //			printf("l=%d r=%d\n",l,r);
      if (l <= m && r >= m) {
        if (m <= (l + r) >> 1)
          return b[l];
        else
          return b[r];
      }
      l = r;
      while (b[r] == b[r + 1] && r + 1 <= n)
        ++r;
      if (l <= m && r >= m) {
        if (b[r] == 1)
          return 1;
        else
          return 0;
      }
      l = r;
    }
  }
  return false;
}

int main() {
  scanf("%d", &m);
  n = (m << 1) - 1;
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  int lb = 1, rb = n + 1;
  while (lb + 1 < rb) {
    int md = (lb + rb) >> 1;
    if (check(md))
      lb = md;
    else
      rb = md;
  }
  printf("%d\n", lb);
  return 0;
}