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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl;                                                              \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL

typedef long long ll;
typedef pair<ll, ll> P;

void solve() {
  ll n;
  cin >> n;
  vector<P> a;
  vector<P> b;
  ll ans = 0;

  REP(i, n) {
    ll k, l, r;
    cin >> k >> l >> r;
    if (l < r) {
      b.PB(MP(n - k - 1, r - l));
    } else {
      a.PB(MP(k - 1, l - r));
    }
    ans += min(l, r);
  }

  sort(ALL(a), greater<P>());
  sort(ALL(b), greater<P>());
  // REP(i, a.size())cout << "(" << a[i].FI << "," << a[i].SE << ")";cout <<
  // endl; REP(i, b.size())cout << "(" << b[i].FI << "," << b[i].SE << ")";cout
  // << endl; cout << "ans: " << ans << endl;

  priority_queue<ll> pq;
  ll pla = 0;
  REV(i, a.size()) {
    while (pla < a.size() && a[pla].FI >= i) {
      pq.push(a[pla].SE);
      pla++;
    }
    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }
  while (!pq.empty())
    pq.pop();
  pla = 0;
  REV(i, b.size()) {
    while (pla < b.size() && b[pla].FI >= i) {
      pq.push(b[pla].SE);
      pla++;
    }
    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }
  while (!pq.empty())
    pq.pop();
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll t;
  cin >> t;
  REP(i, t) solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl;                                                              \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL

typedef long long ll;
typedef pair<ll, ll> P;

class minCostFlow {

  struct edge {
    ll to, cap, cost, rev;
  };

  ll V;
  vector<vector<edge>> G;
  vector<ll> dist;
  vector<ll> prevv;
  vector<ll> preve;

public:
  minCostFlow(ll n) : G(n), dist(n), prevv(n), preve(n), V(n) {}

  void addEdge(ll from, ll to, ll cap, ll cost) {
    G[from].push_back((edge){to, cap, cost, (ll)G[to].size()});
    G[to].push_back((edge){from, 0, -cost, (ll)G[from].size() - 1});
  }

  ll solve(ll s, ll t, ll f) {
    ll ret = 0;
    while (f > 0) {
      fill(dist.begin(), dist.end(), LLINF);
      dist[s] = 0;
      bool update = true;
      while (update) {
        update = false;
        for (ll v = 0; v < V; v++) {
          if (dist[v] == LLINF)
            continue;
          for (ll i = 0; i < G[v].size(); i++) {
            edge &e = G[v][i];
            if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
              dist[e.to] = dist[v] + e.cost;
              prevv[e.to] = v;
              preve[e.to] = i;
              update = true;
            }
          }
        }
      }
      if (dist[t] == LLINF) {
        return LLINF; // 流せない
      }

      ll d = f;
      for (ll v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      ret += d * dist[t];
      for (ll v = t; v != s; v = prevv[v]) {
        edge &e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    return ret;
  }
};

void solve() {
  ll n;
  cin >> n;
  ll m = n + 1;

  minCostFlow mcf(4 * m + 2);
  REP(i, n) {
    mcf.addEdge(m + i, 3 * m + i, 1, 0);
    mcf.addEdge(2 * m + i, 3 * m + i, 1, 0);
    mcf.addEdge(3 * m + i, 4 * m + 1, 1, 0);
    if (i == n - 1)
      continue;
    mcf.addEdge(m + i, m + i + 1, INF, 0);
    mcf.addEdge(2 * m + i + 1, 2 * m + i, INF, 0);
  }

  REP(i, n) {
    ll k, l, r;
    cin >> k >> l >> r;
    mcf.addEdge(4 * m, i, 1, 0);
    mcf.addEdge(i, m + k, 1, -r);
    mcf.addEdge(i, 2 * m + k - 1, 1, -l);
  }

  cout << -mcf.solve(4 * m, 4 * m + 1, n) << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll t;
  cin >> t;
  REP(i, t) solve();

  return 0;
}