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

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

typedef long long lnt;

const int N = 4e5 + 10;

struct T {
  int k, v;
};

T a[N], b[N];

#define lson st, (st + en) >> 1, rt << 1
#define rson ((st + en) >> 1) + 1, en, rt << 1 | 1

// struct {

int x[N << 2];

void pu(int rt) { x[rt] = min(x[rt << 1 | 1], x[rt << 1]); }

void modify(int p, int v, int st, int en, int rt) {
  if (st == en) {
    x[rt] = 1;
    return;
  }
  if (p <= ((st + en) >> 1))
    modify(p, v, lson);
  else
    modify(p, v, rson);
  pu(rt);
}

int query(int l, int r, int st, int en, int rt) {
  if (l > en || r < st)
    return 1;
  if (l <= st && en <= r)
    return x[rt];
  return min(query(l, r, lson), query(l, r, rson));
}

void build(int st, int en, int rt) {
  x[rt] = 0;
  if (st == en)
    return;
  build(lson);
  build(rson);
  pu(rt);
}

// } st;

lnt solve(T *a, int n) {
  if (n == 0)
    return 0;
  sort(a + 1, a + n + 1, [&](T x, T y) {
    if (x.v != y.v)
      return x.v > y.v;
    return x.k > y.k;
  });
  lnt ret = 0;
  build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    int st = 1, en = a[i].k;
    if (a[i].k > n) {
      ret += a[i].v;
      continue;
    }
    if (a[i].k == 0)
      continue;
    if (query(st, en, 1, n, 1) != 0)
      continue;
    while (st != en) {
      int md = (st + en) >> 1;
      if (query(md + 1, en, 1, n, 1) == 0)
        st = md + 1;
      else
        en = md;
    }
    ret += a[i].v;
    modify(st, 1, 1, n, 1);
    // cout << st << "!!" << endl;
  }
  return ret;
}

int main(void) {
  int kase;
  scanf("%d", &kase);
  while (kase--) {
    int n;
    scanf("%d", &n);

    lnt sum = 0;
    int na = 0, nb = 0;
    for (int i = 1, k, l, r; i <= n; i++) {
      scanf("%d%d%d", &k, &l, &r);
      sum += min(l, r);
      if (l > r)
        a[++na] = {k, l - r};
      else if (r > l)
        b[++nb] = {n - k, r - l};
    }
    // cout << na << " " << sum << endl;
    sum += solve(a, na);
    // cout << nb << " " << sum << endl;
    sum += solve(b, nb);
    // cout << sum << endl;
    printf("%lld\\n", sum);
  }
}

Error: 0

Fixed submission:

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

typedef long long lnt;

const int N = 2e5 + 10;

struct T {
  int k, v;
};

T a[N], b[N];

#define lson st, (st + en) >> 1, rt << 1
#define rson ((st + en) >> 1) + 1, en, rt << 1 | 1

// struct {

int x[N << 2];

void pu(int rt) { x[rt] = min(x[rt << 1 | 1], x[rt << 1]); }

void modify(int p, int v, int st, int en, int rt) {
  if (st == en) {
    x[rt] = 1;
    return;
  }
  if (p <= ((st + en) >> 1))
    modify(p, v, lson);
  else
    modify(p, v, rson);
  pu(rt);
}

int query(int l, int r, int st, int en, int rt) {
  if (l > en || r < st)
    return 1;
  if (l <= st && en <= r)
    return x[rt];
  return min(query(l, r, lson), query(l, r, rson));
}

void build(int st, int en, int rt) {
  x[rt] = 0;
  if (st == en)
    return;
  build(lson);
  build(rson);
  pu(rt);
}

// } st;

lnt solve(T *a, int n) {
  sort(a + 1, a + n + 1, [&](T x, T y) {
    if (x.v != y.v)
      return x.v > y.v;
    return x.k > y.k;
  });
  lnt ret = 0;
  build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    int st = 1, en = a[i].k;
    if (a[i].k > n) {
      ret += a[i].v;
      continue;
    }
    if (a[i].k == 0)
      continue;
    if (query(st, en, 1, n, 1) != 0) {
      continue;
    }
    while (st != en) {
      int md = (st + en) >> 1;
      if (query(md + 1, en, 1, n, 1) == 0)
        st = md + 1;
      else
        en = md;
    }
    ret += a[i].v;
    modify(st, 1, 1, n, 1);
    // cout << st << "!!" << endl;
  }
  return ret;
}

int main(void) {
  int kase;
  scanf("%d", &kase);
  while (kase--) {
    int n;
    scanf("%d", &n);

    lnt sum = 0;
    int na = 0, nb = 0;
    for (int i = 1, k, l, r; i <= n; i++) {
      scanf("%d%d%d", &k, &l, &r);
      sum += min(l, r);
      if (l > r)
        a[++na] = {k, l - r};
      else if (r > l)
        b[++nb] = {n - k, r - l};
    }
    // cout << na << " " << sum << endl;
    sum += solve(a, na);
    // cout << nb << " " << sum << endl;
    sum += solve(b, nb);
    // cout << sum << endl;
    printf("%lld\\n", sum);
  }
}