Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int lg, N, M, X, Y, Z, realVal[100009], mp1[100009], mp2[100009],
    cnt[2][200009];
long long SA, aib[2][200009];
pair<int, int> P[100009];
vector<pair<int, int>> nrm;

void Normalize() {
  for (int i = 1; i <= N; i++)
    nrm.push_back({P[i].second - P[i].first, i}),
        nrm.push_back({P[i].second, -i});
  sort(nrm.begin(), nrm.end()), reverse(nrm.begin(), nrm.end()), M = nrm.size();
  for (int i = 0; i < nrm.size(); i++) {
    if (nrm[i].second > 0)
      mp1[nrm[i].second] = i + 1;
    else
      mp2[-nrm[i].second] = i + 1;
    realVal[i + 1] = nrm[i].first;
  }
}

void U(int lin, int pos, int sg) {
  int val = realVal[pos] * sg;
  while (pos <= M)
    cnt[lin][pos] += sg, aib[lin][pos] += val, pos += pos - (pos & (pos - 1));
}

long long sumQ(int lin, int atMost) {
  int pos = 0;
  long long ans = 0;
  for (int i = lg; i >= 0; i--)
    if (pos + (1 << i) <= M && cnt[lin][pos + (1 << i)] <= atMost)
      pos += 1 << i, atMost -= cnt[lin][pos], ans += aib[lin][pos];
  return ans;
}

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d %d %d", &X, &Y, &Z), N = X + Y + Z;
  for (int i = 1; i <= N; i++) {
    int ai, bi, ci;
    scanf("%d %d %d", &ai, &bi, &ci), SA += ai;
    bi -= ai, ci -= ai;
    P[i] = {bi, ci};
  }
  sort(P + 1, P + N + 1), reverse(P + 1, P + N + 1);
  Normalize();
  while ((1 << (lg + 1)) <= M)
    lg++;
  long long SB = 0, best = 0;
  for (int i = 1; i <= N; i++)
    U(1, mp2[i], +1);
  /*printf ("%lld + \n", SA);
  for (int i=1; i<=N; i++)
      printf ("(%d, %d)\n", P[i].first, P[i].second);*/
  for (int i = 1; i <= Y + Z; i++) {
    SB += P[i].first, U(0, mp1[i], +1);
    U(1, mp2[i], -1);
    if (i >= Y) {
      long long curr = SA + SB + sumQ(0, i - Y) + sumQ(1, Z - (i - Y));
      if (curr > best)
        best = curr; //, printf ("after %d -> %lld\n", i, best);
    }
  }
  printf("%lld\n", best);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int lg, N, M, X, Y, Z, realVal[200009], mp1[100009], mp2[100009],
    cnt[2][200009];
long long SA, aib[2][200009];
pair<int, int> P[100009];
vector<pair<int, int>> nrm;

void Normalize() {
  for (int i = 1; i <= N; i++)
    nrm.push_back({P[i].second - P[i].first, i}),
        nrm.push_back({P[i].second, -i});
  sort(nrm.begin(), nrm.end()), reverse(nrm.begin(), nrm.end()), M = nrm.size();
  for (int i = 0; i < nrm.size(); i++) {
    if (nrm[i].second > 0)
      mp1[nrm[i].second] = i + 1;
    else
      mp2[-nrm[i].second] = i + 1;
    realVal[i + 1] = nrm[i].first;
  }
}

void U(int lin, int pos, int sg) {
  int val = realVal[pos] * sg;
  while (pos <= M)
    cnt[lin][pos] += sg, aib[lin][pos] += val, pos += pos - (pos & (pos - 1));
}

long long sumQ(int lin, int atMost) {
  int pos = 0;
  long long ans = 0;
  for (int i = lg; i >= 0; i--)
    if (pos + (1 << i) <= M && cnt[lin][pos + (1 << i)] <= atMost)
      pos += 1 << i, atMost -= cnt[lin][pos], ans += aib[lin][pos];
  return ans;
}

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d %d %d", &X, &Y, &Z), N = X + Y + Z;
  for (int i = 1; i <= N; i++) {
    int ai, bi, ci;
    scanf("%d %d %d", &ai, &bi, &ci), SA += ai;
    bi -= ai, ci -= ai;
    P[i] = {bi, ci};
  }
  sort(P + 1, P + N + 1), reverse(P + 1, P + N + 1);
  Normalize();
  while ((1 << (lg + 1)) <= M)
    lg++;
  long long SB = 0, best = 0;
  for (int i = 1; i <= N; i++)
    U(1, mp2[i], +1);
  /*printf ("%lld + \n", SA);
  for (int i=1; i<=N; i++)
      printf ("(%d, %d)\n", P[i].first, P[i].second);*/
  for (int i = 1; i <= Y + Z; i++) {
    SB += P[i].first, U(0, mp1[i], +1);
    U(1, mp2[i], -1);
    if (i >= Y) {
      long long curr = SA + SB + sumQ(0, i - Y) + sumQ(1, Z - (i - Y));
      if (curr > best)
        best = curr; //, printf ("after %d -> %lld\n", i, best);
    }
  }
  printf("%lld\n", best);
  return 0;
}