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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int tac;
int n;
int k[200010], l[200010], r[200010];
vector<PII> v[3];
int a[200010];
set<int> st;
int main() {
  scanf("%d", &tac);
  while (tac--) {
    scanf("%d", &n);
    for (int i = 0; i < 3; i++)
      v[i].clear();
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d", &k[i], &l[i], &r[i]);
      if (l[i] > r[i])
        v[0].push_back(MP(r[i] - l[i], i));
      else if (l[i] < r[i])
        v[1].push_back(MP(l[i] - r[i], i));
      else
        v[2].push_back(MP(0, i));
    }
    sort(v[0].begin(), v[0].end());
    sort(v[1].begin(), v[1].end());
    vector<int> vr;
    st.clear();
    for (int i = 0; i < n; i++)
      a[i] = -1, st.insert(i);
    int ta = n - 1;
    for (int i = 0; i < v[0].size(); i++) {
      int t = v[0][i].Snd;
      set<int>::iterator it = st.upper_bound(k[t] - 1);
      if (it != st.begin())
        it--, a[*it] = t, st.erase(it);
      else {
        while (a[ta] != -1 && ta >= 0)
          ta--;
        a[ta] = t, st.erase(ta);
        while (a[ta] != -1 && ta >= 0)
          ta--;
      }
    }
    for (int i = 0; i < n; i++)
      if (a[i] != -1)
        vr.push_back(a[i]);
    // cout <<v[0].size()<<" "<<vr.size()<<" ##\\n";
    for (int i = 0; i < v[2].size(); i++)
      vr.push_back(v[2][i].Snd);
    for (int i = 0; i < n; i++)
      a[i] = -1, st.insert(i);
    ta = 0;
    for (int i = 0; i < v[1].size(); i++) {
      int t = v[1][i].Snd;
      set<int>::iterator it = st.lower_bound(k[t]);
      // cout <<*it<<" ##\\n";
      if (it != st.end())
        a[*it] = t, st.erase(it);
      else {
        while (a[ta] != -1 && ta < n)
          ta++;
        a[ta] = t, st.erase(ta);
        while (a[ta] != -1 && ta < n)
          ta++;
      }
    }
    for (int i = 0; i < n; i++)
      if (a[i] != -1)
        vr.push_back(a[i]);
    // cout <<v[1].size()<<" "<<vr.size()<<" ##\\n";
    ll res = 0;
    for (int i = 0; i < n; i++) {
      int t = vr[i];
      // cout <<vr[i]<<" ##\\n";
      if (i < k[t])
        res += l[t];
      else
        res += r[t];
    }
    printf("%lld\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int tac;
int n;
int k[200010], l[200010], r[200010];
vector<PII> v[3];
int a[200010];
set<int> st;
int main() {
  scanf("%d", &tac);
  while (tac--) {
    scanf("%d", &n);
    for (int i = 0; i < 3; i++)
      v[i].clear();
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d", &k[i], &l[i], &r[i]);
      if (l[i] > r[i])
        v[0].push_back(MP(r[i] - l[i], i));
      else if (l[i] < r[i])
        v[1].push_back(MP(l[i] - r[i], i));
      else
        v[2].push_back(MP(0, i));
    }
    sort(v[0].begin(), v[0].end());
    sort(v[1].begin(), v[1].end());
    vector<int> vr;
    st.clear();
    for (int i = 0; i < n; i++)
      a[i] = -1, st.insert(i);
    int ta = n - 1;
    for (int i = 0; i < v[0].size(); i++) {
      int t = v[0][i].Snd;
      set<int>::iterator it = st.upper_bound(k[t] - 1);
      if (it != st.begin())
        it--, a[*it] = t, st.erase(it);
      else {
        a[ta] = t, st.erase(ta);
        while (a[ta] != -1 && ta >= 0)
          ta--;
      }
    }
    for (int i = 0; i < n; i++)
      if (a[i] != -1)
        vr.push_back(a[i]);
    // cout <<v[0].size()<<" "<<vr.size()<<" ##\\n";
    for (int i = 0; i < v[2].size(); i++)
      vr.push_back(v[2][i].Snd);
    for (int i = 0; i < n; i++)
      a[i] = -1, st.insert(i);
    ta = 0;
    for (int i = 0; i < v[1].size(); i++) {
      int t = v[1][i].Snd;
      set<int>::iterator it = st.lower_bound(k[t]);
      // cout <<*it<<" ##\\n";
      if (it != st.end())
        a[*it] = t, st.erase(it);
      else {
        a[ta] = t, st.erase(ta);
        while (a[ta] != -1 && ta < n)
          ta++;
      }
    }
    for (int i = 0; i < n; i++)
      if (a[i] != -1)
        vr.push_back(a[i]);
    // cout <<v[1].size()<<" "<<vr.size()<<" ##\\n";
    ll res = 0;
    for (int i = 0; i < n; i++) {
      int t = vr[i];
      // cout <<vr[i]<<" ##\\n";
      if (i < k[t])
        res += l[t];
      else
        res += r[t];
    }
    printf("%lld\\n", res);
  }
  return 0;
}