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

// Problem Link: https://atcoder.jp/contests/aising2020/tasks/aising2020_e
// Tutorial Link: https://img.atcoder.jp/aising2020/editorial.pdf

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb push_back
#define pLL pair<LL, LL>
#define ff first
#define ss second
#define rep(i, a, b) for (LL i = a; i <= b; ++i)
#define repb(i, a, b) for (LL i = a; i >= b; --i)
#define ld double
#define mp make_pair
#define vLL vector<LL>
#define vpLL vector<pLL>
#define vld vector<ld>
#define pld pair<ld, ld>
#define vpld vector<pld>
#define SLL set<LL>
#define SpLL set<pLL>
#define FIO ios::sync_with_stdio(false)
#define mod 1000000007

LL t, n;
vpLL l, r;

priority_queue<LL> pq;

inline LL process(vpLL &v) {
  LL cur = 0, ret = 0;
  sort(v.begin(), v.end());
  for (auto [k, val] : v) {
    if (k != cur) {
      if (!pq.empty())
        while (pq.size() > cur)
          pq.pop();
      cur = k;
    }
    pq.push(-val);
  }
  while (pq.size() > cur)
    pq.pop();
  while (!pq.empty()) {
    ret -= pq.top();
    pq.pop();
  }
  return ret;
}

int main() {
  FIO;
  cin >> t;
  while (t--) {
    cin >> n;
    LL ans = 0;
    l.clear();
    r.clear();
    rep(i, 1, n) {
      LL k, v1, v2;
      cin >> k >> v1 >> v2;
      if (k == n) {
        ans += v1;
        continue;
      }
      ans += min(v1, v2);
      if (v1 > v2)
        l.pb(mp(k, abs(v1 - v2)));
      else if (v1 < v2)
        r.pb(mp((n - k), abs(v1 - v2)));
    }

    ans += process(l);
    ans += process(r);

    cout << ans << "\\n";
  }
}

Error: 0

Fixed submission:

// Problem Link:
// Tutorial Link:

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb push_back
#define pLL pair<LL, LL>
#define ff first
#define ss second
#define rep(i, a, b) for (LL i = a; i <= b; ++i)
#define repb(i, a, b) for (LL i = a; i >= b; --i)
#define ld double
#define mp make_pair
#define vLL vector<LL>
#define vpLL vector<pLL>
#define vld vector<ld>
#define pld pair<ld, ld>
#define vpld vector<pld>
#define SLL set<LL>
#define SpLL set<pLL>
#define FIO ios::sync_with_stdio(false)
#define mod 1000000007

LL t, n;
vpLL order;

LL bit[200005];

void update(LL x, LL val) {
  LL idx = x;
  while (idx <= n) {
    bit[idx] = max(bit[idx], val);
    idx += idx & (-idx);
  }
}

LL query(LL x) {
  LL idx = x, ans = 0;
  while (idx > 0) {
    ans = max(bit[idx], ans);
    idx -= (idx) & (-idx);
  }
  return ans;
}

int main() {
  FIO;
  cin >> t;
  while (t--) {
    cin >> n;
    LL ans = 0;
    rep(i, 1, n) bit[i] = 0;
    order.clear();
    rep(i, 1, n) {
      LL k, v1, v2;
      cin >> k >> v1 >> v2;
      if (k == n) {
        ans += v1;
        continue;
      }
      ans += min(v1, v2);
      if (v1 > v2)
        order.pb(mp(abs(v1 - v2), k));
      else if (v1 < v2)
        order.pb(mp(abs(v2 - v1), (k - n)));
    }
    sort(order.begin(), order.end());
    reverse(order.begin(), order.end());

    for (auto x : order) {
      if (x.ss > 0) {
        LL val = query(x.ff);
        if (val < x.ss) {
          ans += x.ff;
          update(x.ss, 1);
        }
      } else {
        LL val = query(n) - query(1 - x.ss);
        if (val < -x.ss) {
          ans += x.ff;
          update(n, 1);
        }
      }
    }

    cout << ans << "\\n";
  }
}