Problem p03476 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mem(a, n) memset(a, n, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int prime[N], sum[N];
bool is_pr[N];
int cnt;
inline int Scan() {
  int r = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    r = (r << 3) + (r << 1) + ch - '0';
    ch = getchar();
  }
  return r * f;
}
inline void Print(int x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9)
    Print(x / 10);
  putchar(x % 10 + '0');
}
void sieve() {
  cnt = 0;
  mem(is_pr, 0);
  for (int i = 2; i * i <= N; i++) {
    if (!is_pr[i]) {
      for (int j = i * i; j <= N; j += i)
        is_pr[j] = 1;
    }
  }
  for (int i = 2; i < N; i++)
    if (!is_pr[i])
      prime[cnt++] = i;
  mem(sum, 0);
  for (int i = 1; i < cnt; i++) {
    int tmp = (prime[i] + 1) / 2;
    int pos = lower_bound(prime, prime + i, tmp) - prime;
    if (prime[pos] == tmp)
      sum[prime[i]]++;
  }
  for (int i = 1; i < cnt; i++)
    sum[prime[i]] += sum[prime[i - 1]];
}
int main() {
  sieve();
  int q;
  while ((q = Scan()) != EOF) {
    rep(i, 0, q) {
      int l, r;
      l = Scan();
      r = Scan();
      int pos1 = lower_bound(prime, prime + cnt, l) - prime;
      int pos2 = lower_bound(prime, prime + cnt, r) - prime;
      if (prime[pos2] == r)
        r = prime[pos2];
      else
        r = prime[pos2 - 1];
      int ans = sum[r] - sum[prime[pos1 - 1]];
      Print(ans);
      puts("");
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mem(a, n) memset(a, n, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int prime[N], sum[N];
bool is_pr[N];
int cnt;
inline int Scan() {
  int r = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    r = (r << 3) + (r << 1) + ch - '0';
    ch = getchar();
  }
  return r * f;
}
inline void Print(int x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9)
    Print(x / 10);
  putchar(x % 10 + '0');
}
void sieve() {
  cnt = 0;
  mem(is_pr, 0);
  for (int i = 2; i * i <= N; i++) {
    if (!is_pr[i]) {
      for (int j = i * i; j <= N; j += i)
        is_pr[j] = 1;
    }
  }
  for (int i = 2; i < N; i++)
    if (!is_pr[i])
      prime[cnt++] = i;
  mem(sum, 0);
  for (int i = 1; i < cnt; i++) {
    int tmp = (prime[i] + 1) / 2;
    int pos = lower_bound(prime, prime + i, tmp) - prime;
    if (prime[pos] == tmp)
      sum[prime[i]]++;
  }
  for (int i = 1; i < cnt; i++)
    sum[prime[i]] += sum[prime[i - 1]];
}
int main() {
  sieve();
  int q;
  while (~scanf("%d", &q)) {
    rep(i, 0, q) {
      int l, r;
      l = Scan();
      r = Scan();
      int pos1 = lower_bound(prime, prime + cnt, l) - prime;
      int pos2 = lower_bound(prime, prime + cnt, r) - prime;
      if (prime[pos2] == r)
        r = prime[pos2];
      else
        r = prime[pos2 - 1];
      int ans = sum[r] - sum[prime[pos1 - 1]];
      Print(ans);
      puts("");
    }
  }
  return 0;
}