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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#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

int binarySearch(int key, int data[], int begin, int end) {

  int i = (begin + end + 1) / 2;

  if (data[i] == key)
    return i;
  else if (begin == end)
    return -1; // Not Found
  else if (key < data[i])
    return binarySearch(key, data, begin, i - 1);
  else if (data[i] < key)
    return binarySearch(key, data, i + 1, end);

  return -1;
}

int main(void) {
  int n, q;

  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];

  cin >> q;
  int T[n];
  for (int i = 0; i < q; i++)
    cin >> T[i];

  //    dout<<"S = ";
  //    for(int i=0; i<n; i++) dout<<S[i]<<" ";
  //    dout<<endl;
  //
  //    dout<<"T = ";
  //    for(int i=0; i<q; i++) dout<<T[i]<<" ";
  //    dout<<endl;

  // search T from S
  int ans = 0;
  for (int i = 0; i < q; i++) { // S[]????????????T[i]???key?????¢?´¢
    int key = T[i];
    int foundIndex = -1;
    //        int foundIndex = binarySearch(key, S, 0, n);
    //        if ( foundIndex != -1 ) ans++;

    int left = 0;
    int right = n;

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

      if (S[mid] == key) {
        foundIndex = mid;
        ans++;
        break;
      } else if (key < S[mid])
        right = mid;
      else if (S[mid] < key)
        left = mid + 1;
    }

    if (left == right)
      foundIndex = -1;

    // Test display
    //        dout << "i=" << i << " : key=" << key << " foundIndex=" <<
    //        foundIndex << " left=" <<left <<" right=" << right << " ans=" <<
    //        ans << endl;
  }

  // output
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
// #define TEST
// //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#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

int binarySearch(int key, int data[], int begin, int end) {

  int i = (begin + end + 1) / 2;

  if (data[i] == key)
    return i;
  else if (begin == end)
    return -1; // Not Found
  else if (key < data[i])
    return binarySearch(key, data, begin, i - 1);
  else if (data[i] < key)
    return binarySearch(key, data, i + 1, end);

  return -1;
}

int main(void) {
  int n, q;

  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];

  cin >> q;
  int T[n];
  for (int i = 0; i < q; i++)
    cin >> T[i];

  //    dout<<"S = ";
  //    for(int i=0; i<n; i++) dout<<S[i]<<" ";
  //    dout<<endl;
  //
  //    dout<<"T = ";
  //    for(int i=0; i<q; i++) dout<<T[i]<<" ";
  //    dout<<endl;

  // search T from S
  int ans = 0;
  for (int i = 0; i < q; i++) { // S[]????????????T[i]???key?????¢?´¢
    int key = T[i];
    int foundIndex = binarySearch(key, S, 0, n);
    if (foundIndex != -1)
      ans++;

    // Test display
    //        dout << "i=" << i << " : key=" << key << " foundIndex=" <<
    //        foundIndex << " ans=" << ans << endl;
  }

  // output
  cout << ans << endl;

  return 0;
}