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

#include <iostream>
using namespace std;

long long cnt = 0; // for counting num of invensions

void merge(int *a, int left, int mid, int right) {

  int nL = mid - left;
  int nR = right - mid;

  int L[nL], R[nR];

  for (int i = 0; i < nL; i++) {
    L[i] = a[left + i];
  }
  for (int i = 0; i < nR; i++) {
    R[i] = a[mid + i];
  }

  int indexL = 0, indexR = 0;
  int k = left;
  while (k < right) {

    if (indexL >= nL) { // in case that L[] has no element
      a[k] = R[indexR]; // this is not inversion
      indexR++;
    } else if (indexR >= nR) { // in case that R[] has no element
      a[k] = L[indexL];        // this is not inversion
      indexL++;
    } else if (L[indexL] <= R[indexR]) {
      a[k] = L[indexL]; // this is not inversion
      indexL++;
    } else if (L[indexL] > R[indexR]) { // actually, this condition is not
                                        // needed
      a[k] = R[indexR]; // this is inversion
      indexR++;
      cnt += nL - indexL; // num of inversion = num of elements remaining in L[]
    }

    k++;
  }

  //    //display info. about this step
  //    cout << "-----\\n";
  //    cout << "merge(" << left << ", " << mid << ", " << right <<")" << endl;
  //
  //    cout << "L" << " = "; for(int i=0; i<nL; i++) {cout << L[i] << " ";}
  //    cout << endl; cout << "R" << " = "; for(int i=0; i<nR; i++) {cout <<
  //    R[i] << " ";} cout << endl;
  //
  //    cout << "cnt = " << cnt << endl;

  return;
}

void mergeSort(int *a, int left, int right) {

  if (left + 1 < right) {
    int mid = (left + right) / 2;

    mergeSort(a, left, mid);
    mergeSort(a, mid, right);

    merge(a, left, mid, right);
  }

  return;
}

int main(void) {
  int N;
  cin >> N;

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

  //    //display a[] before sorting
  //    cout << "a[] = ";
  //    for(int i=0; i<N; i++) {
  //        cout << a[i] << " ";
  //    }
  //    cout << endl;

  // merge sort a[]
  mergeSort(a, 0, N);

  //    //display a[] after sorting
  //    cout << "a[] = ";
  //    for(int i=0; i<N; i++) {
  //        cout << a[i] << " ";
  //    }
  //    cout << endl;

  //    cout << "Num of inversions = ";
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> // ifstream, ofstream

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUT2TEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUT2TEXTFILE
// //*******************************************************************************************************************************************
#ifdef OUTPUT2TEXTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
// TEST??????????????????????????¨???outputfile??????????????????????????????
#define OUTPUTFILENAME "output.txt"
ofstream outputfile(OUTPUTFILENAME);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << (A) << " "
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

const int INF = 2147483647;

int main(void) {

  int N = 2;
  cin >> N;

  int a[N];
  rep(i, 0, N) { cin >> a[i]; }

  // binary search????????£???insertion sort
  int cnt = 0;
  rep(i, 1, N) {
    int key = a[i];
    int left = 0;
    int right = i;
    int mid;

    // key????????\\???????????????????????????????????°???????????????????????¢?´¢??§??¢???
    while (left < right) {
      mid = (left + right) / 2;

      if (key < a[mid]) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }

    // key????????\\????????????????????????????´????1???????????????????????????
    for (int j = i - 1; j >= left; j--) {
      a[j + 1] = a[j];
      cnt++;
    }

    a[left] = key;
    //        disp(i);
    //            dispAll(a,N);
  }

  //    dispAll(a,N);
  cout << cnt << endl;

#ifdef OUTPUT2TEXTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}