Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define X first
#define Y second
#define REP(i, a) for (int i = 0; i < a; ++i)
#define REPP(i, a, b) for (int i = a; i < b; ++i)
#define FILL(a, x) memset(a, x, sizeof(a))
#define foreach(gg, itit)                                                      \
  for (typeof(gg.begin()) itit = gg.begin(); itit != gg.end(); itit++)
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define present(c, x) ((c).find(x) != (c).end())
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const ll INF = 1e18;

// #define DEBUG
ll power(ll x, ll y) {
  ll t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}
#ifdef DEBUG
#define dprintf(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
#else
#define dprintf(fmt, ...)
#endif

pii iv[N];
int MX = 800;
int pl[N];
int ans[N];
int tp[N], tp1[N];
int main() {
  int n, m;
  scanf("%d%d", &m, &n);
  REP(i, m) { scanf("%d%d", &iv[i].X, &iv[i].Y); }
  REPP(i, 1, MX) { REP(j, m) if (iv[j].Y / i != (iv[j].X - 1) / i) ans[i]++; }
  REP(i, m) {
    int ml = (iv[i].X - 1) / MX;
    tp[0] = tp1[0] = n + 1;
    REPP(j, 1, ml + 2) {
      tp[j] = (iv[i].X - 1) / j;
      tp1[j] = (iv[i].Y) / j;
    }
    REP(j, ml + 1) {
      pii z = mp(max(tp[j + 1], tp1[j + 1]) + 1, min(tp[j], tp1[j]));
      if (z.X <= z.Y) {
        pl[z.X]++;
        pl[z.Y + 1]--;
      }
      // printf("%d %d %d %d %d\n",iv[i].X,iv[i].Y,j,z.X,z.Y);
    }
  }
  int x = 0;
  REP(i, n + 1) {
    x += pl[i];
    if (i >= MX) {
      ans[i] = m - x;
    }
  }
  REPP(i, 1, n + 1) printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define X first
#define Y second
#define REP(i, a) for (int i = 0; i < a; ++i)
#define REPP(i, a, b) for (int i = a; i < b; ++i)
#define FILL(a, x) memset(a, x, sizeof(a))
#define foreach(gg, itit)                                                      \
  for (typeof(gg.begin()) itit = gg.begin(); itit != gg.end(); itit++)
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define present(c, x) ((c).find(x) != (c).end())
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const ll INF = 1e18;

// #define DEBUG
ll power(ll x, ll y) {
  ll t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}
#ifdef DEBUG
#define dprintf(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
#else
#define dprintf(fmt, ...)
#endif

pii iv[N];
int MX = 300;
int pl[N];
int ans[N];
int tp[N], tp1[N];
int main() {
  int n, m;
  scanf("%d%d", &m, &n);
  REP(i, m) { scanf("%d%d", &iv[i].X, &iv[i].Y); }
  REPP(i, 1, MX) { REP(j, m) if (iv[j].Y / i != (iv[j].X - 1) / i) ans[i]++; }
  REP(i, m) {
    int ml = (iv[i].X - 1) / MX;
    tp[0] = tp1[0] = n + 1;
    REPP(j, 1, ml + 2) {
      tp[j] = (iv[i].X - 1) / j;
      tp1[j] = (iv[i].Y) / j;
    }
    REP(j, ml + 1) {
      pii z = mp(max(tp[j + 1], tp1[j + 1]) + 1, min(tp[j], tp1[j]));
      if (z.X <= z.Y) {
        pl[z.X]++;
        pl[z.Y + 1]--;
      }
      // printf("%d %d %d %d %d\n",iv[i].X,iv[i].Y,j,z.X,z.Y);
    }
  }
  int x = 0;
  REP(i, n + 1) {
    x += pl[i];
    if (i >= MX) {
      ans[i] = m - x;
    }
  }
  REPP(i, 1, n + 1) printf("%d\n", ans[i]);
  return 0;
}