alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int BinarySearch(vector<int> A, int key) {
  int left = 0;
  int right = A.size();

  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] == key) {
      return 1;
    } else if (key < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return 0;
}

int main() {
  int n, q, c = 0;
  vector<int> S, T;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    S.push_back(v);
  }

  cin >> q;
  for (int j = 0; j < q; j++) {
    int v;
    cin >> v;
    T.push_back(v);
  }

  for (auto v1 : T) {
    if (BinarySearch(S, v1)) {
      c++;
    }
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

//============================================================================
// Name        : procon.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, q, sum;
  vector<int> S, T;

  cin >> n;
  for (int i = 0; i < n; i++) {
    int val;
    cin >> val;
    S.push_back(val);
  }

  cin >> q;
  for (int j = 0; j < q; j++) {
    int val;
    cin >> val;
    T.push_back(val);
  }

  for (auto v1 : T) {
    for (auto v2 : S) {
      if (v1 == v2) {
        sum++;
        break;
      }
    }
  }

  cout << sum << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

bool BinarySearch(int t, vector<int> S) {
  int start = 0;
  int end = S.size() - 1;
  do {
    int middle = (end - start) / 2 + start;
    if (t == S[middle]) {
      return true;
    } else if (t < S[middle]) {
      end = middle - 1;
    } else if (t > S[middle]) {
      start = middle + 1;
    }
    //		cout << t << " " << start << " " << middle " " << end << endl;
  } while (end - start >= 0);
  return false;
}

int main() {
  int Count = 0;
  int n;
  cin >> n;
  vector<int> S;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    S.push_back(s);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (BinarySearch(t, S)) {
      Count++;
      //			cout << "t:" << t << endl;
    }
  }
  cout << Count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

bool BinarySearch(int t, vector<int> S) {
  int start = 0;
  int end = S.size() - 1;
  do {
    int middle = (end - start) / 2 + start;
    if (t == S[middle]) {
      return true;
    } else if (t < S[middle]) {
      end = middle - 1;
    } else if (t > S[middle]) {
      start = middle;
    }
  } while (end - start >= 1);
  return false;
}

int main() {
  int Count = 0;
  int n;
  cin >> n;
  vector<int> S;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    S.push_back(s);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (BinarySearch(t, S)) {
      Count++;
    }
  }
  cout << Count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdlib.h>

using namespace std;
int ceil(double x) { return (int)(x + 0.9); }
int binarySearch(int n, int *s, int q, int *t) {
  int sum = 0;

  for (int i = 0; i < q; i++) {
    int mid = n / 2;
    int left = 0;
    int right = n;
    while (left != right) {
      if (s[mid] == t[i]) {
        sum++;
        break;
      }
      if (s[mid] > t[i]) {
        right = mid;
        mid = (left + right) / 2;
      }
      if (s[mid] < t[i]) {
        left = mid;
        mid = (left + right) / 2;
      }
      if (left == mid || right == mid) {
        break;
      }
    }
  }
  return sum;
}
int main() {
  int n = 0;
  int q = 0;
  cin >> n;

  int *s = (int *)malloc(sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }
  cin >> q;
  int *t = (int *)malloc(sizeof(int) * q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &t[i]);
  }
  int sum = binarySearch(n, s, q, t);
  printf("%d\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;
int ceil(double x) { return (int)(x + 0.9); }
int binarySearch(int n, int *s, int q, int *t) {
  int sum = 0;

  for (int i = 0; i < q; i++) {
    int mid = ceil(n / 2);
    double left = 0;
    double right = n - 1;
    while (right != left) {
      if (s[mid] == t[i]) {
        sum++;
        break;
      }
      if (s[mid] > t[i]) {
        right = mid;
        mid = left + ceil((right - left) / 2);
      }
      if (s[mid] < t[i]) {
        left = mid;
        mid = mid + ceil((right - left) / 2);
      }
    }
  }
  return sum;
}
int main() {
  int n = 0;
  int q = 0;
  cin >> n;

  int *s = (int *)malloc(sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }
  cin >> q;
  int *t = (int *)malloc(sizeof(int) * q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &t[i]);
  }
  int sum = binarySearch(n, s, q, t);
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;

int getSameNum(int[], int[], int, int);

int main() {
  int n, q;
  scanf("%d", &n);
  int s[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &s[i]);
  scanf("%d", &q);
  int t[q];
  for (int i = 0; i < q; i++)
    scanf("%d", &t[i]);
  sort(t, t + q);
  printf("%d\\n", getSameNum(s, t, n, q));
  return 0;
}

int getSameNum(int s[], int t[], int n, int q) {
  int cnt = 0, mem = 0;
  for (int i = 0; i < q; i++)
    for (int j = mem; j < n; j++)
      if (t[i] == s[j]) {
        cnt++;
        mem = j;
        break;
      }
  return cnt;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

int getSameNum(int[], int[], int, int);

int main() {
  int n, q;
  scanf("%d", &n);
  int s[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &s[i]);
  scanf("%d", &q);
  int t[q];
  for (int i = 0; i < q; i++)
    scanf("%d", &t[i]);
  sort(t, t + q);
  printf("%d\\n", getSameNum(s, t, n, q));
  return 0;
}

int getSameNum(int s[], int t[], int n, int q) {
  int cnt = 0;
  for (int i = 0; i < q; i++)
    for (int j = 0; j < n; j++)
      if (t[i] == s[j]) {
        cnt++;
        break;
      }
  return cnt;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <vector>

int main(void) {
  int ans = 0;
  int n, q;
  std::cin >> n;
  std::vector<int> arr(n);
  for (auto &a : arr)
    std::cin >> a;
  std::cin >> q;
  for (size_t i = 0; i < q; i++) {
    int in = 0;
    std::cin >> in;
    int left = 0, mid = n / 2, right = n;
    while (left != right) {
      if (arr[mid] == in) {
        ans++;
        break;
      }
      if (in < arr[mid])
        right = mid;
      else
        left = mid + 1;

      mid = (right + left) / 2;
    }
  }

  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <vector>

int main(void) {
  int ans = 0;
  int n, q;
  std::cin >> n;
  std::vector<int> arr(n);
  for (auto &a : arr)
    std::cin >> a;
  std::cin >> q;
  for (size_t i = 0; i < q; i++) {
    int in = 0;
    std::cin >> in;
    int left = 0, mid = n / 2, right = n;
    while (left != right) {
      if (arr[mid] == in) {
        ans++;
        break;
      }
      if (in < arr[mid])
        right = mid;
      else
        left = mid;

      mid = (right + left) / 2;
    }
  }

  std::cout << ans << std::endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>

#define S_SIZE 100000

using namespace std;

int S[S_SIZE];

bool isContain(int left, int right, int number) {
  int m = (left + right) / 2;
  if (left == right) {
    if (S[m] == number) {
      return true;
    } else {
      return false;
    }
  } else if (S[m] == number) {
    return true;
  } else if (S[m] < number) {
    return isContain(m + 1, right, number);
  } else { // S[m] > number
    if (right - left == 1) {
      return isContain(left, m, number);
    } else {
      return isContain(left, m - 1, number);
    }
  }
}

int main() {

  int tmp;
  int n, q, count = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }

  int left, right;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &tmp);
    left = 0;
    right = n - 1;
    if (isContain(left, right, tmp)) {
      count++;
    }
  }
  printf("%d\\n", count);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

#define S_SIZE 100000

using namespace std;

int S[S_SIZE];

bool isContain(int left, int right, int number) {
  int m = (left + right) / 2;
  if (left == right && S[m] != number) {
    return false;
  } else if (S[m] == number) {
    return true;
  } else if (S[m] < number) {
    return isContain(m + 1, right, number);
  } else { // S[m] > number
    return isContain(left, m - 1, number);
  }
}

int main() {

  int tmp;
  int n, q, count = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }

  int left, right;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &tmp);
    left = 0;
    right = n - 1;
    if (isContain(left, right, tmp))
      count++;
  }
  printf("%d\\n", count);
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

int binarySearch(int ary[], int n, int key) {

  int left = 0;
  int right = n;
  int mid = n / 2;

  while (left < right) {

    if (ary[mid] == key)
      return 1;

    else if (key < ary[mid])
      right = mid;

    else
      left = mid + 1;

    mid = (left + right) / 2;
  }

  return 0;
}

int main() {

  int i, j, key, answer;
  int num_of_ary, num_of_key;
  int ary[100010];

  answer = 0;

  std::cin >> num_of_ary;
  for (i = 0; i <= num_of_ary - 1; i++)
    std::cin >> ary[i];

  std::cin >> num_of_key;
  for (i = 0; i <= num_of_key - 1; i++) {

    std::cin >> key;
    answer += binarySearch(ary, num_of_ary, key);
  }

  std::cout << answer << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

int binarySearch(int ary[], int n, int key) {

  int left = 0;
  int right = n;
  int mid = n / 2;

  while (left < right) {

    if (ary[mid] == key)
      return 1;

    else if (key < ary[mid])
      right = mid;

    else
      left = mid + 1;

    mid = (left + right) / 2;
  }

  return 0;
}

int main() {

  int i, j, key, answer;
  int num_of_ary, num_of_key;
  int ary[10010];

  answer = 0;

  std::cin >> num_of_ary;
  for (i = 0; i <= num_of_ary - 1; i++)
    std::cin >> ary[i];

  std::cin >> num_of_key;
  for (i = 0; i <= num_of_key - 1; i++) {

    std::cin >> key;
    answer += binarySearch(ary, num_of_ary, key);
  }

  std::cout << answer << std::endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

int A[1000000], n;

int binarySearch(int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    else if (key < A[mid])
      right = mid;
    else if (key > A[mid])
      left = mid + 1;
  }

  return 0;
}

int main() {
  int q, k, sum = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\\n", sum);
}

Error: TLE

Fixed submission:

#include <cstdio>

int A[1000000], n;

int binarySearch(int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    else if (key < A[mid])
      right = mid;
    else if (key > A[mid])
      left = mid;
  }

  return 0;
}

int main() {
  int q, k, sum = 0;

  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\\n", sum);
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

// ?????§????´¢
bool BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return true;

    if (key > A[mid])
      left = mid + 1;
    if (key < A[mid])
      right = mid;
  }
  return false;
}
int main() {
  int n, q, key;
  int sum = 0;
  int A[100000 + 10];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (BinarySearch(A, n, key))
      sum++;
  }
  printf("%d\\n", sum);
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

// ?????§????´¢
bool BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return true;

    if (key > A[mid])
      left = mid + 1;
    if (key < A[mid])
      right = mid;
  }
  return false;
}
int main() {
  int n, q, key;
  int sum = 0;
  int A[10000 + 10];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (BinarySearch(A, n, key))
      sum++;
  }
  printf("%d\\n", sum);
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
int a[100001];
int b[100001];

int binarySearch(int a[], int n, int data) {
  int left = 0, right = n - 1;
  while (left <= right) {
    int mid = (left + right) / 2;
    if (a[mid] == data) {
      return mid;
    } else if (a[mid] < data) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &b[i]);
  }

  int cnt = 0;
  for (int i = 0; i < m; i++) {
    if (binarySearch(a, n, b[i]) >= 0) {
      cnt++;
    }
  }
  printf("%d\\n", cnt);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <set>
using namespace std;
set<int> p, q;
set<int>::iterator pit;
set<int>::iterator qit;
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    p.insert(a);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int b;
    scanf("%d", &b);
    q.insert(b);
  }

  int cnt = 0;
  for (pit = p.begin(); pit != p.end(); pit++) {
    for (qit = q.begin(); qit != q.end(); qit++) {
      if (*pit == *qit) {
        cnt++;
        break;
      }
    }
  }
  printf("%d\\n", cnt);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int n;
int q;
vector<int> S;

bool binary_search(int key) {
  int h = 0;
  int t = n;

  while (h < t) {
    int mid = (h + t) >> 1;
    if (S[mid] == key) {
      return true;
    } else if (key < S[mid]) {
      t = mid;
    } else {
      h = mid + 1;
    }
  }

  return false;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int s;
    scanf("%d", &s);
    S.push_back(s);
  }

  scanf("%d", &q);
  int ans = 0;
  for (int i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    if (binary_search(t))
      ans++;
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

#define MAX_N (10010)
#define MAX_Q (510)
int n;
int q;
vector<int> S;
int T[MAX_Q];

bool bilinear_search(int key) {
  int h = 0;
  int t = n;
  while (h < t) {
    int mid = (h + t) >> 1;
    if (S[mid] == key)
      return true;
    else if (key < S[mid]) {
      t = mid;
    } else {
      h = mid + 1;
    }
  }
  return false;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int s;
    scanf("%d", &s);
    S.push_back(s);
  }

  sort(S.begin(), S.end());

  scanf("%d", &q);
  int ans = 0;
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
    if (bilinear_search(T[i]))
      ans++;
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;

// 二分搜索
bool BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return true;

    if (key > A[mid])
      left = mid + 1;
    if (key < A[mid])
      right = mid;
  }
  return false;
}
int main() {
  int n, q, key;
  int sum = 0;
  int A[100000 + 10];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (BinarySearch(A, n, key))
      sum++;
  }
  printf("%d\\n", sum);
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
int A[100000 + 10], n;

int binary_searcher(int key) {
  int mid, left = 0, right = n;
  while (left <= right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return mid;
    else if (key < A[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return 0;
}

int main() {
  int i, q, key, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binary_searcher(key))
      sum++;
  }
  printf("%d", sum);
  return sum;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

// 二分搜索
bool BinarySearch(int A[], int n, int key) {
  int left = 0, right = n, mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return true;

    if (key > A[mid])
      left = mid + 1;
    if (key < A[mid])
      right = mid;
  }
  return false;
}
int main() {
  int n, q, key;
  int sum = 0;
  int A[100000 + 10];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (BinarySearch(A, n, key))
      sum++;
  }
  printf("%d\\n", sum);
}


Error: 2

Fixed submission:

#include <iostream>

using namespace std;
long A[1000000], n;

int binary_searcher(int key) {
  int mid = 0;
  int left = 0;
  int right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (A[mid] == key)
      return mid;
    else if (key < A[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return 0;
}

int main() {
  int i = 0;
  int q = 0;
  int sum = 0;
  long key = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> key;
    if (binary_searcher(key))
      sum++;
  }
  cout << sum;
  return sum;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

int n;
int *S;

bool find(int t) {
  int left = -1;
  int right = n;

  while (right - left > 1) {
    int x = (right + left) / 2;
    if (S[x] < t) {
      left = x;
    } else {
      right = x;
    }
  }
  if (S[right] == t) {
    return true;
  }
  return false;
}
int main() {
  int count = 0;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    S[i] = atoi(str.c_str());
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t;
    string str;
    cin >> str;
    t = atoi(str.c_str());
    if (find(t)) {
      count++;
    }
  }

  delete[] S;

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>

using namespace std;

static int n;
static int *S;

bool find(int t) {
  int left = -1;
  int right = n;

  while (right - left > 1) {
    int x = (right - left) / 2;
    if (S[x] < t) {
      left = x;
    } else {
      right = x;
    }
  }
  if (S[right] == t) {
    return true;
  }
  return false;
}
int main() {
  int count = 0;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int t = 0;
    scanf("%d", &t);
    if (find(t)) {
      count++;
    }
  }

  delete[] S;

  cout << count << endl;

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, q;
  cin >> n;
  vector<int> S(n);
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; i++)
    cin >> T[i];

  int sum = 0;

  for (int i = 0; i < q; i++) {
    int x = T[i];
    int low = 0, high = n - 1, mid;
    while (low <= high) {
      mid = (low + high) / 2;
      if (S[mid] == x) {
        sum++;
        break;
      } else if (S[mid] > x) {
        high = mid - 1;
      } else
        low = mid + 1;
    }
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  int n, q;
  cin >> n;
  vector<int> S(n);
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; i++)
    cin >> T[i];
  map<int, int> m;

  int sum = 0;
  for (int x : T) {
    if (m.find(x) != m.end() && m[x] == 1) {
      sum++;
    } else {
      int low = 0, high = n - 1, mid;
      while (low <= high) {
        mid = (low + high) / 2;
        if (S[mid] == x) {
          sum++;
          m[x] = 1;
          break;
        } else if (S[mid] > x) {
          high = mid - 1;
        } else
          low = mid + 1;
      }
    }
  }
  cout << sum;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

/*
 * Problem Description: Linear Search
 * Category:
 * Author: Khan
 * Date: 13th October, 2017
 */

#include <algorithm>
#include <cstdio>

int main(int argc, char const *argv[]) {
  // freopen("sample.txt", "r", stdin);
  int i, n, A[1000000], q, key, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    if (std::binary_search(A, A + n, key))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}

Error: 0

Fixed submission:

/*
 * Problem Description: Linear Search
 * Category:
 * Author: Khan
 * Date: 13th October, 2017
 */

#include <algorithm>
#include <cstdio>

using namespace std;

int main(int argc, char const *argv[]) {
  // freopen("sample.txt", "r", stdin);
  int i, n, A[10000 + 1], q, key, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &key);
    if (binary_search(A, A + n, key))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}