Problem p03819 (C++)

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

using namespace std;

const int maxn = 3e5 + 100;
const int mod = 1e9 + 7;
long long BIT[maxn];
long long ops[maxn];
int f[maxn];
vector<pair<long long, long long>> inters;
vector<long long> strt;
vector<pair<pair<long long, long long>, pair<long long, long long>>> v;
long long ans[maxn];
queue<pair<long long, long long>> que;
vector<pair<long long, long long>> a;
long long max(long long x, long long y) { return (x > y ? x : y); }
long long min(long long x, long long y) { return (x < y ? x : y); }

long long sum(int ind) {
  ind++;
  long long s = 0;
  while (ind > 0) {
    s += BIT[ind];
    ind -= ind & (-ind);
  }
  return s;
}

void update(long long x, long long ind, long long sz) {
  ind++;
  while (ind <= sz) {
    BIT[ind] += x;
    ind += ind & (-ind);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, q, nn, mm;
  cin >> nn >> mm;
  n = mm + 1;
  for (int i = 0; i < nn; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    f[t1]++;
    inters.push_back(make_pair(t1, t2));
  }
  sort(inters.begin(), inters.end());
  for (int i = 0; i < inters.size(); i++) {
    a.push_back(make_pair(inters[i].second, i));
    strt.push_back(inters[i].first);
  }
  long long hgt = 0;
  for (int i = 0; i <= mm; i++) {
    ops[i] = f[i] + hgt;
    hgt = ops[i];
  }
  long long g = 0;
  for (int i = 0; i < mm; i++) {
    long long cmt = ops[0];
    g++;
    for (int j = g; j <= mm; j += g) {
      long long yp = ops[j] - ops[j - g];
      cmt += yp;
      long long t, b, k;
      k = j;
      b = upper_bound(strt.begin(), strt.end(), j) - strt.begin();
      t = upper_bound(strt.begin(), strt.end(), j - g) - strt.begin();
      b--;
      if (b < t) {
        continue;
      }
      v.push_back(make_pair(make_pair(k, i), make_pair(t, b)));
    }
    ans[i] = cmt;
  }
  sort(v.begin(), v.end());
  sort(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++) {
    que.push(a[i]);
  }
  for (int i = 0; i < v.size(); i++) {
    int r = v[i].second.second;
    int l = v[i].second.first;
    int id = v[i].first.second;
    long long u = v[i].first.first;
    while (que.size() && que.front().first < u) {
      update(1, que.front().second, 302100);
      que.pop();
    }
    l--;
    if (l == -1) {
      ans[id] -= sum(r);
      continue;
    }
    ans[id] -= (sum(r) - sum(l));
  }

  // cout << ans[0] << endl;
  for (int i = 0; i < mm; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


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

using namespace std;

const int maxn = 3e5 + 2110;
const int mod = 1e9 + 7;
long long BIT[maxn];
long long ops[maxn];
int f[maxn];
vector<pair<long long, long long>> inters;
vector<long long> strt;
vector<pair<pair<long long, long long>, pair<long long, long long>>> v;
long long ans[maxn];
queue<pair<long long, long long>> que;
vector<pair<long long, long long>> a;
long long max(long long x, long long y) { return (x > y ? x : y); }
long long min(long long x, long long y) { return (x < y ? x : y); }

long long sum(int ind) {
  ind++;
  long long s = 0;
  while (ind > 0) {
    s += BIT[ind];
    ind -= ind & (-ind);
  }
  return s;
}

void update(long long x, long long ind, long long sz) {
  ind++;
  while (ind <= sz) {
    BIT[ind] += x;
    ind += ind & (-ind);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, q, nn, mm;
  cin >> nn >> mm;
  n = mm + 1;
  for (int i = 0; i < nn; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    f[t1]++;
    inters.push_back(make_pair(t1, t2));
  }
  sort(inters.begin(), inters.end());
  for (int i = 0; i < inters.size(); i++) {
    a.push_back(make_pair(inters[i].second, i));
    strt.push_back(inters[i].first);
  }
  long long hgt = 0;
  for (int i = 0; i <= mm; i++) {
    ops[i] = f[i] + hgt;
    hgt = ops[i];
  }
  long long g = 0;
  for (int i = 0; i < mm; i++) {
    long long cmt = ops[0];
    g++;
    for (int j = g; j <= mm; j += g) {
      long long yp = ops[j] - ops[j - g];
      cmt += yp;
      long long t, b, k;
      k = j;
      b = upper_bound(strt.begin(), strt.end(), j) - strt.begin();
      t = upper_bound(strt.begin(), strt.end(), j - g) - strt.begin();
      b--;
      if (b < t) {
        continue;
      }
      v.push_back(make_pair(make_pair(k, i), make_pair(t, b)));
    }
    ans[i] = cmt;
  }
  sort(v.begin(), v.end());
  sort(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++) {
    que.push(a[i]);
  }
  for (int i = 0; i < v.size(); i++) {
    int r = v[i].second.second;
    int l = v[i].second.first;
    int id = v[i].first.second;
    long long u = v[i].first.first;
    while (que.size() && que.front().first < u) {
      update(1, que.front().second, 302100);
      que.pop();
    }
    l--;
    if (l == -1) {
      ans[id] -= sum(r);
      continue;
    }
    ans[id] -= (sum(r) - sum(l));
  }

  // cout << ans[0] << endl;
  for (int i = 0; i < mm; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}