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

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 4e5 + 5;
struct node {
  int k, l, r;
  node(int k = 0, int l = 0, int r = 0) : k(k), l(l), r(r) {}
};
struct haha {
  int id, v, k;
  haha(int id = 0, int v = 0, int k = 0) : id(id), v(v), k(k) {}
  bool operator<(const haha &o) const {
    if (v == o.v)
      return k < o.k;
    return v < o.v;
  }
};
vector<node> vl, vr;
int n, t[N << 2], lz[N << 2], ss[N];
ll ans = 0;
ll s[2][N];
void update(int l, int r, int k) {
  t[k] -= lz[k];
  assert(t[k] >= 0);
  if (l != r)
    lz[k << 1] += lz[k], lz[k << 1 | 1] += lz[k];
  lz[k] = 0;
}
int query(int l, int r, int k, int x) {
  if (lz[k])
    update(l, r, k);
  if (r < x)
    return 1;
  if (l >= x)
    return t[k];
  int m = l + r >> 1;
  int ans = min(query(l, m, k << 1, x), query(m + 1, r, k << 1 | 1, x));
  t[k] = min(t[k << 1], t[k << 1 | 1]);
  return ans;
}
void fix(int l, int r, int k, int x) {
  if (lz[k])
    update(l, r, k);
  if (r < x)
    return;
  if (l >= x) {
    lz[k]++;
    update(l, r, k);
    return;
  }
  int m = l + r >> 1;
  fix(l, m, k << 1, x);
  fix(m + 1, r, k << 1 | 1, x);
  t[k] = min(t[k << 1], t[k << 1 | 1]);
}
void build(int l, int r, int k) {
  t[k] = l;
  lz[k] = 0;
  if (l == r)
    return;
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
}
void solve(vector<node> v, int p) {
  build(1, n, 1);
  for (int i = 1; i <= n; i++)
    ss[i] = i;
  priority_queue<haha> q;
  ll sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += v[i].r, q.push(haha(i, v[i].l - v[i].r, v[i].k));
  s[p][0] = sum;
  for (int i = 1; i <= n; i++) {
    int l = 1, r = n, pos = 0;
    while (l <= r) {
      int m = l + r >> 1;
      if (query(1, n, 1, m) == 0)
        pos = m, l = m + 1;
      else
        r = m - 1;
    }
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      if (v[id].k == 0)
        continue;
      if (v[id].k > pos) {
        // cout<<pos<<' '<<v[id].l<<' '<<v[id].r<<' '<<v[id].k<<endl;
        sum = sum - v[id].r + v[id].l;
        fix(1, n, 1, v[id].k);
        break;
      }
    }
    s[p][i] = max(sum, s[p][i - 1]);
  }
}
fstream in, out;
void getts() {
  int t = 50;
  in.open("1.txt");
  in << t << endl;
  while (t--) {
    int n = 1000;
    in << n << endl;
    for (int i = 1; i <= n; i++) {
      int k = rand() % n + 1, l = rand() % 100 + 1, r = rand() % 100 + 1;
      in << k << ' ' << l << ' ' << r << endl;
    }
  }
  in.close();
}
bool de = false;
int main() {
  srand(time(0));
  // getts();freopen("1.txt","r",stdin);out.open("out.txt");de=true;
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    vl.clear();
    vr.clear();
    ans = 0;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l == r)
        ans += l;
      else if (l > r)
        vl.push_back({k, l, r});
      else
        vr.push_back({n - k, r, l});
    }
    for (int i = 0; i <= n; i++)
      s[0][i] = s[1][i] = 0;
    solve(vl, 0);
    solve(vr, 1);
    ll res = 0;
    for (int i = 0; i <= n; i++)
      res = max(res, s[0][i] + s[1][n - i]);
    if (!de)
      printf("%lld\\n", ans + res);
    if (de)
      out << ans + res << endl;
  }
}
/*
1
5
4 65 49
4 59 67
3 66 67
4 98 68
3 32 91
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 4e5 + 5;
struct node {
  int k, l, r;
  node(int k = 0, int l = 0, int r = 0) : k(k), l(l), r(r) {}
};
struct haha {
  int id, v, k;
  haha(int id = 0, int v = 0, int k = 0) : id(id), v(v), k(k) {}
  bool operator<(const haha &o) const {
    if (v == o.v)
      return k < o.k;
    return v < o.v;
  }
};
vector<node> vl, vr;
int n, t[N << 2], lz[N << 2], ss[N];
ll ans = 0;
ll s[2][N];
void update(int l, int r, int k) {
  t[k] -= lz[k];
  assert(t[k] >= 0);
  if (l != r)
    lz[k << 1] += lz[k], lz[k << 1 | 1] += lz[k];
  lz[k] = 0;
}
int query(int l, int r, int k) {
  if (lz[k])
    update(l, r, k);
  if (t[k] != 0)
    return 0;
  if (l == r)
    return l;
  int m = l + r >> 1;
  if (t[k << 1 | 1] == 0)
    return query(m + 1, r, k << 1 | 1);
  return query(l, m, k << 1);
}
void fix(int l, int r, int k, int x) {
  if (lz[k])
    update(l, r, k);
  if (r < x)
    return;
  if (l >= x) {
    lz[k]++;
    update(l, r, k);
    return;
  }
  int m = l + r >> 1;
  fix(l, m, k << 1, x);
  fix(m + 1, r, k << 1 | 1, x);
  t[k] = min(t[k << 1], t[k << 1 | 1]);
}
void build(int l, int r, int k) {
  t[k] = l;
  lz[k] = 0;
  if (l == r)
    return;
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
}
void solve(vector<node> v, int p) {
  build(1, n, 1);
  for (int i = 1; i <= n; i++)
    ss[i] = i;
  priority_queue<haha> q;
  ll sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += v[i].r, q.push(haha(i, v[i].l - v[i].r, v[i].k));
  s[p][0] = sum;
  for (int i = 1; i <= n; i++) {
    int l = 1, r = n, pos = query(1, n, 1);
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      if (v[id].k == 0)
        continue;
      if (v[id].k > pos) {
        // cout<<pos<<' '<<v[id].l<<' '<<v[id].r<<' '<<v[id].k<<endl;
        sum = sum - v[id].r + v[id].l;
        fix(1, n, 1, v[id].k);
        break;
      }
    }
    s[p][i] = max(sum, s[p][i - 1]);
  }
}
fstream in, out;
void getts() {
  int t = 50;
  in.open("1.txt");
  in << t << endl;
  while (t--) {
    int n = 1000;
    in << n << endl;
    for (int i = 1; i <= n; i++) {
      int k = rand() % n + 1, l = rand() % 100 + 1, r = rand() % 100 + 1;
      in << k << ' ' << l << ' ' << r << endl;
    }
  }
  in.close();
}
bool de = false;
int main() {
  srand(time(0));
  // getts();freopen("1.txt","r",stdin);out.open("out.txt");de=true;
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    vl.clear();
    vr.clear();
    ans = 0;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l == r)
        ans += l;
      else if (l > r)
        vl.push_back({k, l, r});
      else
        vr.push_back({n - k, r, l});
    }
    for (int i = 0; i <= n; i++)
      s[0][i] = s[1][i] = 0;
    solve(vl, 0);
    solve(vr, 1);
    ll res = 0;
    for (int i = 0; i <= n; i++)
      res = max(res, s[0][i] + s[1][n - i]);
    if (!de)
      printf("%lld\\n", ans + res);
    if (de)
      out << ans + res << endl;
  }
}
/*
1
5
4 65 49
4 59 67
3 66 67
4 98 68
3 32 91
*/