Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

template <typename T> struct BinaryIndexedTree {
  vector<T> data;
  int sz;

  BinaryIndexedTree(int n) {
    sz = n + 1;
    data.assign(sz, 0);
  }

  void add(int i, T x) {
    ++i;
    while (i < sz) {
      data[i] += x;
      i += i & -i;
    }
  }

  // [0, i] の区間和
  T sum(int i) {
    T res = 0;
    ++i;
    while (i > 0) {
      res += data[i];
      i -= i & -i;
    }
    return res;
  }

  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }

  // val 以上を満たす 0-indexed の位置を返す
  int lower_bound(T val) {
    // if(val <= 0)    return -1;
    int res = 0;
    int n = 1;
    while (n < sz)
      n <<= 1;
    for (int k = n >> 1; k > 0; k >>= 1) {
      if (res + k < sz && data[res + k] < val) {
        val -= data[res + k];
        res += k;
      }
    }
    return res; // 1-indexed であれば、res + 1 を返す
  }
};

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

  int n, m;
  cin >> n >> m;
  vector<P> table(n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    table[i] = P(a, b);
  }
  sort(table.begin(), table.end(), [](const P &p1, const P &p2) {
    if (p1.second == p2.second) {
      return p1.first < p2.first;
    }
    return p1.second > p2.second;
  });

  BinaryIndexedTree<int> bit(m + 5);
  for (int i = 1; i <= m; ++i)
    bit.add(i, 1);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int sum = bit.sum(m) - bit.sum(table[i].first - 1);
    if (sum == 0)
      continue;
    int p = bit.lower_bound(bit.sum(table[i].first - 1) + 1);
    bit.add(p, -1);
    ans += table[i].second;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

template <typename T> struct BinaryIndexedTree {
  vector<T> data;
  int sz;

  BinaryIndexedTree(int n) {
    sz = n + 1;
    data.assign(sz, 0);
  }

  void add(int i, T x) {
    ++i;
    while (i < sz) {
      data[i] += x;
      i += i & -i;
    }
  }

  // [0, i] の区間和
  T sum(int i) {
    T res = 0;
    ++i;
    while (i > 0) {
      res += data[i];
      i -= i & -i;
    }
    return res;
  }

  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }

  // val 以上を満たす 0-indexed の位置を返す
  int lower_bound(T val) {
    // if(val <= 0)    return -1;
    int res = 0;
    int n = 1;
    while (n < sz)
      n <<= 1;
    for (int k = n >> 1; k > 0; k >>= 1) {
      if (res + k < sz && data[res + k] < val) {
        val -= data[res + k];
        res += k;
      }
    }
    return res; // 1-indexed であれば、res + 1 を返す
  }
};

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

  int n, m;
  cin >> n >> m;
  vector<P> table(n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    table[i] = P(a, b);
  }
  sort(table.begin(), table.end(), [](const P &p1, const P &p2) {
    if (p1.second == p2.second) {
      return p1.first < p2.first;
    }
    return p1.second > p2.second;
  });

  BinaryIndexedTree<int> bit(m + 5);
  for (int i = 1; i <= m; ++i)
    bit.add(i, 1);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (table[i].first > m)
      continue;
    int sum = bit.sum(m) - bit.sum(table[i].first - 1);
    if (sum == 0)
      continue;
    int p = bit.lower_bound(bit.sum(table[i].first - 1) + 1);
    bit.add(p, -1);
    ans += table[i].second;
  }
  cout << ans << endl;
}