Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/allocator.h>
#include <bits/basic_ios.h>
#include <bits/basic_string.h>
#include <bits/basic_string.tcc>
#include <bits/c++config.h>
#include <bits/char_traits.h>
#include <bits/cpp_type_traits.h>
#include <bits/cxxabi_forced.h>
#include <bits/ios_base.h>
#include <bits/locale_classes.h>
#include <bits/localefwd.h>
#include <bits/ostream_insert.h>
#include <bits/postypes.h>
#include <bits/range_access.h>
#include <bits/stl_algobase.h>
#include <bits/stl_function.h>
#include <bits/stl_iterator.h>
#include <bits/stl_iterator_base_funcs.h>
#include <bits/stl_iterator_base_types.h>
#include <bits/stringfwd.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <debug/debug.h>
#include <deque>
#include <exception>
#include <ext/atomicity.h>
#include <ext/numeric_traits.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
int n, l, r;
bool prime[100010];
vector<int> b, v;
int main() {
  cin >> n;
  for (int i = 2; i * i <= 100000; i++)
    if (!prime[i])
      for (int j = i * i; j <= 100000; j += i)
        prime[j] = 1;
  for (int i = 3; i <= 100000; i += 2)
    if (!prime[i] && !prime[(i + 1) / 2])
      v.push_back(i);
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    int num = 0;
    for (int i = 0; i <= 49327; i++) {
      if (v[i] >= l && v[i] <= r)
        num++;
      if (r < v[i])
        break;
    }
    b.push_back(num);
  }
  for (int i = 0; i < n; i++)
    cout << b[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/allocator.h>
#include <bits/basic_ios.h>
#include <bits/basic_string.h>
#include <bits/basic_string.tcc>
#include <bits/c++config.h>
#include <bits/char_traits.h>
#include <bits/cpp_type_traits.h>
#include <bits/cxxabi_forced.h>
#include <bits/ios_base.h>
#include <bits/locale_classes.h>
#include <bits/localefwd.h>
#include <bits/ostream_insert.h>
#include <bits/postypes.h>
#include <bits/range_access.h>
#include <bits/stl_algobase.h>
#include <bits/stl_function.h>
#include <bits/stl_iterator.h>
#include <bits/stl_iterator_base_funcs.h>
#include <bits/stl_iterator_base_types.h>
#include <bits/stringfwd.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <debug/debug.h>
#include <deque>
#include <exception>
#include <ext/atomicity.h>
#include <ext/numeric_traits.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
int n, l, r;
bool prime[100010];
vector<int> b, v;
int main() {
  cin >> n;
  for (int i = 2; i * i <= 100000; i++)
    if (!prime[i])
      for (int j = i * i; j <= 100000; j += i)
        prime[j] = 1;
  for (int i = 3; i <= 100000; i += 2)
    if (!prime[i] && !prime[(i + 1) / 2])
      v.push_back(i);
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    int num = 0;
    for (int i = 0; i <= 672; i++) {
      if (v[i] >= l && v[i] <= r)
        num++;
      if (r < v[i])
        break;
    }
    b.push_back(num);
  }
  for (int i = 0; i < n; i++)
    cout << b[i] << endl;
  return 0;
}