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

#include <bits/stdc++.h>

using namespace std;

const int inf = 1000000001;

struct SegmentTree {
  vector<int> val;
  vector<int> mn;

public:
  SegmentTree(int N) : val(N * 4 + 5), mn(N * 4 + 5) {}

  void build(int l, int r, int x) {
    if (l == r) {
      mn[x] = l;
      return;
    }
    int mid = l + r >> 1;
    build(l, mid, x << 1);
    build(mid + 1, r, x << 1 | 1);
    mn[x] = r;
  }

  void update(int l, int r, int x, int p, int v) {
    if (l == r) {
      val[l] = v;
      return;
    }
    int mid = l + r >> 1;
    if (p <= mid) {
      update(l, mid, x << 1, p, v);
    } else {
      update(mid + 1, r, x << 1 | 1, p, v);
    }
    if (val[mn[x << 1]] <= val[mn[x << 1 | 1]]) {
      mn[x] = mn[x << 1];
    } else {
      mn[x] = mn[x << 1 | 1];
    }
  }

  int query(int l, int r, int x, int a, int b) {
    if (l > b || r < a) {
      return -1;
    }
    if (l >= a && r <= b) {
      return mn[x];
    }
    int mid = l + r >> 1;
    int L = query(l, mid, x << 1, a, b);
    int R = query(mid + 1, r, x << 1 | 1, a, b);
    if (L == -1) {
      return R;
    }
    if (R == -1) {
      return L;
    }
    if (val[L] < val[R]) {
      return L;
    } else {
      return R;
    }
  }

  int V(int p) { return val[p]; }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int T;
  cin >> T;

  while (T--) {
    int n;
    cin >> n;

    vector<int> k(n);
    vector<int> l(n);
    vector<int> r(n);
    vector<tuple<int, int, int>> a;
    vector<tuple<int, int, int>> b;
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      if (k == n) {
        ans += l;
      } else if (l >= r) {
        a.emplace_back(k, l, r);
      } else {
        b.emplace_back(n - k, r, l);
      }
    }

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    auto solve = [&](const vector<tuple<int, int, int>> a) {
      int n = a.size();
      if (n == 0) {
        return 0LL;
      }
      SegmentTree seg(n + 1);
      seg.build(1, n, 1);
      long long ans = 0;
      for (int i = 0; i < n; ++i) {
        auto [k, l, r] = a[i];
        if (k > i) {
          ans += l;
          seg.update(1, n, 1, i + 1, l - r);
        } else {
          int pos = seg.query(1, n, 1, 1, k);
          if (l - r > seg.V(pos)) {
            ans -= seg.V(pos);
            ans += l;
            seg.update(1, n, 1, pos, l - r);
          } else {
            ans += r;
          }
        }
      }

      return ans;
    };

    cout << ans + solve(a) + solve(b) << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int T;
  cin >> T;

  while (T--) {
    int n;
    cin >> n;

    vector<int> k(n);
    vector<int> l(n);
    vector<int> r(n);
    vector<tuple<int, int, int>> a;
    vector<tuple<int, int, int>> b;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l >= r) {
        a.emplace_back(k, l, r);
      } else {
        b.emplace_back(n - k, r, l);
      }
    }

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    auto solve = [&](const vector<tuple<int, int, int>> a) {
      priority_queue<int, vector<int>, greater<int>> q;
      long long ans = 0;
      for (int i = 0; i < a.size(); ++i) {
        auto [k, l, r] = a[i];
        if (k == 0) {
          ans += r;
          continue;
        }
        if (k > i) {
          ans += l;
          q.push(l - r);
        } else {
          if (l - r > q.top()) {
            ans -= q.top();
            ans += l;
            q.pop();
            q.push(l - r);
          } else {
            ans += r;
          }
        }
      }

      return ans;
    };

    cout << solve(a) + solve(b) << '\\n';
  }

  return 0;
}