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

#include <algorithm>
#include <iostream>
#include <limits>
#include <tuple>
#include <vector>
using namespace std;

template <typename Type> class StarrySkyTree {
public:
  unsigned size_;
  std::vector<Type> dat1, dat2, dat;
  inline void update_(unsigned a, unsigned b, Type x, unsigned k, unsigned l,
                      unsigned r) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      dat2[k] += x;
      return;
    }
    update_(a, b, x, (k << 1), l, (l + r) >> 1);
    update_(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
    dat1[k] = std::max(dat1[k << 1] + dat2[k << 1],
                       dat1[(k << 1) + 1] + dat2[(k << 1) + 1]);
  }
  inline Type query_(unsigned a, unsigned b, unsigned k, unsigned l,
                     unsigned r) {
    if (r <= a || b <= l)
      return std::numeric_limits<Type>::min();
    if (a <= l && r <= b)
      return dat1[k] + dat2[k];
    Type lc = query_(a, b, (k << 1), l, (l + r) >> 1);
    Type rc = query_(a, b, (k << 1) + 1, (l + r) >> 1, r);
    return std::max(lc, rc) + dat2[k];
  }
  // StarrySkyTree() : size_(0), dat(std::vector<Type>()) {};
  StarrySkyTree(int size__) {
    for (size_ = 1; size_ < size__;)
      size_ <<= 1;
    dat1.resize(size_ << 1, 0);
    dat2.resize(size_ << 1, 0);
    dat.resize(size_ << 1, 0);
  }
  template <class T> StarrySkyTree(T begin_, T end_) {
    unsigned n = (end_ - begin_);
    for (size_ = 1; size_ < n; size_ <<= 1)
      ;
    dat.resize(size_ << 1, std::numeric_limits<Type>::min());
    for (unsigned i = 0; i < n; i++)
      dat[i + size_] = *(begin_ + i);
    for (unsigned i = size_ - 1; i > 0; i--)
      dat[i] = std::max(dat[i << 1], dat[(i << 1) + 1]);
  }
  inline unsigned size() { return size_; }
  inline void add(unsigned i, Type x) { update_(i, i + 1, x, 1, 0, size_); }
  inline void add(unsigned l, unsigned r, Type x) {
    update_(l, r, x, 1, 0, size_);
  }
  inline Type query(unsigned l, unsigned r) {
    return query_(l, r, 1, 0, size_);
  }
};

long long bit[100009];
void add(int pos, int x) {
  pos++;
  while (pos <= 100008) {
    bit[pos] += x;
    pos += (pos & -pos);
  }
}
long long sum(int pos) {
  long long s = 0;
  pos++;
  while (pos >= 1) {
    s += bit[pos];
    pos -= (pos & -pos);
  }
  return s;
}

long long n, a[100009], b[100009], px[100009], py[100009], qx[100009],
    qy[100009], cnt1, cnt2;
bool used[100009];
vector<pair<int, int>> Z;
vector<tuple<int, int, int, int>> D;

void init() {
  n = 0;
  for (int i = 0; i < 100009; i++) {
    a[i] = 0;
    px[i] = 0;
    py[i] = 0;
    qx[i] = 0;
    qy[i] = 0;
    used[i] = false;
    bit[i] = 0;
  }
  cnt1 = 0;
  cnt2 = 0;
  Z.clear();
  D.clear();
}
void random_generate() {
  n = 3;
  for (int i = 1; i <= n; i++) {
    a[i] = rand() % 5 + 1;
    b[i] = a[i];
  }
}
long long solve_Jury() {
  int res = 100000000;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      swap(a[i], a[j]);
      int ct = 0;
      for (int k = 1; k <= n; k++) {
        for (int l = k + 1; l <= n; l++) {
          if (a[k] > a[l])
            ct++;
        }
      }
      res = min(res, ct);
      swap(a[i], a[j]);
    }
  }
  return res;
}
long long solve_Part() {
  for (int i = 1; i <= n; i++) {
    Z.push_back(make_pair(a[i], 0));
  }
  sort(Z.begin(), Z.end());
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(Z.begin(), Z.end(), make_pair((int)a[i], 0)) - Z.begin();
  }
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    cnt += (sum(100007) - sum(a[i]));
    add(a[i], 1);
  }
  if (cnt == 0) {
    for (int i = 1; i <= n - 1; i++) {
      if (a[i] == a[i + 1])
        return 0;
    }
    return 1;
  }
  int maxn = -1;
  for (int i = 1; i <= n; i++) {
    if (maxn < a[i]) {
      maxn = a[i];
      px[cnt1] = i;
      py[cnt1] = a[i];
      cnt1++;
      used[i] = true;
    }
  }
  maxn = 1000000;
  for (int i = n; i >= 1; i--) {
    if (maxn > a[i]) {
      maxn = a[i];
      qx[cnt2] = i;
      qy[cnt2] = a[i];
      cnt2++;
      used[i] = true;
    }
  }
  reverse(qx, qx + cnt2);
  reverse(qy, qy + cnt2);
  for (int i = 1; i <= n; i++) {
    if (used[i] == true)
      continue;
    int pos1 = lower_bound(py, py + cnt1, a[i]) - py;
    int pos5 = lower_bound(py, py + cnt1, a[i] + 1) - py;
    int pos2 = upper_bound(px, px + cnt1, i) - px;
    pos2--;
    int pos3 = lower_bound(qx, qx + cnt2, i) - qx;
    int pos4 = upper_bound(qy, qy + cnt2, a[i]) - qy;
    pos4--;
    int pos6 = lower_bound(qy, qy + cnt2, a[i]) - qy;
    pos6--;
    D.push_back(make_tuple(pos1, -1, pos3, pos6));
    D.push_back(make_tuple(pos5 - 1, 1, pos3, pos6));
    D.push_back(make_tuple(pos5, -2, pos3, pos6));
    D.push_back(make_tuple(pos2, 2, pos3, pos6));
    D.push_back(make_tuple(pos5, -1, pos6 + 1, pos4));
    D.push_back(make_tuple(pos2, 1, pos6 + 1, pos4));
  }
  sort(D.begin(), D.end());
  StarrySkyTree<long long> A(n + 2);
  long long ret = 0;
  for (int i = 0; i < D.size(); i++) {
    A.add(get<2>(D[i]), get<3>(D[i]) + 1, -1 * get<1>(D[i]));
    ret = max(ret, A.query(0, n + 1));
    // for (int j = 0; j <= n; j++) { cout << A.query(j, j + 1) << " "; }cout <<
    // endl;
  }
  return cnt - ret - 1LL;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  // long long J1 = solve_Jury();
  long long J2 = solve_Part();
  /*int wacase = 0;
  for (int i = 1; i <= 1000; i++) {
          init();
          random_generate();
          long long J1 = solve_Jury();
          long long J2 = solve_Part();
          if (J1 != J2) {
                  cout << "Case " << i << " [WA " << wacase + 1 << "] : Jury = "
  << J1 << " Part = " << J2 << endl; cout << n << endl; for (int i = 1; i <= n;
  i++) { if (i >= 2)cout << " "; cout << b[i]; } cout << endl << endl; wacase++;
          }
  }*/
  cout << J2 << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

#define MAX_N 5100
int x[MAX_N], y[MAX_N], pos1[MAX_N][MAX_N], pos2[MAX_N][MAX_N];
long long n, maxn, bubble;
bool ok = true;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    y[i] = x[i];
  }
  sort(y, y + n);
  for (int i = 0; i < n; i++) {
    if (x[i] != y[i]) {
      ok = false;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (x[i] > x[j]) {
        pos1[i][j] = pos1[i][j - 1] + 1;
      }
      if (x[i] < x[j]) {
        pos1[i][j] = pos1[i][j - 1] - 1;
      }
    }
    for (int j = i - 1; j >= 0; j--) {
      if (x[i] < x[j]) {
        pos2[i][j] = pos2[i][j + 1] + 1;
      }
      if (x[i] > x[j]) {
        pos2[i][j] = pos2[i][j + 1] - 1;
      }
    }
  }
  maxn = -9999999999LL;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (x[i] > x[j]) {
        long long X1 = pos1[i][j - 1];
        long long X2 = pos2[j][i + 1];
        maxn = max(maxn, X1 + X2 + 1);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (x[i] > x[j]) {
        bubble++;
      }
    }
  }
  if (ok == true) {
    bubble = 1;
    maxn = 0;
  }
  cout << bubble - maxn << endl;
  return 0;
}