alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int S[100000];

int main(void) {

  int n;
  cin >> n;

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

  int q, total = 0;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    int left = 0, right = n - 1, mid = 0;
    while (left <= right) {
      mid = (left + right) / 2;
      if (t == S[mid]) {
        ++total;
        break;
      }
      if (t > S[mid]) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int S[100000];

int main(void) {

  int n;
  cin >> n;

  memset(S, -1, sizeof(int) * 100000);
  for (int i = 0; i < n; ++i) {
    int s;
    cin >> s;
    int j = 0;
    while (S[j] != -1 && S[j] != s) {
      //            printf("[insert] j = %d\\n", j);
      if (s < S[j]) {
        j = j * 2 + 1;
      } else {
        j = j * 2 + 2;
      }
    }
    S[j] = s;
  }

  int q, total = 0;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    int j = 0;
    while (S[j] != -1) {
      if (S[j] == t) {
        ++total;
        break;
      } else if (S[j] < t) {
        j = j * 2 + 2;
      } else {
        j = j * 2 + 1;
      }
    }
  }

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

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

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, S[100000], q, T[50000], c = 0, i, i2, toread, cnt;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (i = 0; i < q; i++)
    cin >> T[i];
  /*for(i=0;i<n;i++){
          for(i2=0;i2<n;i2++){
                  if(S[i]<S[i2]){
                          toread=S[i];
                          S[i]=S[i2];
                          S[i2]=toread;
                  }
          }
  }*/
  sort(S, S + n);
  for (i = 0; i < q; i++) {
    cnt = 0;
    toread = n;
    while (toread > 1) {
      if (toread % 2 == 0)
        toread /= 2;
      else
        toread = (toread + 1) / 2;
      if (S[(toread - 1) + cnt] < T[i])
        cnt += toread;
      else if (S[(toread - 1) + cnt] == T[i]) {
        c++;
        break;
      }
    }
  }
  cout << c << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, S[100000], q, T[50000], c = 0, i, i2, toread, cnt;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> S[i];
  cin >> q;
  for (i = 0; i < q; i++)
    cin >> T[i];
  for (i = 0; i < n; i++) {
    for (i2 = 0; i2 < n; i2++) {
      if (S[i] < S[i2]) {
        toread = S[i];
        S[i] = S[i2];
        S[i2] = toread;
      }
    }
  }
  for (i = 0; i < q; i++) {
    cnt = 0;
    toread = n;
    while (toread > 1) {
      if (toread % 2 == 0)
        toread /= 2;
      else
        toread = (toread + 1) / 2;
      if (S[(toread - 1) + cnt] < T[i])
        cnt += toread;
      else if (S[(toread - 1) + cnt] == T[i]) {
        c++;
        break;
      }
    }
  }
  cout << c << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int s[100000] = {0};

int binary_search(int key, int left, int right) {
  int center;

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

    if (s[center] == key) {
      return 1;
    } else if (s[center] < key) {
      left = center + 1;
    } else {
      right = center;
    }
  }

  return 0;
}

int main() {
  int n, q, t[50000] = {0};
  int count = 0;

  cin >> n;

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> t[i];
    count += binary_search(t[i], 0, n);
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int binary_search(int key, int left, int right) {
  int center = (left + right) / 2;

  if (left == right && key != left) {
    return 0;
  }

  if (key == center) {
    return 1;
  } else if (key < center) {
    return binary_search(key, left, center);
  } else {
    return binary_search(key, center, right);
  }
}

int main() {
  int n, q, s[100000] = {0}, t[50000] = {0};
  int count = 0;

  cin >> n;

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> t[i];
    count = binary_search(t[i], s[0], s[n - 1]);
  }

  cout << count << endl;

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int s[1000000], n;

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

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

int main(void) {
  int i, q, t, c = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &t);
    if (binarySearch(t))
      c++;
  }
  printf("%d\\n", c);
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
using namespace std;

int n, s[10000];

int binarySearch(int t) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (s[mid] == t)
      return 1;
    if (t > s[mid])
      left = mid + 1;
    else if (t < s[mid])
      right = mid;
  }
  return 0;
}

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

#include <iostream>
using namespace std;

int D[100010];

int main() {
  int n, q, t, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> D[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    int left = 0, right = n;
    cin >> t;
    while (left < right) {
      int mid = (left + right) / 2;
      if (D[mid] == t) {
        c++;
        break;
      } else if (t < D[mid])
        right = mid;
      else
        left = mid + 1;
    }
  }
  cout << c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int D[100010];

int main() {
  int n, q, t, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> D[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    int left = 0, right = n;
    cin >> t;
    while (left < right) {
      int mid = (left + right) / 2;
      if (D[mid] == t)
        c++;
      else if (t < D[mid])
        right = mid;
      else
        left = mid + 1;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a[100000], b, c[50000], i, k = 0, j;
  for (cin >> n, i = 0; i < n; i++)
    cin >> a[i];
  for (cin >> b, i = 0; i < b; i++)
    cin >> c[i];
  for (i = 0; i < b; i++)
    if (binary_search(a, a + n, c[i]))
      k++;
  cout << k << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[10000], b, c[10000], i, k = 0, j;
  for (cin >> n, i = 0; i < n; i++)
    cin >> a[i];
  for (cin >> b, i = 0; i < b; i++)
    cin >> c[i];
  for (i = 0; i < b; i++)
    if (binary_search(a, a + n, c[i]))
      k++;
  cout << k << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
main() {
  int n, sum = 0, p;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int m;
  cin >> m;
  while (m--) {
    cin >> p;
    int l = 0, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      if (a[mid] == p) {
        sum++;
        break;
      }
      if (p < a[mid])
        r = mid;
      else
        l = mid + 1;
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, sum = 0, p;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int m;
  cin >> m;
  while (m--) {
    int p;
    cin >> p;
    int l = 0, r = n;
    while (l < r) {
      int mid = (l + r) / 2 + 1;
      if (a[mid] == p) {
        sum++;
        break;
      }
      if (p < a[mid])
        r = mid;
      else
        l = mid;
    }
  }
  cout << sum << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

  int x = 0;
  int mid;
  int left = 0;
  int right = n;
  for (int j = 0; j < q; j++) {
    left = 0;
    right = n;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[j]) {
        x++;
        break;
      } else if (T[j] < S[mid]) {
        right = mid;
      } else if (T[j] > S[mid]) {
        left = mid + 1;
      }
    }
  }
  cout << x << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  int x = 0;
  int mid;
  int left = 0;
  int right = n;
  for (int j = 0; j < q; j++) {
    left = 0;
    right = n;
    while (left < right) {
      mid = (left + right) / 2;
      if (S[mid] == T[j]) {
        x++;
        break;
      } else if (T[j] < S[mid]) {
        right = mid;
      } else if (T[j] > S[mid]) {
        left = mid;
      }
    }
  }
  cout << x << endl;

  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

// 含有标记的线性搜索可以将算法效率提高常数倍
// for(int i=0;i<n;i++)
// s[n]==key;while(s[i]!=key)i++;if(i!=n)
// 区别在于主循环中比较运算的次数
#include <cstdio>
#include <iostream>
using namespace std;
int s[100005];
int cnt, n;
/*void LinearSearch(int s[],int num)
{
        for(int i=0;i<n;i++)
        {
                if(s[i]==num)
                {
                        cnt++;
                        break;
                }
        }
}*/
int BinarySearch(int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (s[mid] == key) {
      // cout<<"1"<<endl;
      return 1;
    } else if (key < s[mid]) // 前半部分
    {
      right = mid;
    } else // 后半部分
    {
      left = mid + 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int k = 0; k < n; k++) {
    scanf("%d", &s[k]);
    // cin>>s[k];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int nn;
    // cin>>nn;
    scanf("%d", &nn);
    if (BinarySearch(nn)) {
      cnt++;
      // cout<<nn<<endl;
    }
  }
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

// 含有标记的线性搜索可以将算法效率提高常数倍
// for(int i=0;i<n;i++)
// s[n]==key;while(s[i]!=key)i++;if(i!=n)
// 区别在于主循环中比较运算的次数
#include <cstdio>
#include <iostream>
using namespace std;
int s[10004];
int cnt, n;
/*void LinearSearch(int s[],int num)
{
        for(int i=0;i<n;i++)
        {
                if(s[i]==num)
                {
                        cnt++;
                        break;
                }
        }
}*/
int BinarySearch(int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (s[mid] == key) {
      // cout<<"1"<<endl;
      return 1;
    } else if (key < s[mid]) // 前半部分
    {
      right = mid;
    } else // 后半部分
    {
      left = mid + 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int k = 0; k < n; k++) {
    scanf("%d", &s[k]);
    // cin>>s[k];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int nn;
    // cin>>nn;
    scanf("%d", &nn);
    if (BinarySearch(nn)) {
      cnt++;
      // cout<<nn<<endl;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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

// returns the index of the element which value equals to key.
// if no elements matched, it returns -1.
int search(const vector<int> &x, int key) {
  int left = 0, right = x.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (x[mid] == key) {
      return mid;
    } else if (key < x[mid]) {
      right = mid;
    } else if (x[mid] < key) {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  vector<int> na(n, 0);
  for (int i = 0; i < n; i++)
    cin >> na[i];
  int q;
  cin >> q;
  int t;
  int ans = 0;
  for (int i = 0; i < q; ++i) {
    cin >> t;
    ans += search(na, t) >= 0 ? 1 : 0;
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

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

// returns the index of the element which value equals to key.
// if no elements matched, it returns -1.
int search(const vector<int> &x, int key) {
  int left = 0, right = x.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (x[mid] == key) {
      return mid;
    } else if (key < x[mid]) {
      right = mid;
    } else if (x[mid] < key) {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {
  int n;
  cin >> n;
  vector<int> na(0, n);
  for (int i = 0; i < n; i++)
    cin >> na[i];
  int q;
  cin >> q;
  int t;
  int ans = 0;
  for (int i = 0; i < q; ++i) {
    cin >> t;
    ans += search(na, t) >= 0 ? 1 : 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

  int n, q, t, count = 0;
  int left, right, center;
  int S[100000];

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    left = 0;
    right = n;
    while (left <= right) {
      center = (left + right) / 2;
      if (S[center] < t) {
        left = center + 1;
      } else if (S[center] > t) {
        right = center - 1;
      } else {
        count++;
        break;
      }
    }
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  int n, q, t, count = 0;
  int left = 0, right = n, center;
  int S[100000];

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    left = 0;
    right = n;
    while (left <= right) {
      center = (left + right) / 2;
      if (S[center] < t) {
        left = center + 1;
      } else if (S[center] > t) {
        right = center - 1;
      } else {
        count++;
      }
    }
  }

  cout << count << endl;

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

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int tmp1, tmp2, cnt = 0;
  cin >> tmp1;
  int lis[tmp1];
  for (int i = 0; i < tmp1; i++) {
    cin >> lis[i];
  }
  cin >> tmp2;
  int key[tmp2];
  for (int i = 0; i < tmp2; i++) {
    cin >> key[i];
    if (*lower_bound(lis, lis + tmp1, key[i]) == key[i]) {
      cnt++;
    }
  }
  cout << cnt << endl;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;

int b[1000000010];

int main() {
  int n, q, s, t;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    b[s]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (b[t] >= 1)
      ans++;
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#include <algorithm>
#include <vector>
/********
 *	????????¢?´¢ Binary Search
 *********/

int main(void) {
  int n;
  cin >> n;
  vector<int> strS;
  strS.resize(n);

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

  int q;
  cin >> q;
  vector<int> strT;
  strT.resize(q);

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

  int count = 0;
  for (int j = 0; j < q; j++) {
    bool result = binary_search(strS.begin(), strS.end(), strT[j]);
    if (result)
      count++;
  }

  /*	int left  = 0;	//strS[0];
          int right = n;	//strS[(n-1)];
          int middle= (right + left)/2;


          for(int j = 0; j<q ; j++){
                  if( strS[(middle)] < middle ){
                          left = middle;
                                  if(strT[j] == strS[i]){
                                          count++;
                                          break;
                                  }
                          }
                  } else if( strS[middle] > middle){
                          for(int i =(n/2); i == 0; i--){
                                  if(strT[j] == strS[i]){
                                          count++;
                                          break;
                                  }
                          }
                  }
          }
  */
  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

// #include <stdio.h>
// #include <algorithm>
// #include <string>
#include <vector>

/********
 *   ?????¢??¢?´¢ Linear Search
 *********/

/*
int linearSearch(int strS int strT, int n )
{
        int key;

        for(int i =0 ; i < n; i++){
                if(strS[i] == key){
                        return i;
                }
        }
        return NOT_FOUND;
}
*/

int main(void) {
  int n;
  cin >> n;
  vector<int> strS;
  strS.resize(n);

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

  int q;
  cin >> q;
  vector<int> strT;
  strT.resize(q);

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

  int count = 0;
  for (int j = 0; j < q; j++) {
    for (int i = 0; i < n; i++) {
      if (strT[j] == strS[i]) {
        count++;
        break;
      }
    }
  }
  cout << count << endl;

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

#include <iostream>

using namespace std;
int A[200000], n;

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

int main() {
  int i, q, sum = 0;
  int key;
  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 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
int A[200000], n;

int binary_searcher(int key) {
  int mid;
  int left = 0;
  int right = n - 1;
  while (left <= right) {
    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 i, q, sum = 0;
  int key;
  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 << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

bool binSearch(int x, int array[], int first, int last) {
  int mid = (first + last) / 2;
  bool ret = false;
  if (array[mid] == x) {
    ret = true;
  } else {
    if (first >= last) {
      ret = false;
    } else {
      if (x < array[mid]) {
        ret = binSearch(x, array, first, mid - 1);
      } else {
        ret = binSearch(x, array, mid + 1, last);
      }
    }
  }
  return ret;
}

int main(void) {
  int n, q;
  scanf("%d", &n);

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

  scanf("%d", &q);
  int x, sum = 0;
  for (int i = 0; i < q; i++) {
    scanf("%d", &x);
    if (binSearch(x, S, 0, n - 1)) {
      sum += 1;
    }
  }
  printf("%d\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

bool binSearch(int x, int array[], int first, int last) {
  int mid = (first + last) / 2;
  bool ret = false;
  if (array[mid] == x) {
    ret = true;
  } else {
    if (first == last) {
      ret = false;
    } else {
      if (array[mid] > x) {
        ret = binSearch(x, array, first, mid - 1);
      } else {
        ret = binSearch(x, array, mid + 1, last);
      }
    }
  }
  return ret;
}

int main(void) {
  int n, q;
  scanf("%d", &n);

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

  scanf("%d", &q);
  int x, sum = 0;
  for (int i = 0; i < q; i++) {
    scanf("%d", &x);
    if (binSearch(x, S, 0, n - 1)) {
      sum += 1;
    }
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int s[1000000];
int n;
bool search(int x) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (x == s[mid])
      return 1;
    if (x > s[mid])
      left = mid + 1;
    else if (x < s[mid])
      right = mid;
  }
  return 0;
}
int main() {
  int m, cnt = 0;
  int t[1000000];
  cin >> n;
  rep(i, n) cin >> s[i];
  cin >> m;
  rep(i, m) cin >> t[i];
  sort(s, s + n);
  sort(t, t + m);
  rep(i, m) {
    int x = t[i];
    if (search(x))
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool flag[1000000000];
signed main() {
  int n, m;
  cin >> n;
  int s[100000], t[100000];
  rep(i, n) cin >> s[i];
  cin >> m;
  int cnt = 0;
  rep(i, m) cin >> t[i];
  rep(i, n) {
    rep(j, m) {
      if (s[i] == t[j] && flag[s[i]] == false) {
        cnt++;
        flag[s[i]] = true;
        break;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ctype.h>
#include <math.h>
using namespace std;
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
struct omt {
  omt() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

bool binary_search(int n, int t, vector<int> S) {
  int l = 0, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (S[mid] == t)
      return true;
    else if (t < S[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return false;
}

int main() {
  int n, q;
  cin >> n;
  vector<int> S(n);
  for (auto &x : S)
    cin >> x;
  cin >> q;
  vector<int> T(q);
  for (auto &x : T)
    cin >> x;

  int cnt = 0;
  REP(i, q) {
    if (binary_search(n, T[i], S))
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ctype.h>
#include <math.h>
using namespace std;
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
struct omt {
  omt() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

int main() {
  int n, q;
  cin >> n;
  vector<int> S(n);
  for (auto &x : S)
    cin >> x;
  cin >> q;
  vector<int> T(q);
  for (auto &x : T)
    cin >> x;

  int cnt = 0;
  REP(i, q) {
    REP(j, n) {
      if (T[i] == S[j]) {
        cnt++;
        break;
      }
    }
  }
  cout << cnt << endl;

  return 0;
}