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 * 10 + ch - '0';
    ch = getchar();
  }
  return r * f;
}
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()) {
    rep(i, 0, q) {
      int l, r;
      scanf("%d%d", &l, &r);
      int pos1 = lower_bound(prime, prime + cnt, l) - prime;
      // printf("prime  %d\n",prime[pos1]);
      int pos2 = lower_bound(prime, prime + cnt, r) - prime;
      if (prime[pos2] == r)
        r = prime[pos2];
      else
        r = prime[pos2 - 1];
      // printf("%d %d\n",sum[r],sum[pos1]);
      printf("%d\n", sum[r] - sum[prime[pos1 - 1]]);
    }
  }
  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 * 10 + ch - '0';
    ch = getchar();
  }
  return r * f;
}
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;
  q = Scan();
  rep(i, 0, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    int pos1 = lower_bound(prime, prime + cnt, l) - prime;
    // printf("prime  %d\n",prime[pos1]);
    int pos2 = lower_bound(prime, prime + cnt, r) - prime;
    if (prime[pos2] == r)
      r = prime[pos2];
    else
      r = prime[pos2 - 1];
    // printf("%d %d\n",sum[r],sum[pos1]);
    printf("%d\n", sum[r] - sum[prime[pos1 - 1]]);
  }
  return 0;
}