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 = 2e5 + 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) {
  // cout<<"LE "<<l<<' '<<r<<' '<<t[k]<<endl;
  assert(t[k] >= 0);
  if (l != r) {
    t[k << 1] -= lz[k], t[k << 1 | 1] -= lz[k];
    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) {
    // cout<<"F: "<<l<<' '<<r<<endl;
    lz[k]++;
    t[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) {
  lz[k] = 0;
  t[k] = l;
  if (l == r)
    return;
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
  t[k] = l;
}
void solve(vector<node> v, int p) {
  build(1, n, 1);
  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 pos = query(1, n, 1);
    // cout<<pos<<' '<<t[1]<<endl;
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      if (v[id].k == 0)
        continue;
      if (v[id].k > pos) {
        // cout<<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 = 5;
    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;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5 + 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) {
  // cout<<"LE "<<l<<' '<<r<<' '<<t[k]<<endl;
  assert(t[k] >= 0);
  if (l != r) {
    t[k << 1] -= lz[k], t[k << 1 | 1] -= lz[k];
    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) {
    // cout<<"F: "<<l<<' '<<r<<endl;
    lz[k]++;
    t[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) {
  if (l == r) {
    t[k] = l;
    lz[k] = 0;
    return;
  }
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
  t[k] = l;
}
void solve(vector<node> v, int p) {
  build(1, n, 1);
  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 pos = query(1, n, 1);
    // cout<<pos<<' '<<t[1]<<endl;
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      if (v[id].k == 0)
        continue;
      if (v[id].k > pos) {
        // cout<<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 = 5;
    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;
  }
}