Problem p02610 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<pair<int, int>> vec;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l > r) {
        vec.push_back({l - r, k});
        ans += r;
      } else {
        vec.push_back({r - l, -(n - k)});
        ans += l;
      }
    }
    sort(vec.rbegin(), vec.rend());
    set<int> E;
    for (int i = 1; i <= n; ++i)
      E.insert(i);
    auto f = [&](int m) {
      set<int> empty = E;
      ll score = 0;
      for (int i = 0; i < n; ++i) {
        int k = vec[i].second;
        if (k < 0) {
          // leftmost of last -k.
          auto it = empty.lower_bound(max(m + 1, n + k + 1));
          if (it == empty.end())
            continue;
          empty.erase(it);
          score += vec[i].first;
        } else {
          // rightmost of first k.
          auto it = empty.upper_bound(min(m, k));
          if (it == empty.begin())
            continue;
          --it;
          empty.erase(it);
          score += vec[i].first;
        }
      }
      return score;
    };
    int lo = 0, hi = n;
    while (lo < hi) {
      int m1 = (lo + hi) / 2;
      int m2 = m1 + 1;
      if (f(m1) < f(m2))
        lo = m2;
      else
        hi = m1;
    }
    ans += f(lo);
    printf("%lld\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<pair<int, int>> vec;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l > r) {
        vec.push_back({l - r, k});
        ans += r;
      } else {
        vec.push_back({r - l, -(n - k)});
        ans += l;
      }
    }
    sort(vec.rbegin(), vec.rend());
    auto f = [&](int m) {
      set<int> empty;
      for (int i = 1; i <= n; ++i)
        empty.insert(i);
      ll score = 0;
      for (int i = 0; i < n; ++i) {
        int k = vec[i].second;
        if (k < 0) {
          // leftmost of last -k.
          auto it = empty.lower_bound(max(m + 1, n + k + 1));
          if (it == empty.end())
            continue;
          empty.erase(it);
          score += vec[i].first;
        } else {
          // rightmost of first k.
          auto it = empty.upper_bound(min(m, k));
          if (it == empty.begin())
            continue;
          --it;
          empty.erase(it);
          score += vec[i].first;
        }
      }
      return score;
    };
    int lo = 0, hi = n;
    while (lo < hi) {
      int m1 = (lo + hi) / 2;
      int m2 = m1 + 1;
      if (f(m1) < f(m2))
        lo = m2;
      else
        hi = m1;
    }
    ans += f(lo);
    printf("%lld\\n", ans);
  }
}