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



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

#define ll long long int
#define ld long double
#define mod 998244353
#define big 1e18 + 10
#define small -big
#define pb push_back
#define endl "\\n"

template <typename T>

void prarr(T a) {
  cerr << "[ ";

  for (auto i : a) {
    cerr << i << " ";
  }

  cerr << "]" << endl;
}

#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char *name, Arg1 &&arg1) {
  cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char *names, Arg1 &&arg1, Args &&...args) {

  const char *comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << " : " << arg1 << "] ";
  _er(comma + 2, args...);
}

tuple<ll, ll, ll> A[200005];
vector<tuple<ll, ll, ll>> C;
vector<pair<ll, ll>> B;
set<ll> g;
set<pair<ll, ll>> curr;
ll ans[200005];

void solve() {

  g.clear();
  curr.clear();
  B.clear();
  C.clear();

  ll N;
  cin >> N;

  for (ll i = 1; i <= N; i++) {
    ans[i] = 0;
  }

  for (ll i = 1; i <= N; i++) {

    ll k, l, r;
    cin >> k >> l >> r;
    A[i] = {k, l, r};
    g.insert(i);
    if ((l - r) > 0) {
      C.pb({(l - r), k, i});
    } else {
      B.pb({(r - l), i});
    }
  }

  sort(C.begin(), C.end());
  reverse(C.begin(), C.end());

  for (auto it : C) {
    ll diff = get<0>(it);
    ll k = get<1>(it);
    ll ind = get<2>(it);
    auto jt = g.upper_bound(k);

    if (jt == g.begin()) {
      B.pb({-1 * diff, ind});
      continue;
    } else {
      jt--;
      curr.insert({*jt, ind});
      g.erase(jt);
    }
  }

  g.clear();
  for (ll i = 1; i <= N; i++) {
    g.insert(i);
  }

  ll ans_ind = 1;
  for (auto it : curr) {
    ans[ans_ind] = it.second;
    g.erase(ans_ind);
    ans_ind++;
  }

  sort(B.begin(), B.end());

  reverse(B.begin(), B.end());

  for (auto it : B) {
    ll ind = it.second;
    ll k = get<0>(A[ind]);
    auto jt = g.upper_bound(k);
    if (jt != g.end()) {
      ans[*jt] = ind;
      g.erase(jt);
    } else {
      jt = g.begin();
      ans[*jt] = ind;
      g.erase(jt);
    }
  }

  assert(g.size() == 0);

  ll sum = 0;
  set<ll> ge;

  for (ll i = 1; i <= N; i++) {
    ll ind = ans[i];
    assert(ind >= 1 && ind <= N);
    ge.insert(ind);
    ll k = get<0>(A[ind]);
    ll l = get<1>(A[ind]);
    ll r = get<2>(A[ind]);

    if (i <= k) {
      sum += l;
    } else {
      sum += r;
    }
  }

  assert(g.size() == 0 && ge.size() == N);

  cout << sum << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  cin >> t;

  for (int i = 1; i <= t; i++) {

    solve();
  }

  return 0;
}


Error: 0

Fixed submission:



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

#define ll long long int
#define ld long double
#define mod 998244353
#define big 1e18 + 10
#define small -big
#define pb push_back
#define endl "\\n"

template <typename T>

void prarr(T a) {
  cerr << "[ ";

  for (auto i : a) {
    cerr << i << " ";
  }

  cerr << "]" << endl;
}

#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char *name, Arg1 &&arg1) {
  cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char *names, Arg1 &&arg1, Args &&...args) {

  const char *comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << " : " << arg1 << "] ";
  _er(comma + 2, args...);
}

tuple<ll, ll, ll> A[200005];
vector<tuple<ll, ll, ll>> C;
vector<pair<ll, ll>> B;
set<ll> g;
set<pair<ll, ll>> curr;
ll ans[200005];

void solve() {

  g.clear();
  curr.clear();
  B.clear();
  C.clear();

  ll N;
  cin >> N;

  for (ll i = 1; i <= N; i++) {
    ans[i] = 0;
  }

  ll ma = 0;

  for (ll i = 1; i <= N; i++) {

    ll k, l, r;
    cin >> k >> l >> r;
    A[i] = {k, l, r};
    ma = max(ma, k);
    g.insert(i);
    if ((l - r) > 0) {
      C.pb({(l - r), k, i});
    } else {
      B.pb({(r - l), i});
    }
  }

  sort(C.begin(), C.end());
  reverse(C.begin(), C.end());

  for (auto it : C) {
    ll diff = get<0>(it);
    ll k = get<1>(it);
    ll ind = get<2>(it);
    auto jt = g.upper_bound(k);
    jt--;
    if (*jt > k) {
      B.pb({-1 * diff, ind});
      continue;
    }
    curr.insert({*jt, ind});
    g.erase(jt);
  }

  g.clear();
  for (ll i = 1; i <= N; i++) {
    g.insert(i);
  }

  ll ans_ind = 1;
  for (auto it : curr) {
    ans[ans_ind] = it.second;
    g.erase(ans_ind);
    ans_ind++;
  }

  sort(B.begin(), B.end());

  reverse(B.begin(), B.end());

  for (auto it : B) {
    ll ind = it.second;
    ll k = get<0>(A[ind]);
    auto jt = g.upper_bound(k);
    if (jt != g.end()) {
      ans[*jt] = ind;
      g.erase(jt);
    } else {
      jt = g.begin();
      ans[*jt] = ind;
      g.erase(jt);
    }
  }

  assert(g.size() == 0);

  ll sum = 0;
  set<ll> ge;

  for (ll i = 1; i <= N; i++) {
    ll ind = ans[i];
    assert(ind >= 1 && ind <= N);
    ge.insert(ind);
    ll k = get<0>(A[ind]);
    ll l = get<1>(A[ind]);
    ll r = get<2>(A[ind]);

    if (i <= k) {
      sum += l;
    } else {
      sum += r;
    }
  }

  assert(g.size() == 0 && ge.size() == N);

  cout << sum << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  cin >> t;

  for (int i = 1; i <= t; i++) {

    solve();
  }

  return 0;
}