Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> pa) {
  return os << "[" << pa.first << ", " << pa.second << "]";
}

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "}";
  return os;
}

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug " << i << std::endl

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

bool comp(const P &a, const P &b) {
  return (a.second - a.first) < (b.second - b.first);
}

// とりあえず1-indexedでつくったBIT
class BinaryIndexedTree {
private:
  long long n;
  std::vector<long long> bit;

public:
  BinaryIndexedTree(long long i) : n(i), bit(i + 1, 0){};

  // a番目にwを加算
  void add(long long a, long long w) {
    for (long long x = a; x <= n; x += (x & -x)) {
      bit[x] += w;
    }
  }

  //[1,a]の和
  long long sum(long long a) {
    long long ret = 0;
    for (long long x = a; x > 0; x -= (x & -x)) {
      ret += bit[x];
    }
    return ret;
  }

  // 各項がすべて非負のものに対して、sum[1,a]>=keyを満たす最小のaを返す
  // 存在しないなら-1返す
  long long lower_bound(long long key) {
    long long len = 1, temp = n, now = 0, value = 0, flag;
    while (temp / 2 != 0) {
      len *= 2;
      temp /= 2;
    }

    while (len > 0) {
      while (now + len > n)
        len /= 2;

      if (value + bit[now + len] >= key) {
        len /= 2;
      } else {
        now += len;
        value += bit[now];
        len /= 2;
        if (now == n) {
          return -1;
        }
      }
    }

    now++;
    return now;
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  ll n, m;
  std::cin >> n >> m;

  std::vector<P> ivl(n);
  rep(i, 0, n) {
    ll l, r;
    std::cin >> l >> r;
    ivl[i] = {l, r};
  }

  std::sort(all(ivl), comp);

  BinaryIndexedTree bit(m + 10);
  ll now = 0;

  rep(i, 1, m + 1) {
    while (ivl[now].second - ivl[now].first + 1 < i) {
      bit.add(ivl[now].first, 1);
      bit.add(ivl[now].second + 1, -1);
      now++;
    }
    ll cnt = 0;
    for (ll j = i; j <= m; j += i) {
      cnt += bit.sum(j);
    }
    std::cout << cnt + n - now << "\n";
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> pa) {
  return os << "[" << pa.first << ", " << pa.second << "]";
}

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "}";
  return os;
}

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug " << i << std::endl

// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;

bool comp(const P &a, const P &b) {
  return (a.second - a.first) < (b.second - b.first);
}

// とりあえず1-indexedでつくったBIT
class BinaryIndexedTree {
private:
  long long n;
  std::vector<long long> bit;

public:
  BinaryIndexedTree(long long i) : n(i), bit(i + 1, 0){};

  // a番目にwを加算
  void add(long long a, long long w) {
    for (long long x = a; x <= n; x += (x & -x)) {
      bit[x] += w;
    }
  }

  //[1,a]の和
  long long sum(long long a) {
    long long ret = 0;
    for (long long x = a; x > 0; x -= (x & -x)) {
      ret += bit[x];
    }
    return ret;
  }

  // 各項がすべて非負のものに対して、sum[1,a]>=keyを満たす最小のaを返す
  // 存在しないなら-1返す
  long long lower_bound(long long key) {
    long long len = 1, temp = n, now = 0, value = 0, flag;
    while (temp / 2 != 0) {
      len *= 2;
      temp /= 2;
    }

    while (len > 0) {
      while (now + len > n)
        len /= 2;

      if (value + bit[now + len] >= key) {
        len /= 2;
      } else {
        now += len;
        value += bit[now];
        len /= 2;
        if (now == n) {
          return -1;
        }
      }
    }

    now++;
    return now;
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  ll n, m;
  std::cin >> n >> m;

  std::vector<P> ivl(n);
  rep(i, 0, n) {
    ll l, r;
    std::cin >> l >> r;
    ivl[i] = {l, r};
  }

  std::sort(all(ivl), comp);

  BinaryIndexedTree bit(m + 10);
  ll now = 0;

  rep(i, 1, m + 1) {
    while (now < n && ivl[now].second - ivl[now].first + 1 < i) {
      bit.add(ivl[now].first, 1);
      bit.add(ivl[now].second + 1, -1);
      now++;
    }
    ll cnt = 0;
    for (ll j = i; j <= m; j += i) {
      cnt += bit.sum(j);
    }
    std::cout << cnt + n - now << "\n";
  }

  return 0;
}