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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 7;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  const int MIN = (1 << 31);
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> cml;
    set<int> pos;
    ll ans = 0;
    for (int u = 0; u < n; u++) {
      int k, l, r;
      cin >> k >> l >> r;
      cml.push_back({l - r, k});
      ans += r;
      pos.insert(u + 1);
    }
    sort(cml.begin(), cml.end());
    reverse(cml.begin(), cml.end());
    vector<int> csm;
    for (int u = 0; u < n; u++) {
      if (cml[u].first < 0)
        break;
      if (*pos.begin() <= cml[u].second) {
        auto p = pos.upper_bound(cml[u].second);
        p--;
        csm.push_back(*p);
        ans += cml[u].first;
        pos.erase(p);
        cml[u].first = MIN;
      }
    }
    sort(csm.begin(), csm.end());
    for (int v : csm) {
      if (*pos.begin() <= v) {
        pos.insert(v);
        pos.erase(pos.begin());
      }
    }
    sort(cml.begin(), cml.end());
    for (int u = 0; u < n; u++) {
      if (cml[u].first == MIN)
        continue;
      auto p = pos.upper_bound(cml[u].second);
      if (p == pos.end())
        ans += cml[u].first;
      else
        pos.erase(p);
    }
    cout << ans << "\\n";
  }
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 7;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  const int MIN = (1 << 31);
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> cml;
    set<int> pos;
    ll ans = 0;
    for (int u = 0; u < n; u++) {
      int k, l, r;
      cin >> k >> l >> r;
      cml.push_back({l - r, k});
      ans += r;
      pos.insert(u + 1);
    }
    sort(cml.begin(), cml.end());
    reverse(cml.begin(), cml.end());
    vector<int> csm;
    for (int u = 0; u < n; u++) {
      if (cml[u].first < 0)
        break;
      if (*pos.begin() <= cml[u].second) {
        auto p = pos.upper_bound(cml[u].second);
        p--;
        csm.push_back(*p);
        ans += cml[u].first;
        pos.erase(p);
        cml[u].first = MIN;
      }
    }
    sort(csm.begin(), csm.end());
    for (int v : csm) {
      if (*pos.begin() <= v) {
        pos.erase(pos.begin());
        pos.insert(v);
      }
    }
    sort(cml.begin(), cml.end());
    for (int u = 0; u < n; u++) {
      if (cml[u].first == MIN)
        continue;
      auto p = pos.upper_bound(cml[u].second);
      if (p == pos.end())
        ans += cml[u].first;
      else
        pos.erase(p);
    }
    cout << ans << "\\n";
  }
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}