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

#include <algorithm>
#include <cstdio>
#include <ctype.h>
#include <queue>
#define DEBUG
const int bufSize = 1e6;
using namespace std;
inline char nc() {
#ifdef DEBUG
  return getchar();
#endif
  static char buf[bufSize], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
template <typename T> inline T read(T &r) {
  static char c;
  r = 0;
  for (c = nc(); !isdigit(c);)
    c = nc();
  for (; isdigit(c); c = nc())
    r = r * 10 + c - 48;
  return r;
}
const int maxn = 2e5 + 100;
int T, n, anum, bnum;
struct node {
  int L, R, K;
  bool operator<(const node &b) { return K < b.K; }
} A[maxn], B[maxn];
priority_queue<int, vector<int>, greater<int>> q1, q2;
int main() {
  read(T);
  while (T--) {
    anum = bnum = 0;
    long long ans = 0;
    read(n);
    for (int i = 1; i <= n; ++i) {
      int k, l, r;
      read(k), read(l), read(r);
      if (l > r)
        A[++anum].L = l, A[anum].R = r, A[anum].K = k;
      else
        B[++bnum].L = l, B[bnum].R = r, B[bnum].K = n - k;
      ans += std::min(l, r);
    }
    sort(A + 1, A + 1 + anum), sort(B + 1, B + 1 + bnum);
    for (int i = 1; i <= anum; ++i) {
      if (A[i].K == 0)
        continue;
      if (q1.size() < A[i].K)
        q1.push(A[i].L - A[i].R);
      else if (q1.size() == A[i].K && q1.top() < A[i].L - A[i].R)
        q1.pop(), q1.push(A[i].L - A[i].R);
    }
    for (int i = 1; i <= bnum; ++i) {
      if (B[i].K == 0)
        continue;
      if (q2.size() < B[i].K)
        q2.push(B[i].R - B[i].L);
      else if (q2.size() == B[i].K && q2.top() < B[i].R - B[i].L)
        q2.pop(), q2.push(B[i].R - B[i].L);
    }
    while (!q1.empty())
      ans += q1.top(), q1.pop();
    while (!q2.empty())
      ans += q2.top(), q2.pop();
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (register int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (register int i = (a); i >= (b); --i)
#define pi pair<int, int>
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 100005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
struct Node {
  int l, r, d;
  bool operator<(const Node a) const { return abs(r - l) < abs(a.r - a.l); }
} s[maxn];
vector<int> a, b;
bool cmp1(int a, int b) { return s[a].d < s[b].d; }
bool cmp2(int a, int b) { return s[a].d > s[b].d; }
set<Node> aa, bb;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    aa.clear(), bb.clear(), a.clear(), b.clear();
    ll ans = 0;
    FOR(i, 0, n) {
      scanf("%d%d%d", &s[i].d, &s[i].l, &s[i].r);
      if (s[i].l > s[i].r)
        a.push_back(i);
      else if (s[i].l < s[i].r)
        b.push_back(i);
      else
        ans += s[i].l;
    }
    sort(a.begin(), a.end(), cmp1);
    sort(b.begin(), b.end(), cmp2);
    FOR(i, 0, a.size()) {
      if ((int)aa.size() == s[a[i]].d) {
        if (abs(aa.begin()->r - aa.begin()->l) < abs(s[a[i]].r - s[a[i]].l)) {
          ans += aa.begin()->r - aa.begin()->l;
          aa.erase(aa.begin());
          aa.insert(s[a[i]]);
          ans += s[a[i]].l;
        } else {
          ans += s[a[i]].r;
        }
      } else {
        aa.insert(s[a[i]]);
        ans += s[a[i]].l;
      }
    }
    FOR(i, 0, b.size()) {
      if ((int)bb.size() == n - s[b[i]].d && n - s[b[i]].d) {
        if (abs(bb.begin()->r - bb.begin()->l) < abs(s[b[i]].r - s[b[i]].l)) {
          ans += bb.begin()->l - bb.begin()->r;
          bb.erase(bb.begin());
          ans += s[b[i]].r;
          bb.insert(s[b[i]]);
        } else {
          ans += s[b[i]].l;
        }
      } else if ((int)bb.size() < n - s[b[i]].d) {
        bb.insert(s[b[i]]);
        ans += s[b[i]].r;
      } else {
        ans += s[b[i]].l;
      }
    }
    printf("%lld\\n", ans);
  }
}