Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

const ll MN = 10;
vi f(MN, 0);

struct UnionFind {
  // par[i] parent. if i == par[i] i is root
  vector<ll> par;
  // sizes[i] sizeof tree i if i is root
  vector<ll> sizes;

  UnionFind(ll n) : par(n), sizes(n, 1) {
    // init: all node is root i
    rep(i, n) par[i] = i;
  }

  // find root of x
  ll find(ll x) {
    if (x == par[x])
      return x;
    return par[x] = find(par[x]); // recursive, editing root
  }

  // merge x, y
  void unite(ll x, ll y) {
    // root
    x = find(x);
    y = find(y);

    // do not merge if same tree
    if (x == y)
      return;

    // force size x > y
    if (sizes[x] < sizes[y])
      swap(x, y);

    // unite: x is parent of y
    par[y] = x;
    sizes[x] += sizes[y];
  }

  // x, y same root
  bool same(ll x, ll y) { return find(x) == find(y); }

  // size of tree containing x
  ll size(ll x) { return sizes[find(x)]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n;
  cin >> n;
  vll a(n);
  vll b(n + 1);
  rep(i, n) {
    cin >> a[i];
    b[a[i]] = i + 1;
  }
  UnionFind uf(n + 1);
  vll cnt(n + 1, 0);
  for (int i = n; i >= 1; i--) {
    ll j = b[i];
    f[j] = 1;
    if (i < n) {
      cnt[i] = cnt[i + 1];
    }
    if (f[j + 1]) {
      cnt[i] -= uf.size(j + 1) * (uf.size(j + 1) + 1) / 2;
      uf.unite(j, j + 1);
    }
    if (f[j - 1]) {
      cnt[i] -= uf.size(j - 1) * (uf.size(j - 1) + 1) / 2;
      uf.unite(j - 1, j);
    }
    cnt[i] += uf.size(j) * (uf.size(j) + 1) / 2;
  }
  dump(cnt);
  repr(i, 1, n) { cnt[i] -= cnt[i + 1]; }
  dump(cnt);
  ll ans = 0;
  repr(i, 1, n + 1) { ans += cnt[i] * i; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    y >>= 1;
  }
  return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r *= x;
    x *= x;
    r %= m;
    x %= m;
    y >>= 1;
  }
  return r;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

const ll MN = 200010;
vi f(MN, 0);

struct UnionFind {
  // par[i] parent. if i == par[i] i is root
  vector<ll> par;
  // sizes[i] sizeof tree i if i is root
  vector<ll> sizes;

  UnionFind(ll n) : par(n), sizes(n, 1) {
    // init: all node is root i
    rep(i, n) par[i] = i;
  }

  // find root of x
  ll find(ll x) {
    if (x == par[x])
      return x;
    return par[x] = find(par[x]); // recursive, editing root
  }

  // merge x, y
  void unite(ll x, ll y) {
    // root
    x = find(x);
    y = find(y);

    // do not merge if same tree
    if (x == y)
      return;

    // force size x > y
    if (sizes[x] < sizes[y])
      swap(x, y);

    // unite: x is parent of y
    par[y] = x;
    sizes[x] += sizes[y];
  }

  // x, y same root
  bool same(ll x, ll y) { return find(x) == find(y); }

  // size of tree containing x
  ll size(ll x) { return sizes[find(x)]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n;
  cin >> n;
  vll a(n);
  vll b(n + 1);
  rep(i, n) {
    cin >> a[i];
    b[a[i]] = i + 1;
  }
  UnionFind uf(n + 1);
  vll cnt(n + 1, 0);
  for (int i = n; i >= 1; i--) {
    ll j = b[i];
    f[j] = 1;
    if (i < n) {
      cnt[i] = cnt[i + 1];
    }
    if (f[j + 1]) {
      cnt[i] -= uf.size(j + 1) * (uf.size(j + 1) + 1) / 2;
      uf.unite(j, j + 1);
    }
    if (f[j - 1]) {
      cnt[i] -= uf.size(j - 1) * (uf.size(j - 1) + 1) / 2;
      uf.unite(j - 1, j);
    }
    cnt[i] += uf.size(j) * (uf.size(j) + 1) / 2;
  }
  dump(cnt);
  repr(i, 1, n) { cnt[i] -= cnt[i + 1]; }
  dump(cnt);
  ll ans = 0;
  repr(i, 1, n + 1) { ans += cnt[i] * i; }
  cout << ans << endl;
  return 0;
}