Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

// find_by_order gives kth-largest element
// order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
//
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define db4(x, y, z, w)                                                        \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int, int>
#define MOD (ll)(998244353)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
const int N = 15;

int rnk[N], parent[N], total[N];

int find(int x) {
  if (parent[x] < 0)
    return x;
  return parent[x] = find(parent[x]);
}

int merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y)
    return 0;
  if (rnk[y] > rnk[x])
    swap(x, y);
  parent[y] = x;
  rnk[x] = rnk[x] == rnk[y] ? rnk[x] + 1 : rnk[x];
  int sv = total[x];
  total[x] += total[y];
  return sv * total[y];
}

int32_t main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;

  rep(i, N) {
    parent[i] = -1;
    total[i] = 1;
    rnk[i] = 1;
  }

  vector<pii> bridge(m);
  rep(i, m) cin >> bridge[i].X >> bridge[i].Y;

  reverse(all(bridge));

  // int prev = 0;
  vector<int> store(m);
  rep(i, m) { store[i] = merge(bridge[i].X, bridge[i].Y); }

  reverse(all(store));
  cout << store[0] << '\n';
  repA(i, 1, m - 1) {
    store[i] += store[i - 1];
    cout << store[i] << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

// find_by_order gives kth-largest element
// order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
//
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define db4(x, y, z, w)                                                        \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int, int>
#define MOD (ll)(998244353)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
const int N = 100005;

int rnk[N], parent[N], total[N];

int find(int x) {
  if (parent[x] < 0)
    return x;
  return parent[x] = find(parent[x]);
}

int merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y)
    return 0;
  if (rnk[y] > rnk[x])
    swap(x, y);
  parent[y] = x;
  rnk[x] = rnk[x] == rnk[y] ? rnk[x] + 1 : rnk[x];
  int sv = total[x];
  total[x] += total[y];
  return sv * total[y];
}

int32_t main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;

  rep(i, N) {
    parent[i] = -1;
    total[i] = 1;
    rnk[i] = 1;
  }

  vector<pii> bridge(m);
  rep(i, m) cin >> bridge[i].X >> bridge[i].Y;

  reverse(all(bridge));

  // int prev = 0;
  vector<int> store(m);
  rep(i, m) { store[i] = merge(bridge[i].X, bridge[i].Y); }

  reverse(all(store));
  cout << store[0] << '\n';
  repA(i, 1, m - 1) {
    store[i] += store[i - 1];
    cout << store[i] << '\n';
  }
}