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

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

typedef long long ll;

struct Card {
  int num, id;
  string str;
};

ll partition(Card c[], ll p, ll r) {
  ll x = c[r].num;
  ll i = p - 1;
  for (ll j = p; j < r; j++) {
    if (c[j].num <= x) {
      i++;
      swap(c[i], c[j]);
    }
  }
  swap(c[i + 1], c[r]);
  return i + 1;
}

void qsort(Card c[], ll p, ll r) {
  if (p < r) {
    ll q = partition(c, p, r);
    qsort(c, p, q - 1);
    qsort(c, q + 1, r);
  }
}

// ll bs(ll num,const vector<pair<string,ll> > &v, ll n){
//     ll l=-1,r=n-1;
//     for(;r-l>1;){
//         ll mid=(l+r)/2;
//         if(v[mid].second < num){
//             l = mid;
//         }else{
//             r = mid;
//         }
//     }
//     return l+1;
// }

void check(Card c[], int n) {

  for (int i = 0; i < n - 1; i++) {
    if (c[i].num < c[i + 1].num)
      continue;
    if (c[i].id < c[i + 1].id)
      continue;
    cout << "Not stable\\n";
    return;
  }
  cout << "Stable\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  Card card[100000];
  for (ll i = 0; i < n; i++) {
    cin >> card[i].str >> card[i].num;
    card[i].id = i;
  }
  //    auto res=unique(tab.begin(),tab.end());
  //    tab.erase(res,tab.end());
  //    vector<pair<string, ll> > sub(card);
  qsort(card, 0, n - 1);

  check(card, n);
  for (int i = 0; i < n; i++) {
    cout << card[i].str << " " << card[i].num << endl;
  }
}


Error: 0

Fixed submission:

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

typedef long long ll;

int partition(vector<pair<string, int>> &v, int p, int r) {
  int x = v[r].second;
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (v[j].second <= x) {
      i++;
      swap(v[i], v[j]);
    }
  }
  swap(v[i + 1], v[r]);
  return i + 1;
}

void qsort(vector<pair<string, int>> &v, int p, int r) {
  if (p < r) {
    int q = partition(v, p, r);
    qsort(v, p, q - 1);
    qsort(v, q + 1, r);
  }
}

int bs(int num, const vector<pair<string, int>> &v, int n) {
  int l = -1, r = n - 1;
  for (; r - l > 1;) {
    int mid = (l + r) / 2;
    if (v[mid].second < num) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return l + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<string, int>> card(n);
  int tab[100001] = {};
  for (int i = 0; i < n; i++) {
    cin >> card[i].first >> card[i].second;
    tab[card[i].second - 1]++;
  }
  vector<pair<string, int>> sub(card);
  qsort(card, 0, n - 1);
  vector<pair<string, int>> c(card);
  bool flg = true;

  int nn = n;
  for (pair<string, int> p : sub) {
    int num = p.second;
    if (tab[num - 1] > 1) {
      int itr = bs(num, c, n);
      tab[num - 1]--;
      //            cout <<"debug c:"<< c[itr].first <<" " << c[itr].second <<
      //            endl; cout <<"debug sub:"<< p.first <<" "<< p.second <<
      //            endl;
      if (c[itr] != p) {
        flg = false;
      } else {
        c.erase(c.begin() + itr);
        n--;
      }
    }
    if (!flg)
      break;
  }

  if (flg)
    cout << "Stable\\n";
  else
    cout << "Not stable\\n";

  for (pair<string, int> p : card) {
    cout << p.first << " " << p.second << endl;
  }
}