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

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;

using namespace std;
const int maxn = 2e5 + 100;
const int inf = 1e9 + 7;
int n, T;
ll ans;
vector<pair<int, int>> r1, r2;
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &T);
  while (T--) {
    ans = 0;
    r1.clear(), r2.clear();
    scanf("%d", &n);
    rep(i, 1, n) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l > r)
        r1.push_back(make_pair(k, l - r)), ans += r;
      else if (l < r)
        r2.push_back(make_pair(n - k, r - l)), ans += l;
      else
        ans += l;
    }
    if (!r1.empty())
      sort(r1.begin(), r1.end());
    if (!r2.empty())
      sort(r2.begin(), r2.end());
    priority_queue<int, vector<int>, greater<>> q;
    for (auto v : r1) {
      q.push(v.second);
      ans += v.second;
      while (q.size() > v.first)
        ans -= q.top(), q.pop();
    }
    while (!q.empty())
      q.pop();
    for (auto v : r2) {
      q.push(v.second);
      ans += v.second;
      while (q.size() > v.first)
        ans -= q.top(), q.pop();
    }
    printf("%lld\\n", ans);
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;

using namespace std;
const int maxn = 2e5 + 100;
const int inf = 1e9 + 7;
int n, T;
struct Node {
  int mn, mx, use, l, r;
} t[4 * maxn];
ll ans;
int cnt = 0;
void build(int x, int l, int r) {
  if (l == r) {
    t[x].mn = t[x].mx = l, t[x].use = 0;
    return;
  }
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  t[x].use = t[x << 1].use & t[x << 1 | 1].use;
  t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
  t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
}
void update(int x, int l, int r) {
  if (l == r) {
    t[x].use = 1;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    update(x << 1, l, mid);
  else
    update(x << 1 | 1, mid + 1, r);
  t[x].use = t[x << 1].use & t[x << 1 | 1].use;
  t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
  t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
}
void pushup(int x) {
  t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
  t[x].use = t[x << 1].use & t[x << 1 | 1].use;
  t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
  if (x > 1)
    pushup(x >> 1);
}
int query(int x, int l, int r, int st, int ed, int dir) {
  // 	cout<<x<<' '<<l<<' '<<r<<'$'<<t[x].mn<<' '<<t[x].mx<<'
  // '<<t[x].use<<'$'<<st<<' '<<ed<<' '<<dir<<endl;

  if (l == r) {
    if (t[x].use == 0) {
      t[x].mn = inf, t[x].mx = -1, t[x].use = 1;
      pushup(x);
      return 1;
    } else
      return 0;
  }
  int res = 0;
  if (t[x].use == 0) {
    if (dir && t[x].mn <= ed) {
      int mid = l + r >> 1;
      //            cout<<max(t[x<<1|1].mn,ed)<<' '<<max(t[x<<1].mn,ed)<<endl;
      if (ed > mid)
        res |= query(x << 1 | 1, mid + 1, r, min(t[x << 1 | 1].mn, ed),
                     max(t[x << 1 | 1].mn, ed), dir);
      if (!res && st <= mid)
        res |= query(x << 1, l, mid, min(t[x << 1].mn, ed),
                     max(t[x << 1].mn, ed), dir);
      return res;
    } else if (!dir && t[x].mx >= st) {
      int mid = l + r >> 1;
      if (st <= mid)
        res |= query(x << 1, l, mid, min(t[x << 1].mx, st),
                     max(t[x << 1].mx, st), dir);
      if (!res && ed > mid)
        res |= query(x << 1 | 1, mid + 1, r, min(t[x << 1 | 1].mx, ed),
                     max(t[x << 1 | 1].mx, ed), dir);

      return res;
    }
  }
  return res;
}
struct Tmp {
  int k, l, r;
} a[maxn];
vector<Tmp> r1, r2;
bool cmp1(Tmp x, Tmp y) { return x.l > y.l; }
bool cmp2(Tmp x, Tmp y) { return x.r > y.r; }
int main() {
  //	freopen("in.txt","r",stdin);
  scanf("%d", &T);
  while (T--) {
    ans = 0;
    r1.clear(), r2.clear();
    scanf("%d", &n);
    rep(i, 1, n) {
      scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r);
      if (a[i].l > a[i].r)
        r1.push_back(a[i]);
      else if (a[i].l < a[i].r)
        r2.push_back(a[i]);
      else
        ans += a[i].l;
    }
    build(1, 1, n);

    //        rep(i,1,40)cout<<t[i].mn<<' '<<t[i].mx<<endl;

    sort(r1.begin(), r1.end(), cmp1);
    sort(r2.begin(), r2.end(), cmp2);

    for (auto v : r1) {
      if (query(1, 1, n, 1, v.k, 1))
        ans += max(v.l, v.r);
      else
        ans += min(v.l, v.r);
      //            tmp(ans);
    }
    for (auto v : r2) {
      if (query(1, 1, n, v.k + 1, n, 0))
        ans += max(v.l, v.r);
      else
        ans += min(v.l, v.r);
      //        	tmp(ans);
    }
    printf("%lld\\n", ans);
  }

  return 0;
}