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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

class cBinarySearch {
  int mn, mq;
  string mS, mT;
  int ans;
  vector<int> vs, vt;

  void decode();

public:
  cBinarySearch(const int &n, const int &q, const string &s, const string &t) {
    mn = n;
    mq = q;
    mS = s;
    mT = t;
    ans = 0;

    decode();
  }
  void search();
  void output();
};

void cBinarySearch::search() {
  int l, r, m;
  int count = 0;

  for (int i = 0; i < vt.size(); ++i) {
    l = 0;
    r = vs.size();
    m = (l + r) / 2;
    while (l < r) {
      m = (l + r) / 2;
      if (vt[i] == vs[m]) {
        ++count;
        break;
      } else if (vt[i] < vs[m]) {
        r = m;
      } else
        l = m + 1;
    }
  }
  ans = count;
}

void cBinarySearch::output() { cout << ans << endl; }

void cBinarySearch::decode() {
  istringstream iss;
  iss.str(mS);
  int ts;
  for (int i = 0; i < mn; ++i) {
    iss >> ts;
    vs.push_back(ts);
  }
  iss.str(mT);
  iss.seekg(0, iss.beg);
  for (int i = 0; i < mq; ++i) {
    iss >> ts;
    vt.push_back(ts);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  string s, t;
  cin >> n;
  cin.ignore();
  getline(cin, s);
  cin >> q;
  cin.ignore();
  getline(cin, t);

  cBinarySearch cbs(n, q, s, t);
  cbs.search();
  cbs.output();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

class cBinarySearch {
  int mn, mq;
  string mS, mT;
  int ans;
  vector<int> vs, vt;

  void decode();

public:
  cBinarySearch(const int &n, const int &q, const string &s, const string &t) {
    mn = n;
    mq = q;
    mS = s;
    mT = t;
    ans = 0;

    decode();
  }
  void search();
  void output();
};

void cBinarySearch::search() {
  int b, pb;
  int count = 0;
  for (int i = 0; i < vt.size(); ++i) {
    b = vs.size() / 2;
    pb = 0;
    while (b >= 0) {
      if (vt[i] == vs[b]) {
        ++count;
        break;
      } else {
        if (pb != b && vt[i] > vs[b]) {
          pb = b;
          b = b / 2 + vs.size() / 2;
        } else if (pb != b && vt[i] < vs[b]) {
          pb = b;
          b = b / 2;
        } else if (pb == b) {
          break;
        }
      }
    }
  }
  ans = count;
}

void cBinarySearch::output() { cout << ans << endl; }

void cBinarySearch::decode() {
  istringstream iss;
  iss.str(mS);
  int ts;
  for (int i = 0; i < mn; ++i) {
    iss >> ts;
    vs.push_back(ts);
  }
  iss.str(mT);
  iss.seekg(0, iss.beg);
  for (int i = 0; i < mq; ++i) {
    iss >> ts;
    vt.push_back(ts);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  string s, t;
  cin >> n;
  cin.ignore();
  getline(cin, s);
  cin >> q;
  cin.ignore();
  getline(cin, t);

  cBinarySearch cbs(n, q, s, t);
  cbs.search();
  cbs.output();

  return 0;
}