Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define pw(x) (1LL << (x))
static const int fast_io = []() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
static const int precise_doubles = []() {
  cout << fixed << setprecision(20);
  return 0;
}();
typedef pair<int, int> PII;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// find_by_order(k) - iterator to kth largest, order_of_key(k) - #things < k
typedef long double LD;
typedef long long LL;
#define EVAL(x) x
#define SPA(x) EVAL({ cout << #x " = " << x << ' '; })
#define NEWL EVAL({ cout << '\n'; })
#define SPAR(a, b)                                                             \
  EVAL({                                                                       \
    cout << #a " = ";                                                          \
    for (auto it = a; it != b;)                                                \
      cout << *(it++) << " ";                                                  \
    cout << '\n';                                                              \
  })
#define SPAV(v)                                                                \
  EVAL({                                                                       \
    cout << #v " = ";                                                          \
    for (auto it = v.begin(); it != v.end();)                                  \
      cout << *(it++) << " ";                                                  \
    cout << '\n';                                                              \
  })
const int INF = 0x3f3f3f3f;           // 1.0e9
const LL LINF = 0x3f3f3f3f3f3f3f3fll; // 4.5e18
const LD eps = 1e-20;
const int mod = 3;
inline int msum(int x, int y) { return (x + y < mod ? x + y : x + y - mod); }
inline int mdif(int x, int y) { return (x >= y ? x - y : x - y + mod); }
inline int mprod(int x, int y) { return (1ll * x * y) % mod; }
inline int mpow(int x, LL y) {
  LL r = 1;
  while (y) {
    if (y & 1)
      r = mprod(r, x);
    x = mprod(x, x);
    y >>= 1;
  }
  return r;
}
inline int minv(int x) { return mpow(x, mod - 2); }
#define DIE                                                                    \
  EVAL({                                                                       \
    cout << -1 << '\n';                                                        \
    exit(0);                                                                   \
  })
//------------------------------------------------------------------------------------------------------

const int N = 3e5 + 10;
const int M = 1e5 + 10;

int ans[M];
int n, m;
struct Interval {
  int l, r, len;
};
vector<Interval> intervals;

const int base = 1 << 17;
int seg[base << 1];

void update(int pos, int val) {
  for (int i = base + pos; i >= 1; i >>= 1)
    seg[i] += val;
}
int query(int l, int r, int u = 1, int L = 0, int R = base - 1) {
  if (l == L and r == R)
    return seg[u];
  int mid = (L + R) >> 1;
  if (r <= mid)
    return query(l, r, u << 1, L, mid);
  if (l > mid)
    return query(l, r, u << 1 | 1, mid + 1, R);
  return query(l, mid, u << 1, L, mid) +
         query(mid + 1, r, u << 1 | 1, mid + 1, R);
}

int main() {
  cin >> n >> m;
  REP(i, n) {
    int l, r;
    cin >> l >> r;
    intervals.push_back({l, r, r - l + 1});
  }
  sort(
      intervals.begin(), intervals.end(),
      [&](const Interval &i1, const Interval &i2) { return i1.len <= i2.len; });
  int nex = 0;
  FOR(d, 1, m + 1) {
    while (nex < (int)intervals.size() && intervals[nex].len < d) {
      update(intervals[nex].l, 1);
      update(intervals[nex].r + 1, -1);
      nex++;
    }
    ans[d] = n - nex;
    for (int x = d; x <= m; x += d) {
      ans[d] += query(0, x);
    }
  }
  FOR(d, 1, m + 1) cout << ans[d] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define pw(x) (1LL << (x))
static const int fast_io = []() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
static const int precise_doubles = []() {
  cout << fixed << setprecision(20);
  return 0;
}();
typedef pair<int, int> PII;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// find_by_order(k) - iterator to kth largest, order_of_key(k) - #things < k
typedef long double LD;
typedef long long LL;
#define EVAL(x) x
#define SPA(x) EVAL({ cout << #x " = " << x << ' '; })
#define NEWL EVAL({ cout << '\n'; })
#define SPAR(a, b)                                                             \
  EVAL({                                                                       \
    cout << #a " = ";                                                          \
    for (auto it = a; it != b;)                                                \
      cout << *(it++) << " ";                                                  \
    cout << '\n';                                                              \
  })
#define SPAV(v)                                                                \
  EVAL({                                                                       \
    cout << #v " = ";                                                          \
    for (auto it = v.begin(); it != v.end();)                                  \
      cout << *(it++) << " ";                                                  \
    cout << '\n';                                                              \
  })
const int INF = 0x3f3f3f3f;           // 1.0e9
const LL LINF = 0x3f3f3f3f3f3f3f3fll; // 4.5e18
const LD eps = 1e-20;
const int mod = 3;
inline int msum(int x, int y) { return (x + y < mod ? x + y : x + y - mod); }
inline int mdif(int x, int y) { return (x >= y ? x - y : x - y + mod); }
inline int mprod(int x, int y) { return (1ll * x * y) % mod; }
inline int mpow(int x, LL y) {
  LL r = 1;
  while (y) {
    if (y & 1)
      r = mprod(r, x);
    x = mprod(x, x);
    y >>= 1;
  }
  return r;
}
inline int minv(int x) { return mpow(x, mod - 2); }
#define DIE                                                                    \
  EVAL({                                                                       \
    cout << -1 << '\n';                                                        \
    exit(0);                                                                   \
  })
//------------------------------------------------------------------------------------------------------

const int N = 3e5 + 10;
const int M = 1e5 + 10;

int ans[M];
int n, m;
struct Interval {
  int l, r, len;
};
vector<Interval> intervals;

const int base = 1 << 17;
int seg[base << 1];

void update(int pos, int val) {
  for (int i = base + pos; i >= 1; i >>= 1)
    seg[i] += val;
}
int query(int l, int r, int u = 1, int L = 0, int R = base - 1) {
  if (l == L and r == R)
    return seg[u];
  int mid = (L + R) >> 1;
  if (r <= mid)
    return query(l, r, u << 1, L, mid);
  if (l > mid)
    return query(l, r, u << 1 | 1, mid + 1, R);
  return query(l, mid, u << 1, L, mid) +
         query(mid + 1, r, u << 1 | 1, mid + 1, R);
}

int main() {
  cin >> n >> m;
  REP(i, n) {
    int l, r;
    cin >> l >> r;
    intervals.push_back({l, r, r - l + 1});
  }
  sort(intervals.begin(), intervals.end(),
       [&](const Interval &i1, const Interval &i2) { return i1.len < i2.len; });
  int nex = 0;
  FOR(d, 1, m + 1) {
    while (nex < (int)intervals.size() && intervals[nex].len < d) {
      update(intervals[nex].l, 1);
      update(intervals[nex].r + 1, -1);
      nex++;
    }
    ans[d] = n - nex;
    for (int x = d; x <= m; x += d) {
      ans[d] += query(0, x);
    }
  }
  FOR(d, 1, m + 1) cout << ans[d] << '\n';
}