Problem p03819 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

#define ld long double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--Lazy Evaluation Segment tree (Proportional)
//-------------------------------------------------
template <typename T, typename E> class LazySegmentTree {
private:
  using F = ::std::function<T(T, T)>;
  using G = ::std::function<T(T, E)>;
  using H = ::std::function<E(E, E)>;
  F f;
  G g;
  H h;
  int n, height;
  ::std::vector<T> seg;
  ::std::vector<E> lazy;
  T te;
  E ee;

public:
  LazySegmentTree(int _n, F f, G g, H h, T te, E ee)
      : f(f), g(g), h(h), te(te), ee(ee) {
    init(_n);
  }
  LazySegmentTree(::std::vector<T> &v, F f, G g, H h, T te, E ee)
      : f(f), g(g), h(h), te(te), ee(ee) {
    int _n = v.size();
    init(_n);
    for (int i = n; i < n + _n; i++)
      seg[i] = v[i - n];
    build();
  }
  void init(int _n) {
    n = 1;
    height = 0;
    while (n < _n)
      n <<= 1, height++;
    seg.resize(2 * n, te);
    lazy.resize(2 * n, ee);
  }
  void build() {
    for (int i = n - 1; i > 0; i--)
      seg[i] = f(seg[i << 1], seg[i << 1 | 1]);
  }
  T eval(int k) {
    int c = breadth(k);
    return (lazy[k] == ee) ? seg[k] : g(seg[k], quantify(lazy[k], c));
  }
  void waveto(int k) {
    for (int i = height; i > 0; i--)
      wave(k >> i);
  }
  void wave(int k) {
    seg[k] = eval(k);
    lazy[k << 1] = h(lazy[k << 1], lazy[k]);
    lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
    lazy[k] = ee;
  }
  void recalc(int k) {
    for (int i = 1; i <= height; i++) {
      int m = k >> i;
      seg[m] = f(eval(m << 1), eval(m << 1 | 1));
    }
  }
  void update(int a, int b, E x) {
    if (b <= a)
      return;
    waveto(a += n);
    waveto(b += n - 1);
    T L = ee, R = ee;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (!(r & 1))
        lazy[r] = h(lazy[r], x), r--;
      l >>= 1;
      r >>= 1;
    }
    recalc(a);
    recalc(b);
  }
  T query(int a, int b) {
    waveto(a += n);
    waveto(b += n - 1);
    T L = te, R = te;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        L = f(L, eval(l++));
      if (!(r & 1))
        R = f(eval(r--), R);
      l >>= 1;
      r >>= 1;
    }
    return f(L, R);
  }
  T operator[](int k) { return query(k, k + 1); };
  inline int breadth(int k) {
    int ret = height - 31 + __builtin_clz(k);
    return 1 << ret;
  }
  // return value proportional to segment's width
  inline E quantify(E laz, int c) { return c * laz; }
};

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  auto cmp = [](pi x, pi y) {
    int d1 = x.se - x.fi + 1;
    int d2 = y.se - y.fi + 1;
    return d1 < d2;
  };
  using P = tuple<int, int, int>;
  vector<P> lr(n);
  rep(i, n) {
    int l, r;
    cin >> l >> r;
    lr[i] = make_tuple(r - l + 1, l, r);
  }
  sort(all(lr));
  // for(auto e:lr){
  //     cout<<e.fi<<", "<<e.se<<endl;
  // }
  LazySegmentTree<int, int> seg(
      m + 1, [](int x, int y) { return x + y; },
      [](int x, int y) { return x + y; }, [](int x, int y) { return x + y; }, 0,
      0);
  int idx = 0;
  FOR(i, 1, m) {
    int ans = 0;
    ans += lr.end() - lower_bound(all(lr), make_tuple(i, 0, 0));
    while (idx < m && get<0>(lr[idx]) < i) {
      seg.update(get<1>(lr[idx]), get<2>(lr[idx]) + 1, 1);
      idx++;
    }
    for (int j = i; j <= m; j += i) {
      ans += seg[j];
    }
    cout << ans << "\n";
  }
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

#define ld long double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--Lazy Evaluation Segment tree (Proportional)
//-------------------------------------------------
template <typename T, typename E> class LazySegmentTree {
private:
  using F = ::std::function<T(T, T)>;
  using G = ::std::function<T(T, E)>;
  using H = ::std::function<E(E, E)>;
  F f;
  G g;
  H h;
  int n, height;
  ::std::vector<T> seg;
  ::std::vector<E> lazy;
  T te;
  E ee;

public:
  LazySegmentTree(int _n, F f, G g, H h, T te, E ee)
      : f(f), g(g), h(h), te(te), ee(ee) {
    init(_n);
  }
  LazySegmentTree(::std::vector<T> &v, F f, G g, H h, T te, E ee)
      : f(f), g(g), h(h), te(te), ee(ee) {
    int _n = v.size();
    init(_n);
    for (int i = n; i < n + _n; i++)
      seg[i] = v[i - n];
    build();
  }
  void init(int _n) {
    n = 1;
    height = 0;
    while (n < _n)
      n <<= 1, height++;
    seg.resize(2 * n, te);
    lazy.resize(2 * n, ee);
  }
  void build() {
    for (int i = n - 1; i > 0; i--)
      seg[i] = f(seg[i << 1], seg[i << 1 | 1]);
  }
  T eval(int k) {
    int c = breadth(k);
    return (lazy[k] == ee) ? seg[k] : g(seg[k], quantify(lazy[k], c));
  }
  void waveto(int k) {
    for (int i = height; i > 0; i--)
      wave(k >> i);
  }
  void wave(int k) {
    seg[k] = eval(k);
    lazy[k << 1] = h(lazy[k << 1], lazy[k]);
    lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
    lazy[k] = ee;
  }
  void recalc(int k) {
    for (int i = 1; i <= height; i++) {
      int m = k >> i;
      seg[m] = f(eval(m << 1), eval(m << 1 | 1));
    }
  }
  void update(int a, int b, E x) {
    if (b <= a)
      return;
    waveto(a += n);
    waveto(b += n - 1);
    T L = ee, R = ee;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (!(r & 1))
        lazy[r] = h(lazy[r], x), r--;
      l >>= 1;
      r >>= 1;
    }
    recalc(a);
    recalc(b);
  }
  T query(int a, int b) {
    waveto(a += n);
    waveto(b += n - 1);
    T L = te, R = te;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        L = f(L, eval(l++));
      if (!(r & 1))
        R = f(eval(r--), R);
      l >>= 1;
      r >>= 1;
    }
    return f(L, R);
  }
  T operator[](int k) { return query(k, k + 1); };
  inline int breadth(int k) {
    int ret = height - 31 + __builtin_clz(k);
    return 1 << ret;
  }
  // return value proportional to segment's width
  inline E quantify(E laz, int c) { return c * laz; }
};

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  auto cmp = [](pi x, pi y) {
    int d1 = x.se - x.fi + 1;
    int d2 = y.se - y.fi + 1;
    return d1 < d2;
  };
  using P = tuple<int, int, int>;
  vector<P> lr(n);
  rep(i, n) {
    int l, r;
    cin >> l >> r;
    lr[i] = make_tuple(r - l + 1, l, r);
  }
  sort(all(lr));
  // for(auto e:lr){
  //     cout<<e.fi<<", "<<e.se<<endl;
  // }
  LazySegmentTree<int, int> seg(
      m + 1, [](int x, int y) { return x + y; },
      [](int x, int y) { return x + y; }, [](int x, int y) { return x + y; }, 0,
      0);
  int idx = 0;
  FOR(i, 1, m) {
    int ans = 0;
    ans += lr.end() - lower_bound(all(lr), make_tuple(i, 0, 0));
    while (idx < n && get<0>(lr[idx]) < i) {
      seg.update(get<1>(lr[idx]), get<2>(lr[idx]) + 1, 1);
      idx++;
    }
    for (int j = i; j <= m; j += i) {
      ans += seg[j];
    }
    cout << ans << "\n";
  }
  return 0;
}