Problem p03476 (C++)

Buggy submission (status: Runtime Error):
// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n); // 各桁の和を計算する。
int getDigit(int n);       // 数字の桁数を取得する。
string upper(string str);  // 英字を大文字に変換する。
string lower(string str);  // 英字を小文字に変換する。
vector<pair<long long, long long>> prime_factorize(long long p); // 素因数分解
vector<pair<char, long long>> runLengthEncoding(string s); // ランレングス圧縮

// class
// ------------------------------------------------
class Combi {
public:
  Combi();
  long long Combination(long long n, long long k);
  long long nPk_modp(long long n, long long k, long long p);

private:
  map<long long, map<long long, long long>> memo;
  long long n_num;
  long long k_num;
};

// define
// ------------------------------------------------
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) int((a).size())
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repe(i, n) for (long long i = 0; i <= n; i++)
#define vsort(v) sort((v).begin(), (v).end())
#define rvsort(v) sort(rall((v)))
#define vi vector<int>
#define GCD(a, b) __gcd((a), (b))
#define LCM(a, b) (a) / GCD((a), (b)) * (b)
#define kiriage(a, b) ((a) + (b)-1) / (b)

const int INF = 1e9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<long> vll;
// code
// ------------------------------------------------
int main() {

  const long LIMIT = 10000;

  vector<bool> is_prime(10001, true);

  is_prime[0] = false;
  is_prime[1] = false;

  // エラトステネスの篩
  for (ll i = 2; i <= LIMIT; i++) {
    if (is_prime[i]) {
      for (ll j = 2; j * i <= LIMIT; j++) {
        is_prime[i * j] = false;
      }
    }
  }

  // like_number かチェック
  vector<bool> is_like_number(LIMIT + 1, false);
  for (ll i = 2; i <= LIMIT; i++) {
    if (is_prime[i] && is_prime[(i + 1) / 2]) {
      is_like_number[i] = true;
    }
  }

  // like_numberの累積和を作る
  vector<ll> cum_sum(LIMIT + 2, 0);
  for (ll i = 0; i <= LIMIT; i++) {
    cum_sum[i + 1] = cum_sum[i] + (is_like_number[i + 1] ? 1 : 0);
  }

  ll q;
  cin >> q;
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    ll ans = cum_sum[r + 1] - cum_sum[l - 1];
    cout << ans << endl;
  }

  return 0;
}
// funcの実体
// ------------------------------------------------
int getDigit(int n) {
  int i = 1;
  while (1) {
    n = n / 10;
    if (n == 1)
      break;
    i++;
  }
  return i;
}
int CalcSumOfDigit(int n) {
  int s = 0;
  while (n) {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str) {
  for (auto itr = str.begin(); itr != str.end(); itr++) {
    if (97 <= *itr && *itr <= 122) {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str) {
  for (auto itr = str.begin(); itr != str.end(); itr++) {
    if (65 <= *itr && *itr <= 90) {
      *itr = *itr + 32;
    }
  }
  return str;
}

Combi::Combi() {
  n_num = -1;
  k_num = -1;
};

ll Combi::Combination(ll n, ll k) {

  ll ret;
  if (memo[n][k] != 0) {
    ret = memo[n][k];
  } else if (n == k || k == 0) {
    memo[n][k] = 1;
    ret = 1;
  } else {
    ret = Combination(n - 1, k - 1) + Combination(n - 1, k);
    memo[n][k] = ret;
  }
  return ret;
}

long long Combi::nPk_modp(long long n, long long k, long long p) {
  ll ans = 1;
  for (long long i = k; i <= n; i++) {
    ans = (ans * i) % p;
  }

  return ans;
};

vector<pair<long long, long long>> prime_factorize(long long p) {
  vector<pair<long long, long long>> ret;

  for (long long x = 2; x * x <= p; ++x) {
    if (p % x != 0)
      continue;
    long long num = 0;
    while (p % x == 0) {
      num++;
      p /= x;
    }
    ret.push_back(make_pair(x, num));
  }

  if (p != 1)
    ret.push_back(make_pair(p, 1));

  return ret;
}

vector<pair<char, long long>> runLengthEncoding(string s) {
  vector<pair<char, long long>> ret;

  for (long long i = 0; i < s.length(); i++) {
    char cur = s[i];
    long long cnt = 1;

    for (long long j = i + 1; j < s.length(); j++) {
      if (cur != s[j])
        break;

      cnt++;
    }

    ret.emplace_back(make_pair(cur, cnt));

    i += cnt - 1;
  }

  return ret;
}

Fixed submission:
// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n); // 各桁の和を計算する。
int getDigit(int n);       // 数字の桁数を取得する。
string upper(string str);  // 英字を大文字に変換する。
string lower(string str);  // 英字を小文字に変換する。
vector<pair<long long, long long>> prime_factorize(long long p); // 素因数分解
vector<pair<char, long long>> runLengthEncoding(string s); // ランレングス圧縮

// class
// ------------------------------------------------
class Combi {
public:
  Combi();
  long long Combination(long long n, long long k);
  long long nPk_modp(long long n, long long k, long long p);

private:
  map<long long, map<long long, long long>> memo;
  long long n_num;
  long long k_num;
};

// define
// ------------------------------------------------
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) int((a).size())
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repe(i, n) for (long long i = 0; i <= n; i++)
#define vsort(v) sort((v).begin(), (v).end())
#define rvsort(v) sort(rall((v)))
#define vi vector<int>
#define GCD(a, b) __gcd((a), (b))
#define LCM(a, b) (a) / GCD((a), (b)) * (b)
#define kiriage(a, b) ((a) + (b)-1) / (b)

const int INF = 1e9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<long> vll;
// code
// ------------------------------------------------
int main() {

  const long LIMIT = 100000;

  vector<bool> is_prime(100001, true);

  is_prime[0] = false;
  is_prime[1] = false;

  // エラトステネスの篩
  for (ll i = 2; i <= LIMIT; i++) {
    if (is_prime[i]) {
      for (ll j = 2; j * i <= LIMIT; j++) {
        is_prime[i * j] = false;
      }
    }
  }

  // like_number かチェック
  vector<bool> is_like_number(LIMIT + 1, false);
  for (ll i = 2; i <= LIMIT; i++) {
    if (is_prime[i] && is_prime[(i + 1) / 2]) {
      is_like_number[i] = true;
    }
  }

  // like_numberの累積和を作る
  vector<ll> cum_sum(LIMIT + 2, 0);
  for (ll i = 0; i <= LIMIT; i++) {
    cum_sum[i + 1] = cum_sum[i] + (is_like_number[i + 1] ? 1 : 0);
  }

  ll q;
  cin >> q;
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    ll ans = cum_sum[r + 1] - cum_sum[l - 1];
    cout << ans << endl;
  }

  return 0;
}
// funcの実体
// ------------------------------------------------
int getDigit(int n) {
  int i = 1;
  while (1) {
    n = n / 10;
    if (n == 1)
      break;
    i++;
  }
  return i;
}
int CalcSumOfDigit(int n) {
  int s = 0;
  while (n) {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str) {
  for (auto itr = str.begin(); itr != str.end(); itr++) {
    if (97 <= *itr && *itr <= 122) {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str) {
  for (auto itr = str.begin(); itr != str.end(); itr++) {
    if (65 <= *itr && *itr <= 90) {
      *itr = *itr + 32;
    }
  }
  return str;
}

Combi::Combi() {
  n_num = -1;
  k_num = -1;
};

ll Combi::Combination(ll n, ll k) {

  ll ret;
  if (memo[n][k] != 0) {
    ret = memo[n][k];
  } else if (n == k || k == 0) {
    memo[n][k] = 1;
    ret = 1;
  } else {
    ret = Combination(n - 1, k - 1) + Combination(n - 1, k);
    memo[n][k] = ret;
  }
  return ret;
}

long long Combi::nPk_modp(long long n, long long k, long long p) {
  ll ans = 1;
  for (long long i = k; i <= n; i++) {
    ans = (ans * i) % p;
  }

  return ans;
};

vector<pair<long long, long long>> prime_factorize(long long p) {
  vector<pair<long long, long long>> ret;

  for (long long x = 2; x * x <= p; ++x) {
    if (p % x != 0)
      continue;
    long long num = 0;
    while (p % x == 0) {
      num++;
      p /= x;
    }
    ret.push_back(make_pair(x, num));
  }

  if (p != 1)
    ret.push_back(make_pair(p, 1));

  return ret;
}

vector<pair<char, long long>> runLengthEncoding(string s) {
  vector<pair<char, long long>> ret;

  for (long long i = 0; i < s.length(); i++) {
    char cur = s[i];
    long long cnt = 1;

    for (long long j = i + 1; j < s.length(); j++) {
      if (cur != s[j])
        break;

      cnt++;
    }

    ret.emplace_back(make_pair(cur, cnt));

    i += cnt - 1;
  }

  return ret;
}