prithivMLmods/Coder-Stat huggingface dataset:
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define w cout
#define e '\n'
#define int long long
#define all(v) (begin(v), end(v))
#define pb push_back
using namespace std;
const int N = 1e5 + 10, MOD = 1e9 + 7;

int dd[N], temp[N];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int a[n + 1], b[n + 1];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];
  int sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum = 0;
    for (int j = m - 1; j >= 0; j--) {
      if (a[i] == b[j]) {
        dd[j] += sum + 1;
        dd[j] %= MOD;
      }
      sum += temp[j];
      sum %= MOD;
    }
    for (int j = 0; j < m; j++) {
      temp[j] = dd[j] % MOD;
    }
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    ans += dd[i];
    ans = ans % MOD;
  }
  w << (ans + 1) % MOD;
}


Fixed submission:
#include <bits/stdc++.h>
#define w cout
#define e '\n'
#define int long long
#define all(v) (begin(v), end(v))
#define pb push_back
using namespace std;
const int N = 1e5 + 10, MOD = 1e9 + 7;

int dd[N], temp[N];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int a[n + 1], b[m + 1];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];
  int sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum = 0;
    for (int j = m - 1; j >= 0; j--) {
      if (a[i] == b[j]) {
        dd[j] += sum + 1;
        dd[j] %= MOD;
      }
      sum += temp[j];
      sum %= MOD;
    }
    for (int j = 0; j < m; j++) {
      temp[j] = dd[j] % MOD;
    }
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    ans += dd[i];
    ans = ans % MOD;
  }
  w << (ans + 1) % MOD;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define forn(I, N) for (int I = 0; I < (N); ++I)
#define fornd(I, N) for (int I = (N)-1; I >= 0; --I)
#define forab(I, A, B) for (int I = (A); I <= (B); ++I)
#define forabd(I, A, B) for (int I = (B); I >= (A); --I)
#define FOREACH(I, A)                                                          \
  for (__typeof__(A)::iterator I = A.begin(); I < A.end(); I++)
#define all(V) (V).begin(), (V).end()
#define pb push_back
#define mp make_pair

typedef long long int ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const ll mod = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;

  vector<ll> S(N), T(M);

  forn(i, N) { cin >> S[i]; }

  forn(j, M) { cin >> T[j]; }

  vector<ll> res(N);
  forn(i, N) {
    ll tempSum = 0;
    forn(j, M) {
      ll buf = res[j];
      if (S[i] == T[j]) {
        res[j] = (res[j] + 1 + tempSum) % mod;
      }
      tempSum = (tempSum + buf) % mod;
    }
  }

  ll ans = 1;
  forn(i, M) { ans = (ans + res[i]) % mod; }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define forn(I, N) for (int I = 0; I < (N); ++I)
#define fornd(I, N) for (int I = (N)-1; I >= 0; --I)
#define forab(I, A, B) for (int I = (A); I <= (B); ++I)
#define forabd(I, A, B) for (int I = (B); I >= (A); --I)
#define FOREACH(I, A)                                                          \
  for (__typeof__(A)::iterator I = A.begin(); I < A.end(); I++)
#define all(V) (V).begin(), (V).end()
#define pb push_back
#define mp make_pair

typedef long long int ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const ll mod = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;

  vector<ll> S(N), T(M);

  forn(i, N) { cin >> S[i]; }

  forn(j, M) { cin >> T[j]; }

  vector<ll> res(M);
  forn(i, N) {
    ll tempSum = 0;
    forn(j, M) {
      ll buf = res[j];
      if (S[i] == T[j]) {
        res[j] = (res[j] + 1 + tempSum) % mod;
      }
      tempSum = (tempSum + buf) % mod;
    }
  }

  ll ans = 1;
  forn(i, M) { ans = (ans + res[i]) % mod; }

  cout << ans << endl;

  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>
using namespace std;
constexpr int N_max = 2'000;
constexpr int M_max = 2'000;
constexpr int mod = 1'000'000'007;

#ifdef _MSC_VER
constexpr int debug = 1;
#else
constexpr int debug = 0;
#endif

//             s-pos  t-pos
static int dp0[2'005][2'005];
static int dp1[2'005][2'005];

int main() {
  int n, m;
  cin >> n >> m;
  auto s = vector<int>(n + 5);
  auto t = vector<int>(m + 5);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int j = 0; j < m; j++)
    cin >> t[j];

  dp0[0][0] = 1;

  for (int i = 0; i <= s.size(); i++) {
    for (int j = 0; j <= t.size(); j++) {
      dp0[i + 1][j] = (dp0[i + 1][j] + dp0[i][j]) % mod;
      dp1[i][j] = (dp1[i][j] + dp0[i][j]) % mod;
      dp1[i][j + 1] = (dp1[i][j + 1] + dp1[i][j]) % mod;
      if (s[i] == t[j]) {
        dp0[i + 1][j + 1] = (dp0[i + 1][j + 1] + dp1[i][j]) % mod;
      }
    }
  }

  cout << dp1[n][m];
  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
constexpr int N_max = 2'000;
constexpr int M_max = 2'000;
constexpr int mod = 1'000'000'007;

#ifdef _MSC_VER
constexpr int debug = 1;
#else
constexpr int debug = 0;
#endif

//             s-pos  t-pos
static int dp0[2'005][2'005];
static int dp1[2'005][2'005];

int main() {
  int n, m;
  cin >> n >> m;
  auto s = vector<int>(n + 5);
  auto t = vector<int>(m + 5);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int j = 0; j < m; j++)
    cin >> t[j];

  dp0[0][0] = 1;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      dp0[i + 1][j] = (dp0[i + 1][j] + dp0[i][j]) % mod;
      dp1[i][j] = (dp1[i][j] + dp0[i][j]) % mod;
      dp1[i][j + 1] = (dp1[i][j + 1] + dp1[i][j]) % mod;
      if (s[i] == t[j]) {
        dp0[i + 1][j + 1] = (dp0[i + 1][j + 1] + dp1[i][j]) % mod;
      }
    }
  }

  cout << dp1[n][m];
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll MOD = 1e9 + 7;

ll N, M;
vector<ll> S, T;
vector<vector<ll>> Dp;

int main() {
  cin >> N >> M;
  S.resize(N);
  for (ll i = 0; i < N; ++i)
    cin >> S[i];
  T.resize(M);
  for (ll j = 0; j < M; ++j)
    cin >> T[j];
  Dp.resize(N + 1);
  for (auto &dp : Dp)
    dp.resize(M + 1);
  for (ll i = 0; i <= N; ++i)
    Dp[i][0] = 1;
  for (ll j = 0; j <= N; ++j)
    Dp[0][j] = 1;
  for (ll i = 1; i <= N; ++i) {
    for (ll j = 1; j <= M; ++j) {
      Dp[i][j] = (Dp[i - 1][j] + Dp[i][j - 1] + MOD - Dp[i - 1][j - 1]) % MOD;
      if (S[i - 1] == T[j - 1])
        (Dp[i][j] += Dp[i - 1][j - 1]) %= MOD;
    }
  }
  cout << Dp[N][M] << endl;
  return 0;
}


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

typedef long long ll;

const ll MOD = 1e9 + 7;

ll N, M;
vector<ll> S, T;
vector<vector<ll>> Dp;

int main() {
  cin >> N >> M;
  S.resize(N);
  for (ll i = 0; i < N; ++i)
    cin >> S[i];
  T.resize(M);
  for (ll j = 0; j < M; ++j)
    cin >> T[j];
  Dp.resize(N + 1);
  for (auto &dp : Dp)
    dp.resize(M + 1);
  for (ll i = 0; i <= N; ++i)
    Dp[i][0] = 1;
  for (ll j = 0; j <= M; ++j)
    Dp[0][j] = 1;
  for (ll i = 1; i <= N; ++i) {
    for (ll j = 1; j <= M; ++j) {
      Dp[i][j] = (Dp[i - 1][j] + Dp[i][j - 1] + MOD - Dp[i - 1][j - 1]) % MOD;
      if (S[i - 1] == T[j - 1])
        (Dp[i][j] += Dp[i - 1][j - 1]) %= MOD;
    }
  }
  cout << Dp[N][M] << endl;
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

ll N, M;
ll inf = 1e9 + 7;
int main() {
  cin >> N >> M;
  vector<int> S(N + 1, 0), T(N + 1, 0);
  for (int i = 1; i <= N; i++)
    cin >> S[i];
  for (int j = 1; j <= M; j++)
    cin >> T[j];
  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, 0));
  vector<vector<ll>> sum(N + 1, vector<ll>(M + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      if (i != 0 && j != 0 && S[i] == T[j])
        (dp[i][j] += sum[i - 1][j - 1]) %= inf;
      (sum[i][j] += dp[i][j]) %= inf;
      if (j != 0)
        (sum[i][j] += sum[i][j - 1]) %= inf;
    }
    if (i != 0)
      for (int j = 0; j <= M; j++)
        (sum[i][j] += sum[i - 1][j]) %= inf;
  }
  cout << sum[N][M] << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

ll N, M;
ll inf = 1e9 + 7;
int main() {
  cin >> N >> M;
  vector<int> S(N + 1, 0), T(M + 1, 0);
  for (int i = 1; i <= N; i++)
    cin >> S[i];
  for (int j = 1; j <= M; j++)
    cin >> T[j];
  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, 0));
  vector<vector<ll>> sum(N + 1, vector<ll>(M + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      if (i != 0 && j != 0 && S[i] == T[j])
        (dp[i][j] += sum[i - 1][j - 1]) %= inf;
      (sum[i][j] += dp[i][j]) %= inf;
      if (j != 0)
        (sum[i][j] += sum[i][j - 1]) %= inf;
    }
    if (i != 0)
      for (int j = 0; j <= M; j++)
        (sum[i][j] += sum[i - 1][j]) %= inf;
  }
  cout << sum[N][M] << endl;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;

ll dp[2009][2009];
ll s[2009], t[2009];

ll fun(ll i, ll j) {
  if (i < 0 or j < 0)
    return 0;
  if (i == 0 or j == 0)
    return 1;
  else if (dp[i][j] != -1)
    return dp[i][j];
  else {
    if (s[i - 1] != t[j - 1])
      return dp[i][j] =
                 (fun(i - 1, j) + fun(i, j - 1) - fun(i - 1, j - 1) + mod) %
                 mod;
    else
      return dp[i][j] = (fun(i - 1, j) + fun(i, j - 1)) % mod;
  }
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;

  ll n, m, x;
  cin >> n >> m;

  for (ll i = 0; i < n; i++)
    cin >> s[i];
  for (ll i = 0; i < m; i++)
    cin >> t[i];

  memset(dp, -1, sizeof(dp));

  cout << fun(n, m) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;

ll dp[2009][2009];
ll s[2009], t[2009];

ll fun(ll i, ll j) {
  if (i < 0 or j < 0)
    return 0;
  if (i == 0 or j == 0)
    return 1;
  else if (dp[i][j] != -1)
    return dp[i][j];
  else {
    if (s[i - 1] != t[j - 1])
      return dp[i][j] =
                 (fun(i - 1, j) + fun(i, j - 1) - fun(i - 1, j - 1) + mod) %
                 mod;
    else
      return dp[i][j] = (fun(i - 1, j) + fun(i, j - 1)) % mod;
  }
}

int main() {
  fast;

  ll n, m, x;
  cin >> n >> m;

  for (ll i = 0; i < n; i++)
    cin >> s[i];
  for (ll i = 0; i < m; i++)
    cin >> t[i];

  memset(dp, -1, sizeof(dp));

  cout << fun(n, m) << endl;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

int n, m, mod = 1000000007;
vector<int> s(1e3 + 3), t(1e3 + 3);
long long dp[1003][1003];

long long solve(int i, int j) {
  if (i == 0 || j == 0) {
    return 1;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  dp[i][j] = 0;
  if (s[i] == t[j]) {
    dp[i][j] += solve(i - 1, j - 1);
  }
  dp[i][j] += solve(i - 1, j) + solve(i, j - 1) - solve(i - 1, j - 1);
  dp[i][j] += mod;
  dp[i][j] %= mod;
  return dp[i][j];
}

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

  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> t[i];
  }
  cout << solve(n, m) << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

int n, m, mod = 1000000007;
vector<int> s(2e3 + 3), t(2e3 + 3);
long long dp[2003][2003];

long long solve(int i, int j) {
  if (i == 0 || j == 0) {
    return 1;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  dp[i][j] = 0;
  if (s[i] == t[j]) {
    dp[i][j] += solve(i - 1, j - 1);
  }
  dp[i][j] += solve(i - 1, j) + solve(i, j - 1) - solve(i - 1, j - 1);
  dp[i][j] += mod;
  dp[i][j] %= mod;
  return dp[i][j];
}

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

  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> t[i];
  }
  cout << solve(n, m) << "\n";
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int main() {
  int N, M, S[2000], T[2000];
  int dp[200][200] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  for (int i = 0; i < M; i++)
    cin >> T[i];
  dp[0][0] = S[0] == T[0];
  for (int i = 1; i < N; i++)
    dp[i][0] = dp[i - 1][0] + (S[i] == T[0]);
  for (int i = 1; i < M; i++)
    dp[0][i] = dp[0][i - 1] + (S[0] == T[i]);
  for (int i = 1; i < N; i++)
    dp[i][0] = dp[i - 1][0] + (S[i] == T[0]);
  for (int i = 1; i < M; i++)
    dp[0][i] = dp[0][i - 1] + (S[0] == T[i]);
  for (int i = 1; i < N; i++)
    for (int j = 1; j < M; j++) {
      if (S[i] == T[j])
        dp[i][j] = dp[i][j - 1] + dp[i - 1][j] + 1;
      else
        dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
      dp[i][j] = (dp[i][j] % MOD + MOD) % MOD;
    }
  cout << dp[N - 1][M - 1] + 1;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int main() {
  int N, M, S[2000], T[2000];
  int dp[2000][2000] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  for (int i = 0; i < M; i++)
    cin >> T[i];
  dp[0][0] = S[0] == T[0];
  for (int i = 1; i < N; i++)
    dp[i][0] = dp[i - 1][0] + (S[i] == T[0]);
  for (int i = 1; i < M; i++)
    dp[0][i] = dp[0][i - 1] + (S[0] == T[i]);
  for (int i = 1; i < N; i++)
    dp[i][0] = dp[i - 1][0] + (S[i] == T[0]);
  for (int i = 1; i < M; i++)
    dp[0][i] = dp[0][i - 1] + (S[0] == T[i]);
  for (int i = 1; i < N; i++)
    for (int j = 1; j < M; j++) {
      if (S[i] == T[j])
        dp[i][j] = dp[i][j - 1] + dp[i - 1][j] + 1;
      else
        dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
      dp[i][j] = (dp[i][j] % MOD + MOD) % MOD;
    }
  cout << dp[N - 1][M - 1] + 1;
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> S(n), T(m);
  rep(i, n) cin >> S[i];
  map<int, vector<int>> mp;
  rep(i, m) {
    cin >> T[i];
    mp[T[i]].push_back(i);
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1));
  dp[0][0] = 1;
  rep(i, n) {
    rep(j, m + 1) {
      (dp[i + 1][j] += dp[i][j]) %= mod;
      auto itr = lower_bound(all(mp[S[i]]), j);
      for (auto k = itr; k != mp[S[i]].end(); k++) {
        (dp[i + 1][*k + 1] += dp[i][j]) %= mod;
      }
    }
  }
  ll ans = 0;
  rep(i, m + 1)(ans += dp[n][i]) %= mod;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> S(n), T(m);
  rep(i, n) cin >> S[i];
  map<int, vector<int>> mp;
  rep(i, m) {
    cin >> T[i];
    mp[T[i]].push_back(i);
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1));
  dp[0][0] = 1;
  rep(i, n) {
    rep(j, m + 1) {
      (dp[i + 1][j] += dp[i][j]) %= mod;
      auto itr = lower_bound(all(mp[S[i]]), j);
      if (itr != mp[S[i]].end())
        (dp[i + 1][*itr + 1] += dp[i][j]) %= mod;
    }
    for (auto &ele : mp) {
      rep(j, ele.second.size()) {
        if (j)
          (dp[i + 1][ele.second[j] + 1] +=
           (mod + dp[i + 1][ele.second[j - 1] + 1] -
            dp[i][ele.second[j - 1] + 1]) %
           mod) %= mod;
      }
    }
  }
  ll ans = 0;
  rep(i, m + 1)(ans += dp[n][i]) %= mod;
  cout << ans << "\n";
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;

int N, M, S[200], T[200], DP[201][201], s[201][201];

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    cin >> S[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> T[i];
  }
  for (int i = 0; i <= M; ++i) {
    s[0][i] = 1;
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) {
      s[i][j] = s[i][j - 1];
      if (S[i - 1] == T[j - 1]) {
        DP[i][j] = s[i - 1][j - 1];
        s[i][j] += DP[i][j];
        s[i][j] %= mod;
      }
    }
    for (int j = 0; j <= M; ++j) {
      s[i][j] += s[i - 1][j];
      s[i][j] %= mod;
    }
  }
  cout << s[N][M] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;

int N, M, S[2000], T[2000], DP[2001][2001], s[2001][2001];

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    cin >> S[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> T[i];
  }
  for (int i = 0; i <= M; ++i) {
    s[0][i] = 1;
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) {
      s[i][j] = s[i][j - 1];
      if (S[i - 1] == T[j - 1]) {
        DP[i][j] = s[i - 1][j - 1];
        s[i][j] += DP[i][j];
        s[i][j] %= mod;
      }
    }
    for (int j = 0; j <= M; ++j) {
      s[i][j] += s[i - 1][j];
      s[i][j] %= mod;
    }
  }
  cout << s[N][M] << endl;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  int S[N];
  int T[M];
  for (int i = 0; i < N; i++)
    cin >> S[i];
  for (int i = 0; i < M; i++)
    cin >> T[i];
  ll DP[N + 1][M + 1];
  vector<ll> sum(N + 1);

  ll ans = 1;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      DP[i][j] = 0;
    }
  }
  for (int i = 0; i <= M; i++) {
    sum[i] = 1;
  }
  DP[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    vector<ll> newsum(M + 1);
    for (int j = 0; j <= M; j++) {
      newsum[j] = 0;
    }
    for (int j = 1; j <= M; j++) {
      if (S[i - 1] == T[j - 1]) {
        ll r = sum[j - 1];
        r %= Mod;
        DP[i][j] = r;
      }
      newsum[j] = newsum[j - 1] + DP[i][j];
      newsum[j] %= Mod;
      ans += DP[i][j];
      ans %= Mod;
    }
    for (int j = 0; j <= M; j++) {
      sum[j] += newsum[j];
      sum[j] %= Mod;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  int S[N];
  int T[M];
  for (int i = 0; i < N; i++)
    cin >> S[i];
  for (int i = 0; i < M; i++)
    cin >> T[i];
  ll DP[N + 1][M + 1];
  vector<ll> sum(M + 1);

  ll ans = 1;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      DP[i][j] = 0;
    }
  }
  for (int i = 0; i <= M; i++) {
    sum[i] = 1;
  }
  DP[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    vector<ll> newsum(M + 1);
    for (int j = 0; j <= M; j++) {
      newsum[j] = 0;
    }
    for (int j = 1; j <= M; j++) {
      if (S[i - 1] == T[j - 1]) {
        ll r = sum[j - 1];
        r %= Mod;
        DP[i][j] = r;
      }
      newsum[j] = newsum[j - 1] + DP[i][j];
      newsum[j] %= Mod;
      ans += DP[i][j];
      ans %= Mod;
    }
    for (int j = 0; j <= M; j++) {
      sum[j] += newsum[j];
      sum[j] %= Mod;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define lowbit(x) ((x) & -(x))

const int maxn = (int)1e5 + 20;
const int MOD = 1e9 + 7;

LL fp(LL a, LL n, LL m = MOD) {
  LL res = 1;
  for (; n; n >>= 1, a = a * a % m)
    if (n & 1)
      res = res * a % m;
  return res;
}

int n, m;
int s[1111], t[1111];
int dp[1111][1111];

void work() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int j = 1; j <= m; j++)
    cin >> t[j];

  for (int i = 0; i <= n; i++)
    dp[i][0] = 1;
  for (int j = 0; j <= m; j++)
    dp[0][j] = 1;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
      dp[i][j] = (dp[i][j] % MOD + MOD) % MOD;

      if (s[i] == t[j])
        dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] = (dp[i][j] % MOD + MOD) % MOD;
    }
  cout << dp[n][m] << endl;
}

int main() {
#ifdef yukihana0416
  freopen("in.txt", "r", stdin);
#endif // yukihana0416
  work();
  return 0;
}


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

#define LL long long
#define lowbit(x) ((x) & -(x))

const int maxn = (int)1e5 + 20;
const int MOD = 1e9 + 7;

LL fp(LL a, LL n, LL m = MOD) {
  LL res = 1;
  for (; n; n >>= 1, a = a * a % m)
    if (n & 1)
      res = res * a % m;
  return res;
}

int n, m;
int s[3333], t[3333];
int dp[3333][3333];

void work() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int j = 1; j <= m; j++)
    cin >> t[j];

  for (int i = 0; i <= n; i++)
    dp[i][0] = 1;
  for (int j = 0; j <= m; j++)
    dp[0][j] = 1;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
      dp[i][j] = (dp[i][j] % MOD + MOD) % MOD;

      if (s[i] == t[j])
        dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] = (dp[i][j] % MOD + MOD) % MOD;
    }
  cout << dp[n][m] << endl;
}

int main() {
#ifdef yukihana0416
  freopen("in.txt", "r", stdin);
#endif // yukihana0416
  work();
  return 0;
}
Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IFOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define RFOR(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR((i), 0, (n))

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> void print_container(T &container, string delim = " ") {
  bool first = true;
  for (auto &t : container) {
    if (!first) {
      cout << delim;
    }
    first = false;
    cout << t;
  }
  cout << endl;
}

const int MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> s(n), t(m);
  REP(i, n) { cin >> s[i]; }
  REP(i, m) { cin >> t[i]; }

  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
  IFOR(i, 0, n) { dp[i][0] = dp[0][i] = 1; }

  IFOR(i, 1, n) {
    map<int, ll> accum;
    IFOR(j, 1, m) {
      dp[i][j] = dp[i - 1][j];
      accum[t[j - 1]] = (accum[t[j - 1]] + dp[i - 1][j - 1]) % MOD;
      dp[i][j] = (dp[i][j] + accum[s[i - 1]]) % MOD;
    }
  }

  cout << dp[n][m] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IFOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define RFOR(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR((i), 0, (n))

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> void print_container(T &container, string delim = " ") {
  bool first = true;
  for (auto &t : container) {
    if (!first) {
      cout << delim;
    }
    first = false;
    cout << t;
  }
  cout << endl;
}

const int MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> s(n), t(m);
  REP(i, n) { cin >> s[i]; }
  REP(i, m) { cin >> t[i]; }

  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
  IFOR(i, 0, n) { dp[i][0] = 1; }
  IFOR(i, 0, m) { dp[0][i] = 1; }

  IFOR(i, 1, n) {
    map<int, ll> accum;
    IFOR(j, 1, m) {
      dp[i][j] = dp[i - 1][j];
      accum[t[j - 1]] = (accum[t[j - 1]] + dp[i - 1][j - 1]) % MOD;
      dp[i][j] = (dp[i][j] + accum[s[i - 1]]) % MOD;
    }
  }

  cout << dp[n][m] << endl;
  return 0;
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3", "unroll-loops")
#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 10;
pair<double, double> a[maxn];
double dx[maxn], dy[maxn], ans, lb = 0, lbb, rbb, rb = 2e8;

int n;

char c;

double calc(double t) {
  double u, d, l, r;
  l = r = a[0].first + t * dx[0], u = d = a[0].second + t * dy[0];
  for (int i = 1; i < n; i++) {
    l = min(l, a[i].first + t * dx[i]);
    r = max(r, a[i].first + t * dx[i]);
    u = max(u, a[i].second + t * dy[i]);
    d = min(d, a[i].second + t * dy[i]);
  }
  return (u - d) * (r - l);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second >> c;
    if (c == 'U')
      dx[i] = 0, dy[i] = 1;
    else if (c == 'D')
      dx[i] = 0, dy[i] = -1;
    else if (c == 'R')
      dx[i] = 1, dy[i] = 0;
    else
      dx[i] = -1, dy[i] = 0;
  }
  ans = calc(0);
  while (rb - lb > 1e-10) {
    lbb = (2 * lb + rb) / 3;
    rbb = (lb + 2 * rb) / 3;
    if (calc(lbb) < calc(rbb))
      rb = rbb;
    else
      lb = lbb;
  }
  cout << setprecision(10) << fixed << calc(lb) << '\n';
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3", "unroll-loops")
#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 10;
pair<double, double> a[maxn];
double dx[maxn], dy[maxn], ans, lb = 0, lbb, rbb, rb = 2e8;

int n;

char c;

double calc(double t) {
  double u, d, l, r;
  l = r = a[0].first + t * dx[0], u = d = a[0].second + t * dy[0];
  for (int i = 1; i < n; i++) {
    l = min(l, a[i].first + t * dx[i]);
    r = max(r, a[i].first + t * dx[i]);
    u = max(u, a[i].second + t * dy[i]);
    d = min(d, a[i].second + t * dy[i]);
  }
  return (u - d) * (r - l);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second >> c;
    if (c == 'U')
      dx[i] = 0, dy[i] = 1;
    else if (c == 'D')
      dx[i] = 0, dy[i] = -1;
    else if (c == 'R')
      dx[i] = 1, dy[i] = 0;
    else
      dx[i] = -1, dy[i] = 0;
  }
  ans = calc(0);
  for (int i = 0; i < 1000; i++) {
    lbb = (2 * lb + rb) / 3;
    rbb = (lb + 2 * rb) / 3;
    if (calc(lbb) < calc(rbb))
      rb = rbb;
    else
      lb = lbb;
  }
  cout << setprecision(10) << fixed << calc(lb) << '\n';
  return 0;
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

Int n;
typedef pair<Int, Int> P;
vector<P> x, y;
constexpr double EPS = 1e-14;

double func(double t) {
  double xmi = 1e10, ymi = 1e10, xma = -1e10, yma = -1e10;
  for (Int i = 0; i < n; ++i) {
    xmi = min(xmi, x[i].first + t * x[i].second);
    xma = max(xma, x[i].first + t * x[i].second);
    ymi = min(ymi, y[i].first + t * y[i].second);
    yma = max(yma, y[i].first + t * y[i].second);
  }
  return (xma - xmi) * (yma - ymi);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (Int i = 0; i < n; ++i) {
    int tx, ty;
    char c;
    cin >> tx >> ty >> c;
    if (c == 'R' || c == 'L') {
      y.push_back(P(ty, 0));
      x.push_back(P(tx, c == 'R' ? 1 : -1));
    } else {
      x.push_back(P(tx, 0));
      y.push_back(P(ty, c == 'U' ? 1 : -1));
    }
  }
  double tl = 0.0, tr = 1e10;
  while (tl + EPS < tr) {
    double t1 = (2 * tl + tr) / 3;
    double t2 = (tl + 2 * tr) / 3;
    double f = func(t1), g = func(t2);
    if (f < g) {
      tr = t2;
    } else {
      tl = t1;
    }
  }
  cout << fixed << setprecision(14) << func(tl) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

Int n;
typedef pair<Int, Int> P;
vector<P> x, y;
constexpr double EPS = 1e-14;

double func(double t) {
  double xmi = 1e10, ymi = 1e10, xma = -1e10, yma = -1e10;
  for (Int i = 0; i < n; ++i) {
    xmi = min(xmi, x[i].first + t * x[i].second);
    xma = max(xma, x[i].first + t * x[i].second);
    ymi = min(ymi, y[i].first + t * y[i].second);
    yma = max(yma, y[i].first + t * y[i].second);
  }
  return (xma - xmi) * (yma - ymi);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (Int i = 0; i < n; ++i) {
    int tx, ty;
    char c;
    cin >> tx >> ty >> c;
    if (c == 'R' || c == 'L') {
      y.push_back(P(ty, 0));
      x.push_back(P(tx, c == 'R' ? 1 : -1));
    } else {
      x.push_back(P(tx, 0));
      y.push_back(P(ty, c == 'U' ? 1 : -1));
    }
  }
  double tl = 0.0, tr = 1e10;
  for (int i = 0; i < 150; ++i) {
    double t1 = (2 * tl + tr) / 3;
    double t2 = (tl + 2 * tr) / 3;
    double f = func(t1), g = func(t2);
    if (f < g) {
      tr = t2;
    } else {
      tl = t1;
    }
  }
  cout << fixed << setprecision(14) << func(tl) << "\n";
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
const double inf = 1e18;
double x[N], y[N];
double dx[N], dy[N];
int n;

double f(double a) {
  double minx = inf, maxx = -inf, miny = inf, maxy = -inf;
  for (int i = 0; i < n; i++) { // n个点
    double nx = x[i] + a * dx[i];
    double ny = y[i] + a * dy[i];
    minx = min(minx, nx);
    maxx = max(maxx, nx);
    miny = min(miny, ny);
    maxy = max(maxy, ny);
  }
  return (maxx - minx) * (maxy - miny);
}

double ternary_search(double l, double r) {
  double ans = inf;
  while (r - l > 1e-11) {
    double m1 = l + (r - l) / 3;
    double m2 = r - (r - l) / 3;
    // double m1 = (l*2+r) / 3;
    // double m2 = (r*2+l) / 3;
    double f1 = f(m1);
    double f2 = f(m2);
    ans = min(ans, f1);
    ans = min(ans, f2);
    if (f1 < f2)
      r = m2;
    else
      l = m1;
  }
  return ans;
}

int main() {
  // while (cin >> n) {
  cin >> n;
  // memset(dx, 0, sizeof(dx));
  // memset(dy, 0, sizeof(dy));

  string s;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> s;
    if (s == "R")
      dx[i] = 1;
    if (s == "L")
      dx[i] = -1;
    if (s == "U")
      dy[i] = 1;
    if (s == "D")
      dy[i] = -1;
  }

  // cout << ternary_search(0, 1e9) << endl;
  printf("%lf\n", ternary_search(0, 1e9));
  // }

  return 0;
}

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

const int N = 1e5 + 5;
const double inf = 1e18;
double x[N], y[N];
double dx[N], dy[N];
int n;

double f(double a) {
  double minx = inf, maxx = -inf, miny = inf, maxy = -inf;
  for (int i = 0; i < n; i++) { // n个点
    double nx = x[i] + a * dx[i];
    double ny = y[i] + a * dy[i];
    minx = min(minx, nx);
    maxx = max(maxx, nx);
    miny = min(miny, ny);
    maxy = max(maxy, ny);
  }
  return (maxx - minx) * (maxy - miny);
}

double ternary_search(double l, double r) {
  double ans = inf;
  int times = 200;
  while (times--) {
    double m1 = l + (r - l) / 3;
    double m2 = r - (r - l) / 3;
    // double m1 = (l*2+r) / 3;
    // double m2 = (r*2+l) / 3;
    double f1 = f(m1);
    double f2 = f(m2);
    ans = min(ans, f1);
    ans = min(ans, f2);
    if (f1 < f2)
      r = m2;
    else
      l = m1;
  }
  return ans;
}

int main() {
  // while (cin >> n) {
  cin >> n;
  // memset(dx, 0, sizeof(dx));
  // memset(dy, 0, sizeof(dy));

  string s;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> s;
    if (s == "R")
      dx[i] = 1;
    if (s == "L")
      dx[i] = -1;
    if (s == "U")
      dy[i] = 1;
    if (s == "D")
      dy[i] = -1;
  }

  // cout << ternary_search(0, 1e9) << endl;
  printf("%lf\n", ternary_search(0, 1e9));
  // }

  return 0;
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct Point {
  double x, y;
  int dx, dy;
};
double simulate(vector<Point> points, double t) {
  double maxX = -1e18, maxY = -1e18, minX = 1e18, minY = 1e18;
  for (int i = 0; i < points.size(); ++i) {
    points[i].x += points[i].dx * t;
    points[i].y += points[i].dy * t;
    maxX = max(maxX, points[i].x);
    maxY = max(maxY, points[i].y);
    minX = min(minX, points[i].x);
    minY = min(minY, points[i].y);
  }
  return (maxY - minY) * (maxX - minX);
}
int main() {
  int n;
  cin >> n;
  vector<Point> points(n);
  for (int i = 0; i < n; ++i) {
    cin >> points[i].x >> points[i].y;
    char ch;
    cin >> ch;
    if (ch == 'U') {
      points[i].dx = 0, points[i].dy = 1;
    } else if (ch == 'D') {
      points[i].dx = 0, points[i].dy = -1;
    } else if (ch == 'L') {
      points[i].dx = -1, points[i].dy = 0;
    } else if (ch == 'R') {
      points[i].dx = 1, points[i].dy = 0;
    }
  }
  double l = 0, r = 1e8;
  while (l + 1e-9 < r) {
    double m1 = l + (r - l) / 3;
    double m2 = r - (r - l) / 3;
    if (-simulate(points, m1) > -simulate(points, m2))
      r = m2;
    else
      l = m1;
  }
  printf("%.9f\n", simulate(points, l));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct Point {
  double x, y;
  int dx, dy;
};
double simulate(vector<Point> points, double t) {
  double maxX = -1e18, maxY = -1e18, minX = 1e18, minY = 1e18;
  for (int i = 0; i < points.size(); ++i) {
    points[i].x += points[i].dx * t;
    points[i].y += points[i].dy * t;
    maxX = max(maxX, points[i].x);
    maxY = max(maxY, points[i].y);
    minX = min(minX, points[i].x);
    minY = min(minY, points[i].y);
  }
  return (maxY - minY) * (maxX - minX);
}
int main() {
  int n;
  cin >> n;
  vector<Point> points(n);
  for (int i = 0; i < n; ++i) {
    cin >> points[i].x >> points[i].y;
    char ch;
    cin >> ch;
    if (ch == 'U') {
      points[i].dx = 0, points[i].dy = 1;
    } else if (ch == 'D') {
      points[i].dx = 0, points[i].dy = -1;
    } else if (ch == 'L') {
      points[i].dx = -1, points[i].dy = 0;
    } else if (ch == 'R') {
      points[i].dx = 1, points[i].dy = 0;
    }
  }
  double l = 0, r = 1e8;
  while (l + 1e-8 < r) {
    double m1 = l + (r - l) / 3;
    double m2 = r - (r - l) / 3;
    if (-simulate(points, m1) > -simulate(points, m2))
      r = m2;
    else
      l = m1;
  }
  printf("%.9f\n", simulate(points, l));
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, N = 1e5 + 5;
int mul(int a, int b) { return (long long)a * b; }
typedef long long ll;
int n, x[N], y[N], d[N];
const double inf = 1e20;
double getans(double mid) {
  double mnx = inf, mxx = -inf, mny = inf, mxy = -inf;
  for (int i = 1; i <= n; i++) {
    double xx = x[i], yy = y[i];
    if (d[i] == 1)
      xx -= mid;
    if (d[i] == 2)
      xx += mid;
    if (d[i] == 3)
      yy += mid;
    if (d[i] == 4)
      yy -= mid;
    mnx = min(mnx, xx);
    mxx = max(mxx, xx);
    mny = min(mny, yy);
    mxy = max(mxy, yy);
  }
  return (mxx - mnx) * (mxy - mny);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    char s[5];
    scanf("%d%d%s", &x[i], &y[i], s);
    if (s[0] == 'L')
      d[i] = 1;
    else if (s[0] == 'R')
      d[i] = 2;
    else if (s[0] == 'U')
      d[i] = 3;
    else
      d[i] = 4;
  }
  double l = 0, r = 1e10;
  while (r - l > 1e-10) {
    double mid = (l + r) / 2, midmid = (mid + r) / 2;
    if (getans(mid) < getans(midmid))
      r = midmid;
    else
      l = mid;
  }
  printf("%f\n", getans(l));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, N = 1e5 + 5;
int mul(int a, int b) { return (long long)a * b; }
typedef long long ll;
int n, x[N], y[N], d[N];
const double inf = 1e20;
double getans(double mid) {
  double mnx = inf, mxx = -inf, mny = inf, mxy = -inf;
  for (int i = 1; i <= n; i++) {
    double xx = x[i], yy = y[i];
    if (d[i] == 1)
      xx -= mid;
    if (d[i] == 2)
      xx += mid;
    if (d[i] == 3)
      yy += mid;
    if (d[i] == 4)
      yy -= mid;
    mnx = min(mnx, xx);
    mxx = max(mxx, xx);
    mny = min(mny, yy);
    mxy = max(mxy, yy);
  }
  return (mxx - mnx) * (mxy - mny);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    char s[5];
    scanf("%d%d%s", &x[i], &y[i], s);
    if (s[0] == 'L')
      d[i] = 1;
    else if (s[0] == 'R')
      d[i] = 2;
    else if (s[0] == 'U')
      d[i] = 3;
    else
      d[i] = 4;
  }
  double l = 0, r = 1e10;
  for (int i = 1; i <= 1000; i++) {
    double mid = l + (r - l) / 3, midmid = l + (r - l) / 3 * 2;
    if (getans(mid) < getans(midmid))
      r = midmid;
    else
      l = mid;
  }
  printf("%f\n", getans(l));
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n;
double x[100010], y[100010];
char d[100010];

double f(double t) {
  vector<double> tx(n), ty(n);
  rep(i, n) {
    tx[i] = x[i];
    ty[i] = y[i];
    if (d[i] == 'L')
      tx[i] -= t;
    if (d[i] == 'R')
      tx[i] += t;
    if (d[i] == 'U')
      ty[i] += t;
    if (d[i] == 'D')
      ty[i] -= t;
  }
  sort(tx.begin(), tx.end());
  sort(ty.begin(), ty.end());
  return (tx[n - 1] - tx[0]) * (ty[n - 1] - ty[0]);
}

int main() {
  scanf("%d", &n);
  rep(i, n) cin >> x[i] >> y[i] >> d[i];
  double high = 1e9;
  double low = 0;
  double m2 = high;
  double m1 = low;
  rep(i, 300) {
    m1 = (high + low * 2) / 3;
    m2 = (high * 2 + low) / 3;
    if (f(m1) > f(m2))
      low = m1;
    else
      high = m2;
  }
  printf("%.10f", min({f(low), f(m1), f(m2), f(high)}));
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n;
double x[100010], y[100010];
char d[100010];

double f(double t) {
  vector<double> tx(n), ty(n);
  rep(i, n) {
    tx[i] = x[i];
    ty[i] = y[i];
    if (d[i] == 'L')
      tx[i] -= t;
    if (d[i] == 'R')
      tx[i] += t;
    if (d[i] == 'U')
      ty[i] += t;
    if (d[i] == 'D')
      ty[i] -= t;
  }
  sort(tx.begin(), tx.end());
  sort(ty.begin(), ty.end());
  return (tx[n - 1] - tx[0]) * (ty[n - 1] - ty[0]);
}

int main() {
  scanf("%d", &n);
  rep(i, n) cin >> x[i] >> y[i] >> d[i];
  double high = 1e9;
  double low = 0;
  double m2 = high;
  double m1 = low;
  rep(i, 100) {
    m1 = (high + low * 2) / 3;
    m2 = (high * 2 + low) / 3;
    if (f(m1) > f(m2))
      low = m1;
    else
      high = m2;
  }
  printf("%.10f", min({f(low), f(m1), f(m2), f(high)}));
}
Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
typedef long double llf;
const int MN = 1e5 + 4;
const llf INF = 1e100, EPS = 7e-12, GR = (1 + sqrt(5)) / 2.0;
int N;
llf ans = INF;
struct pnt {
  llf x, y;
  llf ox, oy;
  char c;
} pnts[MN];
static inline void mov(pnt &p, llf dt) {
  switch (p.c) {
  case 'R':
    p.x = p.ox + dt;
    p.y = p.oy;
    break;
  case 'L':
    p.x = p.ox - dt;
    p.y = p.oy;
    break;
  case 'U':
    p.x = p.ox;
    p.y = p.oy + dt;
    break;
  case 'D':
    p.x = p.ox;
    p.y = p.oy - dt;
    break;
  }
}
static inline llf bb() {
  llf mxx = -INF, mxy = -INF, mnx = INF, mny = INF;
  for (int i = 0; i < N; ++i) {
    mxx = std::max(mxx, pnts[i].x);
    mnx = std::min(mnx, pnts[i].x);
    mxy = std::max(mxy, pnts[i].y);
    mny = std::min(mny, pnts[i].y);
  }
  return (mxx - mnx) * (mxy - mny);
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%Lf %Lf %c", &pnts[i].ox, &pnts[i].oy, &pnts[i].c);
  }
  // do GSS
  llf lo = 0, hi = 1e9, mi1, mi2;
  while (fabs(hi - lo) > EPS) {
    mi1 = hi - (hi - lo) / GR;
    mi2 = lo + (hi - lo) / GR;
    llf b1, b2;
    for (int i = 0; i < N; ++i)
      mov(pnts[i], mi1);
    b1 = bb();
    for (int i = 0; i < N; ++i)
      mov(pnts[i], mi2);
    b2 = bb();
    if (b1 < b2)
      hi = mi2;
    else
      lo = mi1;
    ans = std::min(std::min(b1, b2), ans);
  }
  printf("%.9Lf\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
typedef long double llf;
const int MN = 1e5 + 4;
const llf INF = 1e100, EPS = 7e-12, GR = (1 + sqrt(5)) / 2.0;
int N;
llf ans = INF;
struct pnt {
  llf x, y;
  llf ox, oy;
  char c;
} pnts[MN];
static inline void mov(pnt &p, llf dt) {
  switch (p.c) {
  case 'R':
    p.x = p.ox + dt;
    p.y = p.oy;
    break;
  case 'L':
    p.x = p.ox - dt;
    p.y = p.oy;
    break;
  case 'U':
    p.x = p.ox;
    p.y = p.oy + dt;
    break;
  case 'D':
    p.x = p.ox;
    p.y = p.oy - dt;
    break;
  }
}
static inline llf bb() {
  llf mxx = -INF, mxy = -INF, mnx = INF, mny = INF;
  for (int i = 0; i < N; ++i) {
    mxx = std::max(mxx, pnts[i].x);
    mnx = std::min(mnx, pnts[i].x);
    mxy = std::max(mxy, pnts[i].y);
    mny = std::min(mny, pnts[i].y);
  }
  return (mxx - mnx) * (mxy - mny);
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%Lf %Lf %c", &pnts[i].ox, &pnts[i].oy, &pnts[i].c);
  }
  // do GSS
  llf lo = 0, hi = 1e9, mi1, mi2;
  for (int i = 0; i < 500; ++i) {
    mi1 = hi - (hi - lo) / GR;
    mi2 = lo + (hi - lo) / GR;
    llf b1, b2;
    for (int i = 0; i < N; ++i)
      mov(pnts[i], mi1);
    b1 = bb();
    for (int i = 0; i < N; ++i)
      mov(pnts[i], mi2);
    b2 = bb();
    if (b1 < b2)
      hi = mi2;
    else
      lo = mi1;
    ans = std::min(std::min(b1, b2), ans);
  }
  printf("%.9Lf\n", ans);
}
Problem p03005 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.00000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti multiset<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST
#ifndef ONLINE_JUDGE
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  n -= k;
  if (k == 1)
    cout << 0;
  else
    cout << n;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.00000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti multiset<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST ll n, k;
  cin >> n >> k;
  n -= k;
  if (k == 1)
    cout << 0;
  else
    cout << n;
}
Problem p03005 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int N, K;

  cin >> N >> K;

  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }

  return N - K;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N, K;

  cin >> N >> K;

  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }

  cout << N - K << endl;
}
Problem p03005 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define int long long
#define ends ' '
#define endl '\n'
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;

  int n, k;
  cin >> n >> k;

  if (k != 1)
    cout << n - k;
  else
    cout << 0;

  return 0;
}

Fixed submission:
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define int long long
#define ends ' '
#define endl '\n'
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>

int32_t main() {
  IOS;

  int n, k;
  cin >> n >> k;

  if (k != 1)
    cout << n - k;
  else
    cout << 0;

  return 0;
}