Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define rrep(i, a, n) for (int i = (int)(a); i >= (int)(n); --i)
#define debug(x) cerr << #x << " = " << x << "\n"
#define debugv(x)                                                              \
  rep(f, 0, (x.size() - 1)) cerr << x[f] << (f == (x.size() - 1) ? "\n" : " ")
#define debug2(x, y)                                                           \
  cerr << "(" << #x << "," << #y << ") = "                                     \
       << "(" << x << "," << y << ")\n"
#define all(x) x.begin(), x.end()
#define each(a, x) for (auto &a : (x))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define int long long
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
const int MX = 1e5 + 5, INF = 5LL << 57, MOD = 1e9 + 7;

int N;
void input() { scanf("%d", &N); }
bool is_prime[MX + 5];
vector<int> P;
void Eratosthenes(const int N) {
  for (int i = 2; i <= N; i++) {
    is_prime[i] = true; // initialize.
  }
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
}
int sm[MX + 2];
void solve() {
  Eratosthenes(MX);
  sm[3] = 1;
  rep(i, 4, MX - 1) {
    if (is_prime[i] && is_prime[(i + 1) / 2]) {
      sm[i]++;
    }
    sm[i] += sm[i - 1];
  }
  int a, b;
  rep(_, 1, N) {
    scanf("%d %d", &a, &b);
    // cin>>a>>b;
    cout << sm[b] - sm[a - 1] << endl;
  }

  ;
  ;
}
signed main() {
  input();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define rrep(i, a, n) for (int i = (int)(a); i >= (int)(n); --i)
#define debug(x) cerr << #x << " = " << x << "\n"
#define debugv(x)                                                              \
  rep(f, 0, (x.size() - 1)) cerr << x[f] << (f == (x.size() - 1) ? "\n" : " ")
#define debug2(x, y)                                                           \
  cerr << "(" << #x << "," << #y << ") = "                                     \
       << "(" << x << "," << y << ")\n"
#define all(x) x.begin(), x.end()
#define each(a, x) for (auto &a : (x))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
const int MX = 1e5 + 5, INF = 5LL << 57, MOD = 1e9 + 7;

int N;
void input() { scanf("%d", &N); }
bool is_prime[MX + 5];
vector<int> P;
void Eratosthenes(const int N) {
  for (int i = 2; i <= N; i++) {
    is_prime[i] = true; // initialize.
  }
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
}
int sm[MX + 2];
void solve() {
  Eratosthenes(MX);
  sm[3] = 1;
  rep(i, 4, MX - 1) {
    if (is_prime[i] && is_prime[(i + 1) / 2]) {
      sm[i]++;
    }
    sm[i] += sm[i - 1];
  }
  int a, b;
  rep(_, 1, N) {
    scanf("%d %d", &a, &b);
    // cin>>a>>b;
    cout << sm[b] - sm[a - 1] << endl;
  }

  ;
  ;
}
signed main() {
  input();
  solve();
  return 0;
}