Problem p03476 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
bool pr(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  int ans = 0;
  cin >> q;
  int l[q], r[q];
  bool p[100001] = {};
  vector<int> prim;
  p[2] = 1;
  p[3] = 1;
  p[5] = 1;
  p[7] = 1;
  p[11] = 1;
  p[13] = 1;
  p[17] = 1;
  p[19] = 1;
  p[23] = 1;
  p[29] = 1;
  p[31] = 1;
  p[37] = 1;
  p[41] = 1;
  p[43] = 1;
  p[47] = 1;

  prim.push_back(2);
  for (int i = 29; i <= 100000; i = i + 2) {
    if ((i % 2) != 0 && (i % 3) != 0 && (i % 5) != 0 && (i % 7) != 0 &&
        (i % 7) != 0 && (i % 11) != 0 && (i % 13) != 0 && (i % 17) != 0 &&
        (i % 19) != 0 && (i % 23) != 0 && (i % 29) != 0 && (i % 31) != 0 &&
        (i % 37) != 0 && (i % 41) != 0 && (i % 43) != 0 && (i % 47) != 0) {
      p[i] = pr(i);
    }
  }
  for (int i = 3; i <= 100000; i = i + 2) {
    if (p[i]) {
      prim.push_back(i);
    }
  }
  int si = prim.size();
  for (int i = 0; i < q; ++i) {
    ans = 0;
    cin >> l[i] >> r[i];
    vector<int>::iterator lit = lower_bound(prim.begin(), prim.end(), l[i]);
    int tmp = lit - prim.begin();
    if (tmp >= si - 1) {
      goto end1;
    }
    for (int j = tmp; prim[j] < r[i] + 1; j++) {
      if (p[(prim[j] + 1) / 2] && l[i] <= prim[j]) {
        ans++;
      }
      if (j == si - 1) {
        break;
      }
    }
  end1:;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
bool pr(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  int ans = 0;
  cin >> q;
  int l[q], r[q];
  bool p[100001] = {};
  vector<int> prim;
  p[2] = 1;
  p[3] = 1;
  p[5] = 1;
  p[7] = 1;
  p[11] = 1;
  p[13] = 1;
  p[17] = 1;
  p[19] = 1;
  p[23] = 1;
  p[29] = 1;
  p[31] = 1;
  p[37] = 1;
  p[41] = 1;
  p[43] = 1;
  p[47] = 1;

  prim.push_back(2);
  for (int i = 29; i <= 100000; i = i + 2) {
    if ((i % 2) != 0 && (i % 3) != 0 && (i % 5) != 0 && (i % 7) != 0 &&
        (i % 7) != 0 && (i % 11) != 0 && (i % 13) != 0 && (i % 17) != 0 &&
        (i % 19) != 0 && (i % 23) != 0 && (i % 29) != 0 && (i % 31) != 0 &&
        (i % 37) != 0 && (i % 41) != 0 && (i % 43) != 0 && (i % 47) != 0) {
      p[i] = pr(i);
    }
  }
  for (int i = 3; i <= 100000; i = i + 2) {
    if (p[i]) {
      prim.push_back(i);
    }
  }
  int si = prim.size();
  for (int i = 0; i < q; ++i) {
    ans = 0;
    cin >> l[i] >> r[i];
    vector<int>::iterator lit = lower_bound(prim.begin(), prim.end(), l[i]);
    int tmp = lit - prim.begin();
    if (tmp >= si - 1) {
      goto end1;
    }
    for (int j = tmp; prim[j] < r[i] + 1; j++) {
      if (p[(prim[j] + 1) / 2]) {
        ans++;
      }
      if (j == si - 1) {
        break;
      }
    }
  end1:;
    cout << ans << endl;
  }
  return 0;
}