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

#include <bits/stdc++.h>
using namespace std;

#define removeDuplicate(a)                                                     \\
  sort(a.begin(), a.end());                                                    \\
  a.resize(distance(a.begin(), unique(a.begin(), a.end())));

const int N = 6e5 + 10;

int n;
int a[N];
set<int> *q[N];

void merge(set<int> *&a, set<int> *&b) {
  if (a->size() > b->size()) {
    for (auto value : *b) {
      a->insert(value);
    }
    b->clear();
  } else {
    for (auto value : *a) {
      b->insert(value);
    }
    a->clear();
    a = b;
  }
}

void zip() {
  vector<int> rar;
  for (int i = 1; i <= n; i++) {
    rar.push_back(a[i]);
  }
  removeDuplicate(rar);
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(rar.begin(), rar.end(), a[i]) - rar.begin() + 1;
  }
}

int main() {
#define file "9"
  // freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);

  cin >> n;
  for (int i = 1; i <= 3 * n; i++) {
    q[i] = new set<int>;
  }
  for (int i = 1; i <= n; i++) {
    int value;
    cin >> value;
    a[i] = value;
  }
  zip();
  for (int i = 1; i <= n; i++) {
    q[i]->insert(a[i]);
  }
  long long ret = 0;
  int front = 1, back = n;
  for (int value = 1; value <= n; value++) {
    for (int i = front; i <= back; i++) {
      merge(q[back + 1], q[i]);
      front++;
      if (q[back + 1]->find(value) != q[back + 1]->end()) {
        break;
      }
    }
    back++;
    q[back]->erase(value);
    if (q[back]->empty()) {
      back--;
      continue;
    }
    ret += ((int)(q[back]->size()));
  }
  cout << ret << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define removeDuplicate(a)                                                     \\
  sort(a.begin(), a.end());                                                    \\
  a.resize(distance(a.begin(), unique(a.begin(), a.end())));

const int N = 2e5 + 10;

int n;
int a[N];
set<int> *q[N];

void merge(set<int> *&a, set<int> *&b) {
  if (a->size() > b->size()) {
    for (auto value : *b) {
      a->insert(value);
    }
    b->clear();
  } else {
    for (auto value : *a) {
      b->insert(value);
    }
    a->clear();
    a = b;
  }
}

void zip() {
  vector<int> rar;
  for (int i = 1; i <= n; i++) {
    rar.push_back(a[i]);
  }
  removeDuplicate(rar);
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(rar.begin(), rar.end(), a[i]) - rar.begin() + 1;
  }
}

int main() {
#define file "9"
  // freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);

  cin >> n;
  for (int i = 1; i <= 3 * n; i++) {
    q[i] = new set<int>;
  }
  for (int i = 1; i <= n; i++) {
    int value;
    cin >> value;
    a[i] = value;
  }
  zip();
  for (int i = 1; i <= n; i++) {
    q[i]->insert(a[i]);
  }
  long long ret = 0;
  int front = 1, back = n;
  for (int value = 1; value <= n; value++) {
    for (int i = front; i <= back; i++) {
      merge(q[back + 1], q[i]);
      front++;
      if (q[back + 1]->find(value) != q[back + 1]->end()) {
        break;
      }
    }
    back++;
    q[back]->erase(value);
    if (q[back]->empty()) {
      back--;
      continue;
    }
    ret += ((int)(q[back]->size()));
  }
  cout << ret << endl;
  return 0;
}