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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 1000000007
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int dpx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dpy[8] = {-1, 1, 0, 1, -1, 0, -1, 1};

struct Camel {
  int l, r, k;
};

bool cmp(Camel c1, Camel c2) { return abs(c1.l - c1.r) > abs(c2.l - c2.r); }

int32_t main() {
  fastIO inp_out();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<Camel> a(n);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      cin >> a[i].k >> a[i].l >> a[i].r;
      ans += min(a[i].l, a[i].r);
    }

    sort(a.begin(), a.end(), cmp);

    set<int> str;
    set<int, greater<int>> stl;

    for (int i = 1; i <= n; ++i) {
      stl.insert(i);
      str.insert(i);
    }

    for (int i = 0; i < n; ++i) {
      if (a[i].l >= a[i].r) {
        auto itr = stl.lower_bound(a[i].k);
        if (itr != stl.end()) {
          stl.erase(*itr);
          str.erase(str.begin());
          ans += abs(a[i].l - a[i].r);
        }
      } else {
        auto itr = str.upper_bound(a[i].k);
        if (itr != str.end()) {
          str.erase(*itr);
          stl.erase(stl.begin());
          ans += abs(a[i].l - a[i].r);
        }
      }
    }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 1000000007
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int dpx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dpy[8] = {-1, 1, 0, 1, -1, 0, -1, 1};

struct Camel {
  int l, r, k;
};

bool cmp(Camel c1, Camel c2) { return abs(c1.l - c1.r) > abs(c1.l - c2.r); }

int32_t main() {
  fastIO inp_out();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<Camel> a(n);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      cin >> a[i].k >> a[i].l >> a[i].r;
      ans += min(a[i].l, a[i].r);
    }
    sort(a.begin(), a.end(), cmp);

    set<int> st, pos;
    set<int, greater<int>> ulta_st;

    for (int i = 1; i <= n; ++i) {
      st.insert(i);
      ulta_st.insert(i);
    }

    for (int i = 0; i < n; ++i) {
      if (a[i].l >= a[i].r) {
        auto itr = ulta_st.lower_bound(a[i].k);
        if (itr != ulta_st.end()) {
          ulta_st.erase(*itr);
          st.erase(*itr);
          ans += abs(a[i].l - a[i].r);
          pos.insert(*itr);
        }
      } else {
        auto itr = st.upper_bound(a[i].k);
        if (itr != st.end()) {
          ulta_st.erase(*itr);
          st.erase(*itr);
          ans += abs(a[i].l - a[i].r);
        } else {
          auto itr1 = pos.upper_bound(a[i].k);
          if (itr1 != pos.end()) {
            auto itr2 = ulta_st.lower_bound(*itr1);
            if (itr2 != ulta_st.end()) {
              ulta_st.erase(*itr2);
              st.erase(*itr2);
              pos.erase(*itr1);
              pos.insert(*itr2);
              ans += abs(a[i].l - a[i].r);
            }
          }
        }
      }
    }

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