Language: C++

#include <vector>
#include <map>

class BinaryIndexedTree
{
private:
    std::vector<int> vals;
    int n;
public:
    BinaryIndexedTree(int n_)
        : vals(n_, 0), n(n_) { }

    void add(int a, int w)
    {
        for (int x = a; x <= n; x += x & -x) {
            vals[x - 1] += w;
        }
    }

    int sum(int a) const
    {
        int ret = 0;
        for (int x = a; x > 0; x -= x & -x) {
            ret += vals[x - 1];
        }
        return ret;
    }
};

class SnLine
{
private:
    struct ProdRange
    {
        ProdRange() { l = r = 0; }
        ProdRange(int l_, int r_) { l = l_; r = r_; }
        int l, r;
    };

    static ProdRange makeProdRange(int l, int r)
    {
        return ProdRange { l, r };
    }

    std::map< int, std::vector<ProdRange> > prList;

    int numProds;
    int numStops;

public:
    SnLine(int numStops_)
    {
        numStops = numStops_;
        numProds = 0;
    }

    void addProdRange(int l, int r)
    {
        int d = r - l + 1;
        prList[d].emplace_back(l, r);
        ++numProds;
    }

    void solve()
    {
        BinaryIndexedTree bit(numStops);
        
        auto it = prList.begin();
        int numRemainingProds = numProds;

        for (int d = 1; d <= numStops; ++d)
        {
            int r = numRemainingProds;

            if (1 < d)
            {
                for (int a = d; a <= numStops; a += d)
                {
                    r += bit.sum(a);
                }
            }

            std::cout << r << std::endl;

            if (it != prList.end() && it->first == d)
            {
                for (const ProdRange& pr : it->second)
                {
                    bit.add(pr.l, 1);
                    bit.add(pr.r + 1, -1);
                }
                numRemainingProds -= static_cast<int>(it->second.size());
                ++it;
            }
        }
    }
};

void main()
{
    int N, M;
    std::cin >> N >> M;

    SnLine sl(M);

    for (int i = 0; i < N; ++i)
    {
        int l, r;
        std::cin >> l >> r;
        sl.addProdRange(l, r);
    }

    sl.solve();
}


Compiler output:
a.cc: In member function 'void SnLine::solve()':
a.cc:83:18: error: 'cout' is not a member of 'std'
   83 |             std::cout << r << std::endl;
      |                  ^~~~
a.cc:3:1: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    2 | #include <map>
  +++ |+#include <iostream>
    3 | 
a.cc:83:36: error: 'endl' is not a member of 'std'
   83 |             std::cout << r << std::endl;
      |                                    ^~~~
a.cc:3:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    2 | #include <map>
  +++ |+#include <ostream>
    3 | 
a.cc: At global scope:
a.cc:99:1: error: '::main' must return 'int'
   99 | void main()
      | ^~~~
a.cc: In function 'int main()':
a.cc:102:10: error: 'cin' is not a member of 'std'
  102 |     std::cin >> N >> M;
      |          ^~~
a.cc:102:10: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:109:14: error: 'cin' is not a member of 'std'
  109 |         std::cin >> l >> r;
      |              ^~~
a.cc:109:14: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'