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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define itrout(x)                                                              \\
  for (int i = 0; i < x.size(); i++) {                                         \\
    cout << x[i] << (i == x.size() - 1 ? "\\n" : " ");                          \\
  }
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

class BIT {
public:
  vector<int> bit;
  int M;

  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}

  int sum(int i) {
    if (!i)
      return 0;
    return bit[i] + sum(i - (i & -i));
  }

  void add(int i, int x) {
    if (i > M)
      return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};

signed main() {
  int T;
  cin >> T;
  rep(ti, 0, T) {
    int N;
    cin >> N;
    vector<ll> K(N), L(N), R(N);
    rep(i, 0, N) cin >> K[i] >> L[i] >> R[i];
    vector<pair<ll, ll>> LR(N);
    rep(i, 0, N) LR[i] = {abs(L[i] - R[i]), i};
    sort(all(LR), greater<>());
    ll answer = 0;
    set<int> unusedL, unusedR;
    rep(i, 1, N + 1) {
      unusedL.insert(i);
      unusedR.insert(i);
    }
    rep(i, 0, N) {
      int j = LR[i].second;
      if (L[j] - R[j] >= 0) {
        auto itr = unusedL.upper_bound(K[j]);
        if (unusedL.begin() == itr) {
          answer += R[j];
        } else {
          itr--;
          unusedL.erase(itr);
          answer += L[j];
        }
      } else {
        auto itr = unusedR.lower_bound(K[j] + 1);
        if (itr == unusedR.end()) {
          answer += L[j];
        } else {
          unusedR.erase(itr);
          answer += R[j];
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define itrout(x)                                                              \\
  for (int i = 0; i < x.size(); i++) {                                         \\
    cout << x[i] << (i == x.size() - 1 ? "\\n" : " ");                          \\
  }
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

class BIT {
public:
  vector<int> bit;
  int M;

  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}

  int sum(int i) {
    if (!i)
      return 0;
    return bit[i] + sum(i - (i & -i));
  }

  void add(int i, int x) {
    if (i > M)
      return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};

signed main() {
  int T;
  cin >> T;
  rep(ti, 0, T) {
    int N;
    cin >> N;
    vector<ll> K(N), L(N), R(N);
    rep(i, 0, N) cin >> K[i] >> L[i] >> R[i];
    vector<pair<ll, ll>> LR(N);
    rep(i, 0, N) LR[i] = {abs(L[i] - R[i]), i};
    sort(all(LR), greater<>());
    BIT bitL(N + 1), bitR(N + 1);
    ll answer = 0;
    int plus = 0;
    rep(i, 0, N) {
      if (L[i] - R[i] >= 0)
        plus++;
    }
    rep(i, 0, N) {
      int j = LR[i].second;
      if (L[j] - R[j] >= 0) {
        if (bitL.sum(K[j]) >= K[j]) {
          answer += R[j];
          bitL.add(plus, 1);
        } else {
          answer += L[j];
          bitL.add(K[j], 1);
        }
      } else {
        if (bitR.sum(N - K[j] + 1) >= N - K[j]) {
          answer += L[j];
          bitL.add(N - plus + 1, 1);
        } else {
          answer += R[j];
          bitR.add(N - K[j] + 1, 1);
        }
      }
    }
    bitL.add(0, 1);
    cout << answer << endl;
  }
  return 0;
}