Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;
typedef long long ll;

void solve() {
  int n;
  cin >> n;

  vector<vector<int>> x(n, vector<int>(3));
  forn(i, n) forn(j, 3) cin >> x[i][j];

  sort(all(x), [&](const vector<int> &lhs, const vector<int> &rhs) {
    return lhs[1] - lhs[2] > rhs[1] - rhs[2]; // sort by L-R
  });

  set<int> left;
  forn(i, n) left.insert(i + 1);

  int first = n, skip = 0;
  ll ans = 0;

  forn(i, n) {
    if (x[i][1] - x[i][2] < 0) {
      first = i;
      break;
    }

    // greatest e in left <= x[i][0]
    auto it = left.upper_bound(x[i][0]);
    if (it == left.begin()) {
      ans += x[i][2];
      skip++;
      continue;
    }
    --it;

    left.erase(it);
    ans += x[i][1];
  }

  left.clear();
  for (int i = n - 1; i >= first; i--)
    left.insert(i + 1);

  for (int i = n - 1; i >= first; i--) {
    assert(x[i][1] - x[i][2] < 0);

    // want to be in smallest e > x[i][0]
    auto it = left.upper_bound(x[i][0]);

    if (it == left.end()) {
      ans += x[i][1];
      left.erase(left.begin());
      continue;
    }

    left.erase(it);
    ans += x[i][2];
  }

  cout << ans << "\\n";
}

int main() {
  ios;
  int t;
  cin >> t;
  while (t--)
    solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define ios                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;
typedef long long ll;

struct BT {
  BT(int n) : MAXN(n * 2 + 17), T(n * 2 + 17){};

  void add(int x, int v) {
    for (x++; x < MAXN; x += x & -x)
      T[x] += v;
  }

  // return sum of vals[i], 0 <= i <= x
  int get(int x) {
    int ret = 0;
    for (x++; x > 0; x &= x - 1)
      ret += T[x];
    return ret;
  }

  int MAXN;
  vector<int> T;
};

void solve() {
  int n;
  cin >> n;

  vector<vector<int>> x(n, vector<int>(3));
  forn(i, n) forn(j, 3) cin >> x[i][j];

  sort(all(x), [&](const vector<int> &lhs, const vector<int> &rhs) {
    return lhs[1] - lhs[2] > rhs[1] - rhs[2]; // sort by L-R
  });

  BT bit(n);
  int first = n;
  ll ans = 0;

  forn(i, n) {
    if (x[i][1] - x[i][2] < 0) {
      first = i;
      break;
    }

    int g = bit.get(x[i][0]);
    assert(g <= x[i][0]);

    if (g == x[i][0]) {
      ans += x[i][2];
      continue;
    }

    bit.add(x[i][0], 1);
    ans += x[i][1];
  }

  fill(all(bit.T), 0);

  for (int i = n - 1; i >= first; i--) {
    assert(x[i][1] - x[i][2] < 0);

    // dont want to be in the top x[i][0]
    // = want to be in the bottom n-x[i][0]

    int g = bit.get(n - x[i][0]);
    assert(g <= n - x[i][0]);

    if (g == n - x[i][0]) {
      ans += x[i][1];
      continue;
    }

    bit.add(n - x[i][0], 1);
    ans += x[i][2];
  }

  cout << ans << "\\n";
}

int main() {
  ios;
  int t;
  cin >> t;
  while (t--)
    solve();
}