Problem p02617 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, x, n) for (int i = x; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(X) (X).begin(), (X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) {
  fill((T *)a, (T *)(a + N), v);
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "{";
  rep(i, t.size()) { os << t[i] << ","; }
  os << "}";
  return os;
}
template <class T, size_t n>
ostream &operator<<(ostream &os, const array<T, n> &t) {
  os << "{";
  rep(i, n) { os << t[i] << ","; }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}

const ll INF = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  ll N, ans = 0;
  vector<pii> e;
  int add[200005] = {};

  cin >> N;
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    if (u > v)
      swap(u, v);
    e.emplace_back(u, v);
    add[v]++;
  }

  sort(all(e));
  reverse(all(e));

  ll sum = 0, cur = 0;
  rep(i, N) {
    cur += 1 - add[i];
    sum += cur;
  }
  ans += sum;
  rep(i, N) {
    sum -= (N - i);
    while (e.back().X == i) {
      sum += N - e.back().Y;
      e.pop_back();
    }
    ans += sum;
  }

  cout << ans << endl;

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, x, n) for (int i = x; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(X) (X).begin(), (X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) {
  fill((T *)a, (T *)(a + N), v);
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "{";
  rep(i, t.size()) { os << t[i] << ","; }
  os << "}";
  return os;
}
template <class T, size_t n>
ostream &operator<<(ostream &os, const array<T, n> &t) {
  os << "{";
  rep(i, n) { os << t[i] << ","; }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}

const ll INF = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  ll N, ans = 0;
  vector<pii> e;
  int add[200005] = {};

  cin >> N;
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    if (u > v)
      swap(u, v);
    e.emplace_back(u, v);
    add[v]++;
  }

  sort(all(e));
  reverse(all(e));

  ll sum = 0, cur = 0;
  rep(i, N) {
    cur += 1 - add[i];
    sum += cur;
  }
  ans += sum;
  rep(i, N) {
    sum -= (N - i);
    while (!e.empty() && e.back().X == i) {
      sum += N - e.back().Y;
      e.pop_back();
    }
    ans += sum;
  }

  cout << ans << endl;

  return 0;
}