Problem p03153 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define printvector(v, n)                                                      \
  for (int i = 1; i <= n; i++)                                                 \
    cout << v[i] << " ";                                                       \
  cout << ln;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \
  __builtin_popcount(a) // count set bits (for ints only diff for int64)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\n"
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define RFOR(i, b, a) for (int i = b; i >= a; i--)
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define setPresent(x, s) (s.find(x) != s.end())
#define sqr(x) x *x
#define siz(v) int(v.size())
const double pi = 3.14159265358979323846;
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef pair<char, int> pci;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
typedef map<ll, ll> m64;
#pragma GCC optimize("-O2")
template <class T>
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
const int LM = 1e5;
ll n, d, a[LM + 2], INF = 1e18;
p64 sg1[4 * LM + 5], sg2[4 * LM + 5];
int sz[LM + 2], p[LM + 2], siz = 1;
vector<pair<ll, p32>> edges;
vp64 v;
// in the segtree also store at what index the minimum
// occurs
int find(int x) {
  while (p[x] != x)
    x = p[x];
  return x;
}
void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (sz[a] < sz[b])
    swap(a, b);
  p[b] = a;
  sz[a] += sz[b];
}
void init(p64 sg[]) {
  while (siz < n)
    siz *= 2;
  FOR(i, 1, 2 * siz - 1) sg[i] = mp(INF, 1);
}
void update(int x, ll val, p64 sg[]) {
  sg[x + siz - 1] = mp(val, x);
  x += siz - 1;
  x /= 2;
  while (x != 0) {
    sg[x] = min(sg[2 * x], sg[2 * x + 1]);
    x /= 2;
  }
}
p64 query(int l, int r, p64 sg[]) {
  l += siz - 1;
  r += siz - 1;
  p64 ret = mp(INF, 1);
  while (r >= l) {
    if (l % 2 == 1) {
      ret = min(ret, sg[l++]);
    }
    if (r % 2 == 0) {
      ret = min(ret, sg[r--]);
    }
    l /= 2;
    r /= 2;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> d;
  init(sg1);
  init(sg2);
  FOR(i, 1, n) {
    cin >> a[i];
    v.pb(mp(a[i], i));
  }
  sort(v.begin(), v.end());
  for (auto u : v) {
    auto t1 = query(1, u.se - 1, sg1);
    auto t2 = query(u.se + 1, n, sg2);
    int i1 = t1.se, i2 = t2.se;
    ll w1 = u.fi + d * u.se + t1.fi;
    ll w2 = u.fi - d * u.se + t2.fi;
    edges.pb(mp(w1, mp(u.se, i1)));
    edges.pb(mp(w2, mp(u.se, i2)));
    update(u.se, u.fi - d * u.se, sg1);
    update(u.se, u.fi + d * u.se, sg2);
  }
  ll ans = 0;
  FOR(i, 1, n) p[i] = i;
  FOR(i, 1, n) sz[i] = 1;
  sort(edges.begin(), edges.end());
  for (auto u : edges) {
    if (find(u.se.fi) == find(u.se.se))
      continue;
    else {
      unite(u.se.fi, u.se.se);
      ans += u.fi;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define printvector(v, n)                                                      \
  for (int i = 1; i <= n; i++)                                                 \
    cout << v[i] << " ";                                                       \
  cout << ln;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \
  __builtin_popcount(a) // count set bits (for ints only diff for int64)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\n"
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define RFOR(i, b, a) for (int i = b; i >= a; i--)
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define setPresent(x, s) (s.find(x) != s.end())
#define sqr(x) x *x
#define siz(v) int(v.size())
const double pi = 3.14159265358979323846;
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef pair<char, int> pci;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
typedef map<ll, ll> m64;
#pragma GCC optimize("-O2")
template <class T>
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
const int LM = 2e5;
ll n, d, a[LM + 2], INF = 1e18;
p64 sg1[4 * LM + 5], sg2[4 * LM + 5];
int sz[LM + 2], p[LM + 2], siz = 1;
vector<pair<ll, p32>> edges;
vp64 v;
// in the segtree also store at what index the minimum
// occurs
int find(int x) {
  while (p[x] != x)
    x = p[x];
  return x;
}
void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (sz[a] < sz[b])
    swap(a, b);
  p[b] = a;
  sz[a] += sz[b];
}
void init(p64 sg[]) {
  while (siz < n)
    siz *= 2;
  FOR(i, 1, 2 * siz - 1) sg[i] = mp(INF, 1);
}
void update(int x, ll val, p64 sg[]) {
  sg[x + siz - 1] = mp(val, x);
  x += siz - 1;
  x /= 2;
  while (x != 0) {
    sg[x] = min(sg[2 * x], sg[2 * x + 1]);
    x /= 2;
  }
}
p64 query(int l, int r, p64 sg[]) {
  l += siz - 1;
  r += siz - 1;
  p64 ret = mp(INF, 1);
  while (r >= l) {
    if (l % 2 == 1) {
      ret = min(ret, sg[l++]);
    }
    if (r % 2 == 0) {
      ret = min(ret, sg[r--]);
    }
    l /= 2;
    r /= 2;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> d;
  init(sg1);
  init(sg2);
  FOR(i, 1, n) {
    cin >> a[i];
    v.pb(mp(a[i], i));
  }
  sort(v.begin(), v.end());
  for (auto u : v) {
    auto t1 = query(1, u.se - 1, sg1);
    auto t2 = query(u.se + 1, n, sg2);
    int i1 = t1.se, i2 = t2.se;
    ll w1 = u.fi + d * u.se + t1.fi;
    ll w2 = u.fi - d * u.se + t2.fi;
    edges.pb(mp(w1, mp(u.se, i1)));
    edges.pb(mp(w2, mp(u.se, i2)));
    update(u.se, u.fi - d * u.se, sg1);
    update(u.se, u.fi + d * u.se, sg2);
  }
  ll ans = 0;
  FOR(i, 1, n) p[i] = i;
  FOR(i, 1, n) sz[i] = 1;
  sort(edges.begin(), edges.end());
  for (auto u : edges) {
    if (find(u.se.fi) == find(u.se.se))
      continue;
    else {
      unite(u.se.fi, u.se.se);
      ans += u.fi;
    }
  }
  cout << ans << endl;
}