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

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
const int MAXN = 200005;
const int MAXT = 530000;

int n, a[MAXN];
vector<int> v;

struct seg {
  pi tree[MAXT];
  int lazy[MAXT];
  void init(int s, int e, int p, vector<int> &v) {
    if (s == e) {
      tree[p] = pi(v[s], s);
      return;
    }
    int m = (s + e) / 2;
    init(s, m, 2 * p, v);
    init(m + 1, e, 2 * p + 1, v);
    tree[p] = min(tree[2 * p], tree[2 * p + 1]);
  }
  void lazydown(int p) {
    tree[2 * p].first += lazy[p];
    tree[2 * p + 1].first += lazy[p];
    lazy[2 * p] += lazy[p];
    lazy[2 * p + 1] += lazy[p];
    lazy[p] = 0;
  }
  void add(int s, int e, int ps, int pe, int p, int v) {
    if (e < ps || pe < s)
      return;
    if (s <= ps && pe <= e) {
      tree[p].first += v;
      lazy[p] += v;
      return;
    }
    int pm = (ps + pe) / 2;
    lazydown(p);
    add(s, e, ps, pm, 2 * p, v);
    add(s, e, pm + 1, pe, 2 * p + 1, v);
    tree[p] = min(tree[2 * p], tree[2 * p + 1]);
  }
  pi query(int s, int e, int ps, int pe, int p) {
    if (e < ps || pe < s)
      return pi(2e9, -1);
    if (s <= ps && pe <= e)
      return tree[p];
    int pm = (ps + pe) / 2;
    lazydown(p);
    return min(query(s, e, ps, pm, 2 * p), query(s, e, pm + 1, pe, 2 * p + 1));
  }
} seg;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    v.push_back(a[i]);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  seg.init(0, v.size() - 1, 1, v);
  lint ans = 0;
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    auto ret = seg.query(a[i], v.size() - 1, 0, v.size() - 1, 1);
    ans += ret.first - v[a[i]];
    seg.add(0, ret.second - 1, 0, v.size() - 1, 1, 1);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<lint, lint> pi;
const int MAXN = 2005, N = MAXN;
const int mod = 1e9 + 7, inv2 = (mod + 1) / 2;
int dp[MAXN][3][MAXN], odp[3][MAXN], sz[MAXN];
vector<int> adj[N];
void sadd(int &x, int y) {
  x += y;
  while (x >= mod)
    x -= mod;
}
void dfs(int v, int p = -1) {
  dp[v][0][1] = 1;
  sz[v] = 1;
  for (int u : adj[v]) {
    if (u != p) {
      dfs(u, v);
      memcpy(odp, dp[v], sizeof dp[v]);
      memset(dp[v], 0, sizeof dp[v]);
      for (int dv = 0; dv <= 2; ++dv) {
        for (int du = 0; du <= 2; ++du) {
          for (int use = 0; use <= 1; ++use) {
            if (use == 1 && (du > 1 || dv > 1))
              continue;
            for (int cv = 1; cv <= sz[v]; ++cv)
              for (int cu = 1; cu <= sz[u]; ++cu) {
                int ways = (use == 1 ? mod - 1 : 1);
                if (use) {
                  if (du == 0 && dv == 0)
                    ways = (long long)ways * 2 % mod;
                  if (du == 1 && dv == 1)
                    ways = (long long)ways * inv2 % mod;
                }
                sadd(dp[v][dv + use][cv + cu - use],
                     odp[dv][cv] * (long long)dp[u][du][cu] % mod * ways % mod);
              }
          }
        }
      }
      sz[v] += sz[u];
    }
  }
}
int f[N], a[N];
int main() {
  int n;
  long long res = 0;
  cin >> n;
  for (int j = 0; j < n; ++j)
    cin >> a[j];
  for (int j = 0; j < n; ++j) {
    int best = 2e9;
    for (int i = j; i >= 0; --i) {
      if (max(i == 0 ? 0 : a[i - 1] - a[j], 0) + j - i < best) {
        best = max(i == 0 ? 0 : a[i - 1] - a[j], 0) + j - i;
      }
    }
    for (int i = j; i >= 0; --i) {
      if (max(i == 0 ? 0 : a[i - 1] - a[j], 0) + j - i == best) {
        for (int k = j; k > i; --k) {
          swap(a[k], a[k - 1]);
        }
        a[i] = max(a[i], (i == 0 ? 0 : a[i - 1]));
      }
    }
    res += best;
  }
  cout << res << endl;
}