Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
const long long mod = 1000000007;

vector<int> tree[100010];

vector<vector<int>> nums;

void build(int n = 0, int l = 0, int r = (int)nums.size() - 1) {
  if (l == r) {
    tree[n] = nums[l];
    sort(tree[n].begin(), tree[n].end());
    return;
  }
  int md = (l + r) >> 1;
  build(2 * n + 1, l, md);
  build(2 * n + 2, md + 1, r);
  tree[n] = tree[2 * n + 1];
  tree[n].insert(tree[n].end(), tree[2 * n + 2].begin(), tree[2 * n + 2].end());
  sort(tree[n].begin(), tree[n].end());
}

int K;
int get(int a, int b, int n = 0, int l = 0, int r = (int)nums.size() - 1) {
  if (a == l && b == r) {
    return lower_bound(tree[n].begin(), tree[n].end(), K) - tree[n].begin();
  }
  int md = (l + r) >> 1;
  int res = 0;
  if (a <= md)
    res = get(a, min(b, md), 2 * n + 1, l, md);
  if (b > md)
    res += get(max(a, md + 1), b, 2 * n + 2, md + 1, r);
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, M, u, v;
  cin >> N >> M;
  nums.resize(M + 1);
  for (int i = 0; i < N; i++) {
    cin >> u >> v;
    nums[u].push_back(v);
  }
  build();
  for (int i = 1; i <= M; i++) {
    int res = N;
    for (int j = 0; j < M; j += i) {
      K = j + i;
      res -= get(j + 1, min(j + i - 1, M));
    }
    cout << res << '\n';
  }
  return 0;
}
/**/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
const long long mod = 1000000007;

vector<int> tree[400010];

vector<vector<int>> nums;

void build(int n = 0, int l = 0, int r = (int)nums.size() - 1) {
  if (l == r) {
    tree[n] = nums[l];
    sort(tree[n].begin(), tree[n].end());
    return;
  }
  int md = (l + r) >> 1;
  build(2 * n + 1, l, md);
  build(2 * n + 2, md + 1, r);
  tree[n] = tree[2 * n + 1];
  tree[n].insert(tree[n].end(), tree[2 * n + 2].begin(), tree[2 * n + 2].end());
  sort(tree[n].begin(), tree[n].end());
}

int K;
int get(int a, int b, int n = 0, int l = 0, int r = (int)nums.size() - 1) {
  if (a == l && b == r) {
    return lower_bound(tree[n].begin(), tree[n].end(), K) - tree[n].begin();
  }
  int md = (l + r) >> 1;
  int res = 0;
  if (a <= md)
    res = get(a, min(b, md), 2 * n + 1, l, md);
  if (b > md)
    res += get(max(a, md + 1), b, 2 * n + 2, md + 1, r);
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, M, u, v;
  cin >> N >> M;
  nums.resize(M + 1);
  for (int i = 0; i < N; i++) {
    cin >> u >> v;
    nums[u].push_back(v);
  }
  build();
  for (int i = 1; i <= M; i++) {
    int res = N;
    for (int j = 0; j < M; j += i) {
      K = j + i;
      res -= get(j + 1, min(j + i - 1, M));
    }
    cout << res << '\n';
  }
  return 0;
}
/**/