alexjercan/bugnet huggingface dataset:
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30
#define MAX_P 1000000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int p[MAX_P], n, sum[MAX_P];

int main() {
  p[0] = p[1] = 1;
  REP(i, 2, MAX_P) {
    sum[i] = sum[i - 1];
    if (!p[i]) {
      sum[i]++;
      for (int j = i + i; j < MAX_P; j += i)
        p[j] = 1;
    }
  }
  //  rep(i, 100) printf("%d ", sum[i]); puts("");

  while (scanf("%d", &n) && n) {
    int ip, m, res = 0;
    rep(i, n) {
      scanf("%d%d", &ip, &m);
      //      int c = 0;
      //      REP(j, (ip-m)>0?(ip-m):0, (ip+m+1)>MAX_P?MAX_P:(ip+m+1)) if(!p[j])
      //      c++;
      int top = (ip + m) > MAX_P - 1 ? MAX_P - 1 : (ip + m);
      int under = (ip - m - 1) > 0 ? (ip - m - 1) : 0;
      int c = sum[top] - sum[under];
      if (c)
        res += c - 1;
      else
        res--;
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30
#define MAX_P 1000000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int p[MAX_P], n;

int main() {
  p[0] = p[1] = 1;
  REP(i, 2, MAX_P / 2) {
    if (!p[i]) {
      for (int j = i + i; j < MAX_P; j += i)
        p[j] = 1;
    }
  }
  //  rep(i, 100) if(!p[i])printf("%d ", i); puts("");

  while (scanf("%d", &n) && n) {
    int ip, m, res = 0;
    rep(i, n) {
      scanf("%d%d", &ip, &m);
      int c = 0;
      REP(j, (ip - m) > 0 ? (ip - m) : 0,
          (ip + m + 1) > MAX_P ? MAX_P : (ip + m + 1))
      if (!p[j]) c++;
      if (c)
        res += c - 1;
      else
        res--;
    }
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional> //greaterの使用
#include <iostream>
using namespace std;
#define MAX_LIST 999983 + 100
#define MP 999983

int main() {
  bool prime[MAX_LIST];
  for (int i = 0; i < MAX_LIST; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 0; i * i < MAX_LIST; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < MAX_LIST; j = j + i) {
        prime[j] = 0;
      }
    }
  }
  for (int i = MP + 1; i < MAX_LIST; i++) {
    prime[i] = 0;
  }
  int prime_num[MAX_LIST];
  prime_num[0] = 1;
  for (int i = 1; i < MAX_LIST; i++) {
    prime_num[i] = prime_num[i - 1] + prime[i];
  }

  int pair_num;
  while (cin >> pair_num, pair_num) {
    int ans = 0;
    for (int i = 0; i < pair_num; i++) {
      int p, m;
      cin >> p;
      cin >> m;
      int x = 0;
      int a, b;
      if (p + m > MP) {
        a = MP;
      } else {
        a = p + m;
      }
      if (p - m < 0) {
        b = 0;
      } else {
        b = p - m;
      }
      x = prime_num[a] - prime_num[b] + prime[b];

      if (x == 0) {
        ans--;
      } else {
        x--;
        ans = ans + x;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <functional> //greaterの使用
#include <iostream>
using namespace std;
#define MAX_LIST 999983 + 100
#define MP 999983

int main() {
  bool prime[MAX_LIST];
  for (int i = 0; i < MAX_LIST; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 0; i * i < MAX_LIST; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < MAX_LIST; j = j + i) {
        prime[j] = 0;
      }
    }
  }
  for (int i = MP + 1; i < MAX_LIST; i++) {
    prime[i] = 0;
  }

  int prime_num[MAX_LIST];
  prime_num[0] = 1;
  for (int i = 1; i < MP; i++) {
    prime_num[i] = prime_num[i - 1] + prime[i];
  }

  int pair_num;
  while (cin >> pair_num, pair_num) {
    int ans = 0;
    for (; pair_num--;) {
      int p, m;
      cin >> p;
      cin >> m;
      int x = 0;
      if (p + m > MP) {
        x = prime_num[MP] - prime_num[p - m];
      } else if (p - m < 0) {
        x = prime_num[p + m] - prime_num[0];
      } else {
        x = prime_num[p + m] - prime_num[p - m];
      }

      if (x == 0) {
        ans--;
      } else {
        x--;
        ans = ans + x;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

int pos[1000001], p[100000], size;

bool isPrime(int x) {
  for (int i = 0; i < size && p[i] <= sqrt(x); i++) {
    if (x % p[i] == 0)
      return false;
  }
  return true;
}

void mkPrime(void) {
  memset(pos, -1, sizeof(pos));
  p[0] = 2;
  pos[0] = pos[1] = pos[2] = 0;
  size = 1;

  for (int i = 3; i <= 999983; i++) {
    if (isPrime(i)) {
      p[size] = i;
      for (int j = i; pos[j] == -1; j--)
        pos[j] = size;
      size++;
    }
  }

  for (int i = 999984; i <= 1000000; i++) {
    pos[i] = size;
  }
}

int calX(int from, int to) {
  int res = 0;
  for (int i = pos[from]; i < size && p[i] <= to; i++, res++)
    ;
  return res;
}

int main(void) {
  int n;

  mkPrime();

  while (scanf("%d", &n) && n) {
    int ans = 0;
    while (n--) {
      int p, m;
      scanf("%d%d", &p, &m);
      int count =
          calX(p - m < 0 ? 0 : p - m, p + m > 1000000 ? 1000000 : p + m);
      if (count > 0)
        ans += count - 1;
      else if (count == 0)
        ans--;
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

int count[1000001];
int size;
char flg[1000001];

void mkPrime(void) {
  memset(flg, 1, sizeof(flg));
  flg[0] = flg[1] = 0;

  for (int i = 2; i <= 999983; i++) {
    if (flg[i]) {
      for (int j = i * 2; j <= 999983; j += i) {
        flg[j] = 0;
      }
    }
  }
  for (int i = 999984; i <= 1000000; i++) {
    flg[i] = 0;
  }
}

int calX(int from, int to) {
  int res = 0;
  for (int i = from; i <= to; i++) {
    res += flg[i];
  }
  return res;
}

int main(void) {
  int n;

  mkPrime();

  while (cin >> n && n) {
    int minP = 1000000;
    int maxP = 0;

    memset(count, -1, sizeof(count));
    while (n--) {
      int p, m;
      cin >> p >> m;
      if (minP > p)
        minP = p;
      if (maxP < p)
        maxP = p;
      if (count[p] == -1)
        count[p] = 0;
      count[p] +=
          calX(p - m < 0 ? 0 : p - m, p + m > 1000000 ? 1000000 : p + m);
    }

    int ans = 0;
    for (int i = minP; i <= maxP; i++) {
      if (count[i] > 0)
        ans += count[i] - 1;
      else if (count[i] == 0)
        ans--;
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

const int MAX = 999984;
bool isPrime[MAX];

void createIsPrime() {
  fill_n(isPrime, MAX, true);
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 4; i < MAX; i += 2) {
    isPrime[i] = false;
  }
  for (int i = 3; i * i <= MAX; i += 2) {
    if (isPrime[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        isPrime[j] = false;
      }
    }
  }
}

int main() {
  createIsPrime();
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int bill = 0;
    while (n--) {
      int p, m;
      cin >> p >> m;
      int prime = 0, start = p - m;
      if ((p - m) % 2 == 0) {
        start++;
      }
      if (p - m <= 2) {
        bill++;
      }
      for (int i = max(start, 3), end = min(p + m, MAX - 1); i <= end; i += 2) {
        if (isPrime[i]) {
          prime++;
        }
      }
      if (prime > 0) {
        bill += prime - 1;
      } else {
        bill--;
      }
    }
    cout << bill << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

const int MAX = 999984;
bool isPrime[MAX];

void createIsPrime() {
  fill_n(isPrime, MAX, true);
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 4; i < MAX; i += 2) {
    isPrime[i] = false;
  }
  for (int i = 3; i * i <= MAX; i += 2) {
    if (isPrime[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        isPrime[j] = false;
      }
    }
  }
}

int main() {
  createIsPrime();
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int bill = 0;
    while (n--) {
      int p, m;
      cin >> p >> m;
      int prime = 0;
      for (int i = max(p - m, 2), end = min(p + m, MAX - 1); i <= end; i++) {
        if (isPrime[i]) {
          prime++;
        }
      }
      if (prime > 0) {
        bill += prime - 1;
      } else {
        bill--;
      }
    }
    cout << bill << endl;
  }
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int furui[999984];

int main() {
  furui[0] = furui[1] = 1;
  for (int i = 2; i * i < 999984; i++) {
    if (furui[i] == 0) {
      for (int j = i; j <= 999983 / i; j++) {
        furui[i * j] = 1;
      }
    }
  }
  int n;
  int a, b, p, m;
  int counter;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    counter = 0;
    for (int i = 0; i < n; i++) {
      cin >> p >> m;
      a = p - m;
      if (a <= 2) {
        a = 3;
        counter++;
      }
      if (a % 2 == 0) {
        a++;
      }
      b = p + m;
      if (b > 999983) {
        b = 999983;
      }
      for (int j = a; j <= b; j += 2) {
        if (furui[j] == 0) {
          counter++;
        }
      }
      counter--;
    }
    cout << counter << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int furui[999984];

int main() {
  furui[0] = furui[1] = 1;
  for (int i = 2; i * i < 999984; i++) {
    if (furui[i] == 0) {
      for (int j = i; j <= 999983 / i; j++) {
        furui[i * j] = 1;
      }
    }
  }
  int n;
  int a, b, p, m;
  int counter;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    counter = 0;
    for (int i = 0; i < n; i++) {
      cin >> p >> m;
      a = p - m;
      if (a < 2) {
        a = 3;
        counter++;
      }
      b = p + m;
      if (b > 999983) {
        b = 999983;
      }
      for (int j = a; j <= b; j++) {
        if (j % 2 != 0 && furui[j] == 0) {
          counter++;
        }
      }
      counter--;
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; ++i)
#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define P pair<int, int>
const int N = 1000000;
const int RATE = 9973;

int pr[N];

void prime() {
  REP(i, N) pr[i] = 1;
  pr[0] = 0;
  pr[1] = 0;
  for (int i = 2; i < N; ++i) {
    if (!pr[i])
      continue;
    for (int j = i + i; j < N; j += i)
      pr[j] = 0;
  }
  FOR(i, 1, N) pr[i] += pr[i - 1];
}

int main() {
  prime();
  int n;
  while (cin >> n && n) {
    // map<int, int> m;
    int ans = 0;
    REP(i, n) {
      int p, m;
      cin >> p >> m;
      // m[p] += (pr[min(N - 1, p + m)] - pr[max(0, p - m - 1)]);
      int tmp = (pr[min(N - 1, p + m)] - pr[max(0, p - m - 1)]);
      if (tmp == 0)
        --ans;
      else
        ans += (tmp - 1);
    }
    cout << ans << endl;
    // int ans = 0;
    // for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it){
    //   if((*it).second == 0) --ans;
    //   else ans += ((*it).second - 1);
    // }
    // cout <<max(0, ans) <<endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define N 1000000
using namespace std;

bool li[N + 1];

void calpri();
int calx(int p, int m);
int money(int x);

int main() {
  calpri();
  cout << "ok" << endl;
  int n, p, m, x, summoney;
  while (1) {
    cin >> n; // IÊÌüÍ
    if (n == 0)
      break;
    summoney = 0;                 // nª0Åbreak
    for (int i = 0; i < n; i++) { // nÂÌIÊÌîñ
      cin >> p >> m;
      x = calx(p, m);
      summoney += money(x);
    }
    cout << summoney << endl;
  }
  return 0;
}

void calpri() { // GgXelXÌâ¿Éæèboolì
  int max = N, min = 2; // iªf=true fÅÍÈ¢=false
  li[0] = false;
  li[1] = false;

  for (int i = 2; i <= max; i++) {
    li[i] = 1;
  }

  for (int i = 2; (i * i) <= max;) {
    for (int j = (i + 1); j <= max; j++) {
      if (j % i == 0 && li[j] != false) {
        li[j] = false;
      }
      while (!li[max]) {
        max--;
      }
    }
    do {
      i++;
      if (i > max)
        break;
    } while (!li[i]);
  }
}

int calx(int p, int m) { // zñliÌtrueÌ©çfJEg
  int nop = 0, max, min;
  if ((p + m) > (N + 1))
    max = N;
  else
    max = p + m;
  if (p - m < 0)
    min = 0;
  else
    min = p - m;
  for (int i = min; i <= max;) {
    if (li[i]) {
      nop++;
    }
    do {
      i++;
      if (i > N)
        break;
    } while (!li[i]);
  }
  return nop;
}

int money(int x) { // ÜàvZ
  if (x == 0)
    return -1;
  else
    return (x - 1);
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<double> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int sosu[1000001] = {1, 1, 0};
int main() {
  for (int i = 2; i * i <= 1000001; i++)
    if (sosu[i] == 0)
      for (int j = i * 2; j < 1000001; j += i)
        sosu[j] = true;
  rep(i, 1000001) sosu[i] ^= 1;
  rep(i, 1000000) sosu[i + 1] += sosu[i];

  int n;
  while (cin >> n, n) {
    int out = 0;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      int s = max(0, a - b - 1), t = min(a + b, 1000000);
      int co = sosu[t] - sosu[s];
      if (co == 0)
        out--;
      else
        out += co - 1;
    }
    cout << out << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
#define MAX 1000100
  bool num[MAX] = {1, 1, 0};
  for (int i = 3; i < MAX; i += 2) {
    if (num[i])
      continue;
    for (int j = i * 3; j < MAX; j += i * 2) {
      num[j] = 1;
    }
  }
  num[1000003] = 1;

  int n;
  while (cin >> n, n) {
    int sum = 0;
    rep(i, n) {
      int a, b;
      int co = 0;
      cin >> a >> b;
      if (a - b == 2)
        co++;
      for (int j = a - b + (a - b + 1) % 2; j < a + b + 1; j += 2) {
        if (j < 2)
          continue;
        if (j > 1000000)
          break;
        if (a - b == 2)
          co++;
        if (j > 2 && !num[j])
          co++;
      }
      // cout<<co<<endl;
      if (co)
        sum += co - 1;
    }
    cout << sum << endl;
  }
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;

int prime[1000000];
bool is_prime[1000001];

void sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

int d[1000001];

int main() {
  int n, p, m;
  sieve(1000000);
  rep(i, 1, 1000001) {
    d[i] = d[i - 1];
    if (is_prime[i])
      d[i]++;
  }
  while (1) {
    int ans = 0;
    cin >> n;
    if (n == 0)
      break;
    rep(i, 0, n) {
      cin >> p >> m;
      int c = 0;
      c = d[min(p + m, 1000000)] - d[max(1, p - m - 1)];
      // o(c);
      if (c == 0)
        ans--;
      else
        ans += c - 1;
    }
    o(ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;

bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

int main() {
  int n, p, m;
  // sieve(1000000);
  while (1) {
    int ans = 0;
    cin >> n;
    if (n == 0)
      break;
    rep(i, 0, n) {
      cin >> p >> m;
      int c = 0;
      rer(j, max(1, p - m), min(p + m, 1000000)) {
        if (is_prime(j)) {
          c++; // o(j);
        }
      }
      // o(c);
      if (c == 0)
        ans--;
      else
        ans += c - 1;
    }
    o(ans);
  }
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

#define MAX 1000000

using namespace std;

int eratos[MAX];
int primes[MAX];
int max_primes;

void MakePrimes() {
  int n = 0;
  eratos[0] = 1;
  eratos[1] = 1;
  eratos[2] = 0;
  for (int i = 2; i < MAX; ++i) {
    if (eratos[i] == 0) {
      for (int j = 2; i * j < MAX; ++j) {
        eratos[i * j] = 1;
      }
      primes[n++] = i;
    }
  }
  primes[n++] = 1000003;
  max_primes = n;
}
int bin_search_prime(int n) {
  int r = max_primes - 1, l = 0, m;

  while (r > l + 1) {
    m = (r + l) / 2;
    if (primes[m] > n)
      r = m;
    else if (primes[m] < n)
      l = m;
    else
      return m;
  }
  return l;
}

int main() {
  MakePrimes();
  while (true) {
    int n;
    int ans = 0;

    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < n; ++i) {
      int X = 0;
      int p, m;
      scanf("%d%d", &p, &m);

      int l = bin_search_prime(std::max(0, p - m));
      int r = bin_search_prime(std::min(MAX - 1, p + m));

      if (primes[l] < p - m)
        ++l;
      X = (r - l) + 1;

      ans += X - 1;
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

#define MAX 1000000

int eratos[MAX];

void MakePrimes() {
  eratos[0] = 1;
  eratos[1] = 1;
  eratos[2] = 0;
  for (int i = 2; i < MAX; ++i) {
    if (eratos[i] == 0) {
      for (int j = 2; i * j < MAX; ++j) {
        eratos[i * j] = 1;
      }
    }
  }
}
int CountPrime(int a, int b) {
  int ret = 0;
  a = std::max(0, a);
  b = std::min(MAX - 1, b);
  for (int i = a; i <= b; ++i) {
    if (eratos[i] == 0)
      ++ret;
  }
  return ret;
}

int main() {
  MakePrimes();
  while (true) {
    int n;
    int ans = 0;

    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < n; ++i) {
      int X = 0;
      int p, m;
      scanf("%d%d", &p, &m);

      X = CountPrime(p - m, p + m);
      ans += X - 1;
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define USE_MATH_DEFINES
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

bool prime[2000000];
int cnt[2000000];

int main() {
  fill(prime, prime + 2000000, true);
  prime[0] = prime[1] = false;
  for (int i = 2; i * i < 2000000; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < 2000000; j += i)
        prime[j] = false;
    }
  }
  int c = 0;
  rep(i, 2000000) {
    if (prime[i])
      c++;
    cnt[i] = c;
  }

  int n, x, p, m, a, b;
  while (cin >> n && n) {
    x = 0;
    rep(i, n) {
      cin >> p >> m;
      x--;
      a = max(0, p - m - 1);
      b = min(999983, p + m);
      x += cnt[b] - cnt[a];
    }
    cout << x << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define USE_MATH_DEFINES
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

bool prime[2000000];

int main() {
  fill(prime, prime + 2000000, true);
  prime[0] = prime[1] = false;
  for (int i = 2; i * i < 2000000; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j < 2000000; j += i)
        prime[j] = false;
    }
  }

  int n, x, p, m;
  while (cin >> n && n) {
    x = 0;
    rep(i, n) {
      cin >> p >> m;
      x--;
      rep2(j, p - m, p + m + 1) {
        if (0 <= j && j <= 999983 && prime[j])
          x++;
      }
    }
    cout << x << endl;
  }
  return 0;
}