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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

typedef set<int> Group;

Group *make_group(int num) {
  Group *new_group = new Group();
  new_group->insert(num);
  return new_group;
}

Group *merge(Group *left, Group *right) {
  if (left->size() < right->size()) {
    swap(left, right);
  }
  for (Group::iterator it = right->begin(); it != right->end(); it++)
    left->insert(*it);

  return left;
}

int main() {

  int N;
  scanf("%d", &N);
  queue<Group *> Q;

  vector<int> V, WORK;

  int tmp;
  map<int, int> MAP;

  for (int i = 0; i < N; i++) {
    scanf("%d", &tmp);
    V.push_back(tmp);
    WORK.push_back(tmp);
  }

  sort(WORK.begin(), WORK.end());

  for (int i = 0; i < N; i++) {
    MAP[WORK[i]] = i + 1;
  }
  for (int i = 0; i < N; i++) {
    V[i] = MAP[V[i]];
  }

  for (int i = 0; i < N; i++) {
    Q.push(make_group(V[i]));
  }

  ll ans = 0;
  for (int num = 1; num <= N; num++) {
    Group *group = Q.front();
    Q.pop();

    while (true) {
      if (*(group->begin()) == num) {
        group->erase(group->begin());
        ans += group->size();
        if (group->size() > 0) {
          Q.push(group);
        }
        break;
      }
      Group *next_group = Q.front();
      Q.pop();
      group = merge(group, next_group);
    }
  }

  printf("%lld\\n", ans);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

typedef set<int> Group;

Group *make_group(int num) {
  Group *new_group = new Group();
  new_group->insert(num);
  return new_group;
}

Group *merge(Group *left, Group *right) {
  if (left->size() < right->size()) {
    swap(left, right);
  }
  for (Group::iterator it = right->begin(); it != right->end(); it++)
    left->insert(*it);

  return left;
}

int main() {

  int N;
  scanf("%d", &N);
  queue<Group *> Q;

  vector<int> V, WORK;

  int tmp;
  map<int, int> MAP;

  for (int i = 0; i < N; i++) {
    scanf("%d", &tmp);
    V.push_back(tmp);
    WORK.push_back(tmp);
  }

  sort(WORK.begin(), WORK.end());

  for (int i = 0; i < N; i++) {
    MAP[WORK[i]] = i + 1;
  }
  for (int i = 0; i < N; i++) {
    V[i] = MAP[V[i]];
  }

  for (int i = 0; i < N; i++) {
    Q.push(make_group(V[i]));
  }

  ll ans = 0;
  for (int num = 1; num <= N; num++) {
    Group *group = Q.front();
    Q.pop();

    while (true) {
      if (*(group->begin()) == num) {
        group->erase(group->begin());
        ans += group->size();
        if (group->size() > 0) {
          Q.push(group);
        }
        break;
      }
      Group *next_group = Q.front();
      Q.pop();
      merge(group, next_group);
    }
  }

  printf("%lld\\n", ans);

  return 0;
}