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

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

int main() {
  int Max, Min;
  int N;
  int R[200000];

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

  Min = R[0];
  Max = -2000000000;

  for (int i = 1; i < N; i++) {
    Max = max(Max, R[i] - Min);
    Min = min(Min, R[i]);
  }

  cout << Max << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, R[200000];
  int Max = -9999999999;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> R[i];
  }

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {

      Max = max(Max, R[j] - R[i]);
    }
  }

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

#include <iostream>
using namespace std;

int main() {
  int n, R[200000], min;
  int answer = -999999999;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];
  min = R[0];
  for (int i = 1; i < n; i++) {
    if (min > R[i]) {
      if (answer < R[i] - min)
        answer = R[i] - min;
      min = R[i];
    } else if (min <= R[i]) {
      if (answer < R[i] - min)
        answer = R[i] - min;
    }
  }
  cout << answer << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, R[200000];
  int answer = -999999999; //-1000000000;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (answer < R[j] - R[i])
        answer = R[j] - R[i];
    }
  }
  cout << answer << endl;

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

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

int main() {
  int N;
  cin >> N;
  vector<int> R(N);
  for (int i = 0; i < N; ++i)
    cin >> R[i];
  int mini = R[0];
  int ans = INT_MIN;
  for (int i = 1; i < N; ++i) {
    ans = max(ans, R[i] - mini);
    mini = min(mini, R[i]);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> R(N);
  for (int i = 0; i < N; ++i)
    cin >> R[i];
  int ans = INT_MIN;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (j > i)
        ans = max(ans, R[j] - R[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int *arr = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int maxv = arr[1] - arr[0];
  int minv = arr[0];
  for (int i = 1; i < n; i++) {
    maxv = max(maxv, arr[i] - minv);
    minv = min(minv, arr[i]);
  }
  delete[] arr;
  cout << maxv << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n;
  cin >> n;
  int *arr = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int maxv = arr[1] - arr[0];
  for (int i = 2; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (arr[i] - arr[j] > maxv)
        maxv = arr[i] - arr[j];
    }
  }
  delete[] arr;
  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int x = -2000000000, y = a.at(0);
  for (int i = 1; i < n; i++) {
    x = max(x, a.at(i) - y);
    y = min(y, a.at(i));
  }
  cout << x << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int ans;
  for (int i = 0; i < n - 1; i++) {
    int x = a.at(i), y = a.at(i + 1);
    for (int j = i + 1; j < n; j++) {
      if (a.at(j) > y)
        y = a.at(j);
    }
    if (i == 0)
      ans = y - x;
    else
      ans = max(ans, y - x);
  }
  cout << ans << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

// 模範解答 from プログラミングコンテスト攻略のためのアルゴリズムとデータ構造
// (マイナビ)

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int R[MAX], n;

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

  int maxv = -2000000000; // 問題の制約を踏まえて順分に小さい初期値
  int minv = R[0];

  for (int i = 1; i < n;
       i++) { // "i = 0"は初期値として使用したので"i = 1"からスタート
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }

  cout << maxv << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits>

int InputAsInt();

int main() {
  int n = InputAsInt();
  int prices[n];
  for (int i = 0; i < n; ++i) {
    prices[i] = InputAsInt();
  }

  int max_profit = std::numeric_limits<int>::min();
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int profit = prices[j] - prices[i];
      if (profit > max_profit) {
        max_profit = profit;
      }
    }
  }

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

int InputAsInt() {
  int result;
  std::cin >> result;
  return result;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

/***
 *	?????§?????????
 ****/

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

int main(void) {
  int n;
  cin >> n;
  vector<int> fxR(n);

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

  int maxR = fxR[1] - fxR[0];
  int minR = fxR[0];

  for (int j = 1; j < n; j++) { // n???????????????????????°??????????¢????
    if (maxR < (fxR[j] - minR)) {
      maxR = fxR[j] - minR;
    }
    if (minR > fxR[j]) {
      minR = fxR[j];
    }
  }

  cout << maxR << endl; //?????§???????????¨?????????

  return 0;
}

Error: TLE

Fixed submission:

/***
 *	?????§?????????
 ****/

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

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

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

  int maxR = fxR[1] - fxR[0];
  for (int i = 0; i < (n - 1); i++) { // n???????????????????????°??????????¢????
    for (int j = (i + 1); j < n; j++) {
      int tempMax = fxR[j] - fxR[i];
      if (maxR < tempMax) {
        maxR = tempMax;
      }
    }
  }

  cout << maxR << endl; //?????§???????????¨?????????

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

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

int main() {
  int n, ans, tmp = 1000000001, num;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    if (i == 1)
      ans = num - tmp;
    if (num >= tmp)
      ans = (num - tmp > ans) ? num - tmp : ans;
    else
      tmp = num;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, ans = 1 - 1000000000;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++)
    ans = (ans < *max_element(a + i, a + n) - a[i - 1])
              ? *max_element(a + i, a + n) - a[i - 1]
              : ans;
  cout << ans << endl;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int i, n, d, min, max, R_t[200000];
  cin >> n >> R_t[0] >> R_t[1];
  min = R_t[0];
  max = R_t[1] - R_t[0];
  if (R_t[0] > R_t[1])
    min = R_t[1];

  for (i = 2; i < n; i++) {
    cin >> R_t[i];
    d = R_t[i] - min;
    if (max < d)
      max = d;
    if (min > R_t[i])
      min = R_t[i];
  }

  cout << max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int i, j, n, max, R_t[200000];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> R_t[i];

  max = R_t[1] - R_t[0];
  for (i = 2; i < n; i++) {
    for (j = 0; j < i; j++) {
      if (max < R_t[i] - R_t[j])
        max = R_t[i] - R_t[j];
    }
  }
  cout << max << endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long n;
  cin >> n;
  long long maxx = -2000000000, minx = 2000000000;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    maxx = max(maxx, x - minx);
    minx = min(x, minx);
  }
  cout << maxx << endl;
  char c;
  cin >> c;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, a, maxx;
  vector<int> l;
  cin >> n >> a;
  l.push_back(a);
  for (int i = 1; i < n; i++) {
    cin >> a;
    if (i == 1)
      maxx = a - l[0];
    else {
      for (int i = 0; i != l.size(); i++)
        maxx = max(maxx, a - l[i]);
    }
    l.push_back(a);
  }
  cout << maxx << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int max_value(int maxv, int max) {
  if (maxv > max) {
    return maxv;
  } else {
    return max;
  }
}

int min_value(int minv, int min) {
  if (minv < min) {
    return minv;
  } else {
    return min;
  }
}

int main() {
  int i, n, r[200000];

  cin >> n;

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

  int maxv = -2000000000;
  int minv = r[0];

  for (i = 1; i < n; i++) {
    maxv = max_value(maxv, r[i] - minv);
    minv = min_value(minv, r[i]);
  }

  cout << maxv << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int i, sa = -1000000000, n, r[200000];

  cin >> n;

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

  for (n--; n > 0; n--) {
    for (i = 0; i < n; i++) {
      if (sa < r[n] - r[i]) {
        sa = r[n] - r[i];
      }
    }
  }

  cout << sa << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200001] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int max = a[1] - a[0];
  int min = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] - min > max)
      max = a[i] - min;
    if (min > a[i])
      min = a[i];
  }
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200001] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int max = a[1] - a[0];
  for (int i = 1; i < n; i++)
    for (int j = 0; j < i; j++)
      if (a[i] - a[j] > max)
        max = a[i] - a[j];
  cout << max << endl;
  return 0;
}
Problem p02258 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) for (int i = 0; i < (a); i++)

int main() {
  int n;
  cin >> n;

  long min = 1e10;
  long res = -1e10;
  REP(i, n) {
    int a;
    cin >> a;

    if (a - min > res)
      res = a - min;
    if (a < min)
      min = a;
  }

  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) for (int i = 0; i < (a); i++)

int main() {
  int n;
  cin >> n;

  int prices[200000];
  REP(i, n) { cin >> prices[i]; }

  int res = -1e9;
  REP(i, n) {
    FOR(j, i + 1, n) {
      int gap = prices[j] - prices[i];
      if (gap > res)
        res = gap;
    }
  }

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

#include <iostream>
#define MAXN 200000 + 10
using namespace std;
int s[MAXN];
int t[MAXN];
int ns, nt;
int tot, l, r, mid;
int main() {
  int i, now;
  cin >> ns;
  for (i = 0; i < ns; ++i)
    cin >> s[i];
  cin >> nt;
  for (i = 0; i < nt; ++i)
    cin >> t[i];

  for (i = 0; i < nt; ++i) {
    now = t[i];
    l = 0;
    r = ns;
    while (l < r) {
      mid = l + (r - l) / 2;
      if (s[mid] == now) {
        tot++;
        break;
      }
      if (s[mid] < now)
        l = mid + 1;
      else
        r = mid;
    }
  }
  cout << tot << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAXN 200000 + 10
using namespace std;
int s[MAXN];
int t[MAXN];
int ns, nt;
int tot, l, r, mid;
int main() {
  int i, now;
  cin >> ns;
  for (i = 0; i < ns; ++i)
    cin >> s[i];
  cin >> nt;
  for (i = 0; i < nt; ++i)
    cin >> t[i];

  for (i = 0; i < nt; ++i) {
    now = t[i];
    l = 0;
    r = ns;
    while (l < r) {
      mid = l + (r - l) / 2;
      if (s[mid] == now) {
        tot++;
        break;
      }
      if (s[mid] < now)
        l = mid;
      else
        r = mid;
    }
  }
  cout << tot << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int cnt = 0;

bool binary_search(vector<int> &data, int keyv) {
  int r_ind = data.size();
  int l_ind = 0;
  while (l_ind < r_ind) {
    int c_ind = (l_ind + r_ind) / 2;
    if (data[c_ind] == keyv) {
      return true;
    } else if (keyv < data[c_ind]) {
      r_ind = c_ind;
    } else {
      l_ind = c_ind + 1;
    }
  }
  return false;
}

void solve(vector<int> &sorted, vector<int> &unsorted) {
  int l = unsorted.size();
  for (int i = 0; i < l; i++) {
    if (binary_search(sorted, unsorted[i])) {
      cnt++;
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> S(n);
  rep(i, n) { cin >> S[i]; }

  int q;
  cin >> q;
  vector<int> T(q);
  rep(i, q) { cin >> T[i]; }

  solve(S, T);

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <stack>

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int main() {

  int n;
  cin >> n;
  int num;
  list<int> S;

  rep(i, n) {
    cin >> num;
    S.push_front(num);
  }

  int q;
  cin >> q;
  stack<int> T;
  rep(i, q) {
    cin >> num;
    T.push(num);
  }

  int cnt = 0;
  int l = T.size();
  while (l != 0) {
    num = T.top();
    T.pop();
    for (auto itr = S.begin(); itr != S.end(); itr++) {
      if (num == *itr) {
        cnt++;
        break;
      }
    }
    l--;
  }

  cout << cnt << endl;

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

#include <algorithm>
#include <cstdio>

static const size_t n_max = 100000;
static int S[n_max];

int main() {
  size_t n, q;
  scanf("%zu", &n);
  for (size_t i = 0; i < n; i++)
    scanf("%d", S + i);
  scanf("%zu", &q);
  int c = 0;
  for (size_t i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    if (std::binary_search(S, S + n, t))
      c++;
  }
  printf("%d\\n", c);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

static const size_t n_max = 10000;
static int S[n_max];

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

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

int binarySearch(int A[], int n, int key) {
  int left = 0;
  int right = n;
  int m;

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

int main() {
  int n, q, sum = 0;
  int S[100000], T[100000];

  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);
  cin >> q;
  for (int i = 0; i < q; i++)
    scanf("%d", &T[i]);

  for (int i = 0; i < q; i++) {
    if (binarySearch(S, n, T[i]))
      sum++;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

int binarySearch(int A[], int n, int key) {
  int left = 0;
  int right = n - 1;
  int m;

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

int main() {
  int n, q, sum = 0;
  int S[100000], T[100000];

  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);
  cin >> q;
  for (int i = 0; i < q; i++)
    scanf("%d", &T[i]);

  for (int i = 0; i < q; i++) {
    if (binarySearch(S, n, T[i]))
      sum++;
  }
  cout << sum << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n = 0;
  int q = 0;
  int S[100000] = {0};
  int T[50000] = {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];

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


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n = 0;
  int q = 0;
  int S[100000] = {0};
  int T[50000] = {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];

  int count = 0;
  for (int i = 0; i < q; i++) {
    int left = 0;
    int right = n;
    while (left < right) {
      int x = (left + right) / 2;
      if (T[i] == S[x]) {
        count++;
        break;
      } else if (T[i] > S[x])
        left = x;
      else
        right = x + 1;
    }
  }
  cout << count << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

int n, q, t, ans = 0;

int bin(int t, int *S, int l = 0, int h = n - 1) {
  if (t == S[l] || t == S[h]) {
    return 1;
  } else if (l == h) {
    return 0;
  } else {
    int m = (l + h) / 2;
    if (t < S[m]) {
      return bin(t, S, l, m - 1);
    } else {
      return bin(t, S, m, h - 1);
    }
  }
}

int main() {
  scanf("%d", &n);
  int S[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);
    ans += bin(t, S);
  }
  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

#define MAX (1 << 9)
int n, q, t, ans = 0, S[MAX];

int bin(int t, int l = 0, int h = n - 1) {
  if (t == S[l] || t == S[h]) {
    return 1;
  } else if (l == h) {
    return 0;
  } else {
    int m = l + h / 2;
    if (t < S[m]) {
      return bin(t, l, m - 1);
    } else {
      return bin(t, m, h - 1);
    }
  }
}

int main() {
  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);
    ans += bin(t);
  }
  printf("%d\\n", ans);

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

#include <iostream>
using namespace std;

bool binarySearch(int a[], int n, int x) {
  int mid;
  int left = 0;
  int right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (a[mid] == x)
      return true;
    else if (x < a[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

int main(void) {
  int n, q, sum = 0;
  int S[100001], T[50001];

  /* input */

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

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

  for (int i = 0; i < q; i++) {
    if (binarySearch(S, n, T[i])) {
      sum++;
    }
  }

  cout << sum << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

bool binarySearch(int a[], int n, int x) {
  int mid;
  int left = 0;
  int right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (a[mid] == x)
      return true;
    else if (x < a[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

int main(void) {
  int n, q, sum = 0;
  int S[10001], T[10001];

  /* input */

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

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

  for (int i = 0; i < q; i++) {
    if (binarySearch(S, n, T[i])) {
      sum++;
    }
  }

  cout << sum << "\\n";

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;

int n, q;
ll S[100000], T[50000];

int cond(ll x, ll qq) {
  // fprintf(stderr, "%lld %lld\\n", x, qq);

  if (S[x] < qq)
    return 0;
  return 1;
}

ll binarySearch(ll MX, ll qq) {
  ll left = 0;
  ll right = MX;

  if (cond(0, qq) == 1)
    return 0;
  if (cond(MX - 1, qq) == 0)
    return MX;

  while (left + 1 < right) {
    ll mid = (left + right) / 2;
    if (cond(mid, qq) == 0) {
      left = mid;
    } else {
      right = mid;
    }
  }
  return right;
}

bool exist(ll qq) {
  // fprintf(stderr, "----- \\n");
  ll bs = binarySearch(n, qq);
  // fprintf(stderr, "----- %lld %lld\\n", qq, bs);

  if (bs == n)
    return false;
  return S[bs] == qq;
}

int main() {
  cin >> n;
  rep(i, n) { cin >> S[i]; }
  cin >> q;
  rep(i, q) { cin >> T[i]; }

  int ret = 0;
  rep(i, q) {
    if (exist(T[i]))
      ret++;
  }
  cout << ret << endl;
}

Error: 0
----- 
0 3
4 3
2 3
1 3
----- 3 2
----- 
0 4
4 4
2 4
3 4
----- 4 3
----- 
0 1
----- 1 0


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;

int n, q;
ll S[100000], T[50000];

int cond(ll x, ll qq) {
  fprintf(stderr, "%lld %lld\\n", x, qq);

  if (S[x] < qq)
    return 0;
  return 1;
}

ll binarySearch(ll MX, ll qq) {
  ll left = 0;
  ll right = MX;

  if (cond(0, qq) == 1)
    return 0;
  if (cond(MX - 1, qq) == 0)
    return MX;

  while (left + 1 < right) {
    ll mid = (left + right) / 2;
    if (cond(mid, qq) == 0) {
      left = mid;
    } else {
      right = mid;
    }
  }
  return right;
}

bool exist(ll qq) {
  fprintf(stderr, "----- \\n");
  ll bs = binarySearch(n, qq);
  fprintf(stderr, "----- %lld %lld\\n", qq, bs);

  if (bs == n)
    return false;
  return S[bs] == qq;
}

int main() {
  cin >> n;
  rep(i, n) { cin >> S[i]; }
  cin >> q;
  rep(i, q) { cin >> T[i]; }

  int ret = 0;
  rep(i, q) {
    if (exist(T[i]))
      ret++;
  }
  cout << ret << endl;
}