Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef long long int ll;

// int memo[1000010] = {0,0,1};
double PI = 3.14159265359;

ll MOD = 10e9 + 7;

ll dataA[100010];
ll dataB[100010];

class fenwick {
public:
  void Add(int a, int b, int x, int k, int l, int r) {
    if (a <= l && r <= b) {
      dataA[k] += x;
    } else if (l < b && a < r) {
      // cout << a << ":" << b << endl;
      dataB[k] += (min(b, r) - max(a, l)) * x;
      Add(a, b, x, k * 2 + 1, l, (l + r) / 2);
      Add(a, b, x, k * 2 + 2, (l + r) / 2, r);
    }
  }

  ll sum(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return 0;
    } else if (a <= l && r <= b) {
      return dataA[k] * (r - l) + dataB[k];
    } else {
      ll res = (min(b, r) - max(a, l)) * dataA[k];
      res += sum(a, b, k * 2 + 1, l, (l + r) / 2);
      res += sum(a, b, k * 2 + 2, (l + r) / 2, r);
      return res;
    }
  }
};

vector<pair<int, pair<int, int>>> gift;

int main(void) {
  // Here your code !
  ll n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> l >> r;
    gift.push_back(pair<int, pair<int, int>>(r - l + 1, pair<int, int>(l, r)));
  }

  fenwick fwk;

  sort(gift.begin(), gift.end());
  int t = 0;

  // cout << endl;

  for (int i = 1; i <= m; i++) {

    for (; t < n; t++) {
      if (gift[t].first <= i) {
        // cout << gift[t].second.first << " " << gift[t].second.second << endl;
        fwk.Add(gift[t].second.first, gift[t].second.second + 1, 1, 0, 0,
                m + 1);
      } else {
        break;
      }
    }

    ll res = n - t;
    // cout << res << endl;

    for (int j = 0; j <= m; j += i) {
      res += fwk.sum(j, j + 1, 0, 0, m + 1);
    }

    cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef long long int ll;

// int memo[1000010] = {0,0,1};
double PI = 3.14159265359;

ll MOD = 10e9 + 7;

ll dataA[500010];
ll dataB[500010];

class fenwick {
public:
  void Add(int a, int b, int x, int k, int l, int r) {
    if (a <= l && r <= b) {
      dataA[k] += x;
    } else if (l < b && a < r) {
      // cout << a << ":" << b << endl;
      dataB[k] += (min(b, r) - max(a, l)) * x;
      Add(a, b, x, k * 2 + 1, l, (l + r) / 2);
      Add(a, b, x, k * 2 + 2, (l + r) / 2, r);
    }
  }

  ll sum(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return 0;
    } else if (a <= l && r <= b) {
      return dataA[k] * (r - l) + dataB[k];
    } else {
      ll res = (min(b, r) - max(a, l)) * dataA[k];
      res += sum(a, b, k * 2 + 1, l, (l + r) / 2);
      res += sum(a, b, k * 2 + 2, (l + r) / 2, r);
      return res;
    }
  }
};

vector<pair<int, pair<int, int>>> gift;

int main(void) {
  // Here your code !
  ll n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> l >> r;
    gift.push_back(pair<int, pair<int, int>>(r - l + 1, pair<int, int>(l, r)));
  }

  fenwick fwk;

  sort(gift.begin(), gift.end());
  int t = 0;

  // cout << endl;

  for (int i = 1; i <= m; i++) {

    for (; t < n; t++) {
      if (gift[t].first <= i) {
        // cout << gift[t].second.first << " " << gift[t].second.second << endl;
        fwk.Add(gift[t].second.first, gift[t].second.second + 1, 1, 0, 0,
                m + 1);
      } else {
        break;
      }
    }

    ll res = n - t;
    // cout << res << endl;

    for (int j = 0; j <= m; j += i) {
      res += fwk.sum(j, j + 1, 0, 0, m + 1);
    }

    cout << res << endl;
  }
}