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

#include <bits/stdc++.h>
#define ll long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int n;
vector<pii_> lft, rgt;
ll cal(vector<pii_> a) {
  int n = a.size();
  ll res = 0;
  set<int> pos;
  rep(i, 1, n) pos.insert(i);
  for (auto x : a) {
    auto it = pos.upper_bound(x.se);
    if (it != pos.begin()) {
      it--;
      res += x.fi;
      pos.erase(it);
    }
  }
  return res;
}
int main() {
  fastio();
  int _;
  for (cin >> _; _; _--) {
    cin >> n;
    ll ans = 0;
    lft.clear(), rgt.clear();
    rep(i, 1, n) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r)
        lft.pb({l - r, k}), ans += r;
      else
        rgt.pb({r - l, n - k}), ans += l;
    }
    sort(lft.begin(), lft.end());
    sort(rgt.begin(), rgt.end());
    reverse(lft.begin(), lft.end());
    reverse(rgt.begin(), rgt.end());
    ans = ans + cal(lft) + cal(rgt);
    cout << ans << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii_ pair<int, int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define show1(a) cout << #a << " = " << a << endl
#define show2(a, b) cout << #a << " = " << a << "; " << #b << " = " << b << endl
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int n, K[maxn];
ll tr[maxn << 5], lazy[maxn << 5];
ll L[maxn], R[maxn];
vector<int> lft, rgt;
bool cmp1(int x, int y) { return L[x] - R[x] > L[y] - R[y]; }
bool cmp2(int x, int y) { return R[x] - L[y] > R[y] - L[y]; }
void push_up(int id) { tr[id] = tr[id << 1] + tr[id << 1 | 1]; }
void push_down(int id) {
  if (lazy[id] == 0)
    return;
  int k = lazy[id];
  lazy[id] = 0;
  tr[id << 1] += k, lazy[id] += k;
  tr[id << 1 | 1] += k, lazy[id << 1 | 1] += k;
}
void build(int id, int l, int r) {
  tr[id] = lazy[id] = 0;
  if (l == r) {
    tr[id] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
  push_up(id);
}
void update(int id, int stl, int str, int l, int r) {
  if (stl == l && str == r) {
    tr[id] -= 1;
    lazy[id] -= 1;
    return;
  }
  push_down(id);
  int mid = (stl + str) >> 1;
  if (r <= mid)
    update(id << 1, stl, mid, l, r);
  else if (l > mid)
    update(id << 1 | 1, mid + 1, str, l, r);
  else
    update(id << 1, stl, mid, l, mid),
        update(id << 1 | 1, mid + 1, str, mid + 1, r);
  push_up(id);
}
int query(int id, int stl, int str, int l, int r) {
  if (stl == l && str == r)
    return tr[id];
  push_down(id);
  int mid = (stl + str) >> 1;
  int res;
  if (r <= mid)
    res = query(id << 1, stl, mid, l, r);
  else if (l > mid)
    res = query(id << 1 | 1, mid + 1, str, l, r);
  else
    res = query(id << 1, stl, mid, l, mid) +
          query(id << 1 | 1, mid + 1, str, mid + 1, r);
  push_up(id);
  return res;
}
int main() {
  fastio();
  int _;
  for (cin >> _; _; _--) {
    cin >> n;
    ll ans = 0;
    build(1, 1, n);
    lft.clear();
    rgt.clear();
    rep(i, 1, n) cin >> K[i] >> L[i] >> R[i];
    rep(i, 1, n) {
      if (L[i] > R[i])
        lft.pb(i);
      else
        rgt.pb(i);
    }
    sort(lft.begin(), lft.end(), cmp1);
    sort(rgt.begin(), rgt.end(), cmp2);
    for (int x : lft) {
      if (K[x] == n) {
        ans += L[x];
        continue;
      }
      if (query(1, 1, n, 1, K[x]) > 0) {
        ans += L[x];
        int l = 1, r = K[x], res;
        while (l <= r) {
          int mid = (l + r) >> 1;
          if (query(1, 1, n, mid, K[x]) > 0)
            res = mid, l = mid + 1;
          else
            r = mid - 1;
        }
        update(1, 1, n, res, K[x]);
      } else {
        ans += R[x];
      }
    }
    for (int x : rgt) {
      if (K[x] == n) {
        ans += L[x];
        continue;
      }
      if (query(1, 1, n, K[x] + 1, n) > 0) {
        ans += R[x];
        int l = K[x] + 1, r = n, res;
        while (l <= r) {
          int mid = (l + r) >> 1;
          if (query(1, 1, n, K[x] + 1, mid) > 0)
            res = mid, r = mid - 1;
          else
            l = mid + 1;
        }
        update(1, 1, n, res, n);
      } else {
        ans += L[x];
      }
    }
    cout << ans << endl;
  }
  return 0;
}