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

#include <cstdio>
#include <iostream>
#include <limits.h>

using namespace std;

#define SENTINEL INT_MAX

long long int cnt = 0;
int s[200000];

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;

  int *L = new int[n1 + 1];
  int *R = new int[n2 + 1];

  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }

  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }

  L[n1] = R[n2] = SENTINEL;

  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      cnt += n1 - i;
      j++;
    }
  }

  delete[] R;
  delete[] L;
}

void mergeSort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (right + left) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}

int main(void) {

  int n;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

  /*/???????????°??¨
  for (int i = 1; i <= 10000; i++) {
          cnt = 0;
          for (int j = 0; j < 10000; j++) {
                  s[j] = 0;
          }
          for (int j = 0; j < i; j++) {
                  s[j] = i - j;
          }
          mergeSort(s,0,i);
          if (cnt == (1 + i) / 2.0 * i) {
                  cout << "i = " << i <<"OK" << endl;
          }
          else {
                  cout << "i = " << i << "OUT" << endl;
          }

  }
  */

  mergeSort(s, 0, n);

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int A[200000];
int cnt = 0;

template <typename T> int partition(T A[], int const p, int const r) {
  T x = A[r];
  int i = p - 1;
  int cntKeep = 0;

  /*/???????????°??¨
  printf("----------------------------\\n");
  for (int i = p; i <= r; i++) {
          printf("%d ", A[i]);
  }
  putchar('\\n');
  //--*/

  for (int j = p; j < r; j++) {

    if (A[j] <= x) {
      i++;
      if (A[i] > x) {
        cnt += cntKeep;
      }

      /*??¬??\\?????\\?????????
      T tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
      */

      //?¨????????¢??????????????????????????????????????§????
      T tmp = A[j];
      for (int k = j - 1; k >= i; k--) {
        A[k + 1] = A[k];
      }
      A[i] = tmp;
      //---

    } else {
      cntKeep++;
    }

    /*/???????????°??¨
    for (int i = p; i <= r; i++) {
            printf("%d ", A[i]);
    }
    putchar('\\n');
    //--*/
  }

  // cnt += (i - p + 2)*(r - i-1);

  if (A[i + 1] != A[r]) {
    cnt += cntKeep;
    //?¨????????¢??????????????????????????????????????§????
    T tmp = A[r];
    for (int j = r - 1; j > i; j--) {
      A[j + 1] = A[j];
    }
    A[i + 1] = tmp;
    //---
  }

  /*??¬??\\?????\\???????????????
          T tmp = A[i + 1];
          A[i + 1] = A[r];
          A[r] = tmp;
  */

  /*/???????????°??¨
  printf("cntKeep = %d\\n", cntKeep);
  printf("cnt = %d\\n", cnt);

  for (int i = p; i <= r; i++) {
          printf("%d ", A[i]);
  }
  putchar('\\n');
  //--*/

  return i + 1;
}

template <typename T> void quickSort(T A[], int const p, int const r) {
  if (p < r) {
    int q = partition(A, p, r);
    quickSort(A, p, q - 1);
    quickSort(A, q + 1, r);
  }
}

int main(void) {

  int n;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  quickSort(A, 0, n - 1);

  cout << cnt << endl;

  return 0;
}