Problem p02277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[100002], as[100002];
char c[100002], cs[100002], sc[250002], tc[250002];
int p(int u, int v) {
  int x = a[v], i = u - 1, j;
  for (j = u; j < v; j++) {
    if (a[j] <= x) {
      swap(a[j], a[++i]);
      swap(c[j], c[i]);
    }
  }
  swap(a[i + 1], a[v]);
  swap(c[i + 1], c[v]);
  return i + 1;
}
void q(int u, int v) {
  if (u < v) {
    int s = p(u, v);
    q(u, s - 1);
    q(s + 1, v);
  }
}
void me(int left, int d, int right) {
  int n1 = d - left, n2 = right - d, i, j, s[250002], t[250002];
  for (i = 0; i < n1; i++) {
    s[i] = as[left + i];
    sc[i] = cs[left + i];
  }
  for (i = 0; i < n2; i++) {
    t[i] = as[d + i];
    tc[i] = cs[d + i];
  }
  s[n1] = 2000000002;
  t[n2] = 2000000002;
  i = 0;
  j = 0;
  for (int k = left; k < right; k++) {
    if (s[i] <= t[j]) {
      as[k] = s[i];
      cs[k] = sc[i];
      i++;
    } else {
      as[k] = t[j];
      cs[k] = tc[j];
      j++;
    }
  }
}
void ms(int left, int right) {
  if (left + 1 < right) {
    int d = (left + right) / 2;
    ms(left, d);
    ms(d, right);
    me(left, d, right);
  }
}
int main() {
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> c[i] >> a[i];
    cs[i] = c[i];
    as[i] = a[i];
  }
  q(0, n - 1);
  ms(0, n);
  for (i = 0; i < n; i++) {
    if (cs[i] != c[i])
      break;
  }
  if (i != n)
    cout << "Not stable\\n";
  else
    cout << "Stable\\n";
  for (i = 0; i < n; i++)
    cout << c[i] << ' ' << a[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

struct card {
  int num;
  char str;
};
card number[100005];
card card1[100005];

void merge(int first, int mid, int last);
void quicksort(int first, int last) {
  int f = first;
  int l = last;
  card d = number[f];
  int x = number[f].num;
  if (f == l)
    return;
  while (f != l) {
    while (f < l && number[f].num <= x)
      f++;
    swap(number[f], d);
    while (f < l && number[l].num > x)
      l--;
    swap(number[l], d);
  }
  quicksort(first, l - 1);
  quicksort(l + 1, last);
}
void mergesort(int first, int last) {
  int mid = (last + first) / 2;
  if (last - first > 1) {
    mergesort(first, mid);
    mergesort(mid, last);
    merge(first, mid, last);
  }
}
void merge(int first, int mid, int last) {
  card num[100005];
  int f = first;
  int m = mid;
  int l = last;
  int i = 0;
  while (f < mid && m < l) {
    if (card1[f].num <= card1[l].num) {
      num[i] = card1[f];
      i++;
      f++;
    } else {
      num[i] = card1[m];
      m++;
      i++;
    }
  }
  while (f != mid) {
    num[i] = card1[f];
    f++;
    i++;
  }
  while (m != last) {
    num[i] = card1[m];
    m++;
    i++;
  }
  i = 0;
  for (int j = first; j < last; j++) {
    card1[j] = num[i];
    i++;
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  bool flag = true;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> number[i].str >> number[i].num;
  }
  for (int i = 0; i < n; i++) {
    card1[i] = number[i];
  }
  quicksort(0, n - 1);
  mergesort(0, n);
  for (int i = 0; i < n; i++) {
    if (number[i].str != card1[i].str) {
      flag = false;
      cout << "Not stable" << endl;
      break;
    }
  }
  if (flag)
    cout << "Stable";
  for (int i = 0; i < n; i++) {
    cout << number[i].str << " " << number[i].num << endl;
  }
  return 0;
}