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

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

long long f(vector<pair<long long, long long>> &x) {
  priority_queue<long long, vector<long long>, greater<long long>> candidates;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  for (pair<long long, long long> p : x) {
    q.push(p);
  }
  long long current = 1;
  while (!q.empty() && size_t(current) <= x.size()) {
    long long index = q.top().first;
    if (index < current) {
      q.pop();
      continue;
    }
    if (index > current) {
      while (size_t(current) < candidates.size()) {
        candidates.pop();
      }
      current = index;
    } else {
      long long diff = q.top().second;
      q.pop();
      candidates.push(diff);
    }
  }
  while (size_t(current) < candidates.size()) {
    candidates.pop();
  }
  while (!q.empty()) {
    candidates.push(q.top().second);
    q.pop();
  }

  long long ret = 0;
  while (!candidates.empty()) {
    ret += candidates.top();
    candidates.pop();
  }
  return ret;
}

void solve() {
  long long N;
  cin >> N;
  vector<long long> K(N);
  vector<long long> R(N);
  vector<long long> L(N);
  vector<pair<long long, long long>> right;
  vector<pair<long long, long long>> left;
  long long ans = 0;
  for (long long n = 0; n < N; n++) {
    cin >> K[n] >> R[n] >> L[n];
    if (R[n] > L[n]) {
      right.push_back(make_pair(K[n], R[n] - L[n]));
    } else if (R[n] < L[n]) {
      left.push_back(make_pair(N - K[n], L[n] - R[n]));
    }
    ans += min(R[n], L[n]);
  }

  ans += f(right);
  ans += f(left);
  cout << ans << endl;
}

int main() {
  long long T;
  cin >> T;
  for (long long t = 0; t < T; t++) {
    solve();
  }
}


Error: 0

Fixed submission:

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

long long f(vector<pair<long long, long long>> &x) {
  assert(x.size() > 0);
  priority_queue<long long, vector<long long>, greater<long long>> candidates;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  for (pair<long long, long long> p : x) {
    q.push(p);
  }
  long long current = 1;
  while (!q.empty() && current <= x.size()) {
    long long index = q.top().first;
    if (index < current) {
      q.pop();
      continue;
    }
    if (index > current) {
      while (size_t(current) < candidates.size()) {
        candidates.pop();
      }
      current = index;
    } else {
      long long diff = q.top().second;
      q.pop();
      candidates.push(diff);
    }
  }

  while (!q.empty()) {
    candidates.push(q.top().second);
    q.pop();
  }

  long long ret = 0;
  while (!candidates.empty()) {
    ret += candidates.top();
    candidates.pop();
  }
  return ret;
}

void solve() {
  long long N;
  cin >> N;
  vector<long long> K(N);
  vector<long long> R(N);
  vector<long long> L(N);
  vector<pair<long long, long long>> right;
  vector<pair<long long, long long>> left;
  long long ans = 0;
  for (long long n = 0; n < N; n++) {
    cin >> K[n] >> R[n] >> L[n];
    if (R[n] > L[n]) {
      right.push_back(make_pair(K[n], R[n] - L[n]));
    } else if (R[n] < L[n]) {
      left.push_back(make_pair(N - K[n], L[n] - R[n]));
    }
    ans += min(R[n], L[n]);
  }

  ans += f(right);
  ans += f(left);
  cout << ans << endl;
}

int main() {
  long long T;
  cin >> T;
  for (long long t = 0; t < T; t++) {
    solve();
  }
}