Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 100005;
int tree[2 * N + 5];

struct record {
  int a, b;
};

record arr[N];

void update(int node, int start, int end, int idx, int val) {
  if (start == end) {
    tree[node] += val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx && idx <= mid) {
      update(node * 2, start, mid, idx, val);
    } else {
      update(node * 2 + 1, mid + 1, end, idx, val);
    }
    tree[node] = tree[node * 2] + tree[node * 2 + 1];
  }
}

int query(int node, int start, int end, int l, int r) {
  if (l > end || r < start)
    return 0;
  if (l <= start && end <= r)
    return tree[node];
  int mid = (start + end) / 2;
  int t1 = query(node * 2, start, mid, l, r);
  int t2 = query(node * 2 + 1, mid + 1, end, l, r);
  return t1 + t2;
}

int find_l(int start, int end) {
  int a = start, m = end;
  int result = -1;

  while (start <= end) {
    int mid = (start + end) / 2;
    int slot = query(1, 1, m, a, mid);
    if (slot < mid - a + 1) {
      result = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  return result;
}

bool compare(record x, record y) {
  if (x.b == y.b) {
    return x.a < y.a;
  }
  return x.b > y.b;
}

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; ++i) {
    cin >> arr[i].a >> arr[i].b;
  }

  sort(arr + 1, arr + 1 + n, compare);
  /*
  cout << endl;
  for(int i = 1; i <=n; ++i){
    cout << arr[i].b << " " << arr[i].a << endl;
  }
  */
  int result = 0;

  for (int i = 1; i <= n; ++i) {
    int a = arr[i].a;
    int b = arr[i].b;
    if (a > m)
      continue;
    int slot = query(1, 1, m, a, m);

    if (slot < m - a + 1) {
      result += b;
      int l = find_l(a, m);
      update(1, 1, m, l, 1);
    }
  }

  cout << result << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 100005;
int tree[4 * N + 5];

struct record {
  int a, b;
};

record arr[N];

void update(int node, int start, int end, int idx, int val) {
  if (start == end) {
    tree[node] += val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx && idx <= mid) {
      update(node * 2, start, mid, idx, val);
    } else {
      update(node * 2 + 1, mid + 1, end, idx, val);
    }
    tree[node] = tree[node * 2] + tree[node * 2 + 1];
  }
}

int query(int node, int start, int end, int l, int r) {
  if (l > end || r < start)
    return 0;
  if (l <= start && end <= r)
    return tree[node];
  int mid = (start + end) / 2;
  int t1 = query(node * 2, start, mid, l, r);
  int t2 = query(node * 2 + 1, mid + 1, end, l, r);
  return t1 + t2;
}

int find_l(int start, int end) {
  int a = start, m = end;
  int result = -1;

  while (start <= end) {
    int mid = (start + end) / 2;
    int slot = query(1, 1, m, a, mid);
    if (slot < mid - a + 1) {
      result = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  return result;
}

bool compare(record x, record y) {
  if (x.b == y.b) {
    return x.a < y.a;
  }
  return x.b > y.b;
}

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; ++i) {
    cin >> arr[i].a >> arr[i].b;
  }

  sort(arr + 1, arr + 1 + n, compare);
  /*
  cout << endl;
  for(int i = 1; i <=n; ++i){
    cout << arr[i].b << " " << arr[i].a << endl;
  }
  */
  int result = 0;

  for (int i = 1; i <= n; ++i) {
    int a = arr[i].a;
    int b = arr[i].b;
    if (a > m)
      continue;
    int slot = query(1, 1, m, a, m);

    if (slot < m - a + 1) {
      result += b;
      int l = find_l(a, m);
      update(1, 1, m, l, 1);
    }
  }

  cout << result << endl;
  return 0;
}