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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <utility> // pair
#include <vector>
#define MAX_N 100001
#define MOD 1000000007
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define RER(i, n) for (ll i = n - 1; i >= 0; i--)
#define ALL(v) v.begin() v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
string s;
bool d[MAX_N];

signed main() {
  cin >> n >> s;
  d[0] = 1;
  for (ll i = 1; i < 2 * n; i++) {
    if (s[i] == s[i - 1])
      d[i] = 1 - d[i - 1];
    else
      d[i] = d[i - 1];
  }
  // for(ll i=0; i<2*n; i++){
  // 	if(d[i]) cout << 'L';
  // 	else cout << 'R';
  // }
  ll ret = 1;
  ll num_l = 1;
  for (ll i = 1; i < 2 * n; i++) {
    if (d[i])
      num_l++;
    else {
      ret *= num_l--;
      ret %= MOD;
    }
  }
  if (num_l != 0)
    ret = 0;
  if (s[0] == 'W')
    ret = 0;
  // cout << ret << endl;
  ret %= MOD;
  for (ll i = 1; i <= n; i++) {
    ret *= i;
    ret %= MOD;
  }
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <utility> // pair
#include <vector>
#define MAX_N 100001
#define MOD 1000000007
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define RER(i, n) for (ll i = n - 1; i >= 0; i--)
#define ALL(v) v.begin() v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
string s;
bool d[2 * MAX_N];

signed main() {
  cin >> n >> s;
  d[0] = 1;
  for (ll i = 1; i < 2 * n; i++) {
    if (s[i] == s[i - 1])
      d[i] = 1 - d[i - 1];
    else
      d[i] = d[i - 1];
  }
  // for(ll i=0; i<2*n; i++){
  // 	if(d[i]) cout << 'L';
  // 	else cout << 'R';
  // }
  ll ret = 1;
  ll num_l = 1;
  for (ll i = 1; i < 2 * n; i++) {
    if (d[i])
      num_l++;
    else {
      ret *= num_l--;
      ret %= MOD;
    }
  }
  if (num_l != 0)
    ret = 0;
  if (s[0] == 'W')
    ret = 0;
  // cout << ret << endl;
  ret %= MOD;
  for (ll i = 1; i <= n; i++) {
    ret *= i;
    ret %= MOD;
  }
  cout << ret << endl;
  return 0;
}
Problem p02929 (C++)

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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, b, a) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define hash dhsjakhd
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2() {
  return (ll(DD(eng)) * (1ll << 31) + ll(DD(eng))) & ((1ll << 62ll) - 1);
}

ll n, m, T, k, q;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;

const int MAXN = 100001;

string s;

ll A[MAXN] = {0};

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

  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);
  ll a, b, c, d;

  cin >> n;
  cin >> s;

  ll ans = 1;
  rep(c1, 0, n) {
    ans *= ll(c1 + 1);
    ans %= big;
  }

  ll parity = 0;
  per(c1, 2 * n, 0) {
    a = 0;
    if (s[c1] == 'B')
      a = 1;
    a ^= parity;
    A[c1] = a;
    parity ^= 1;
  }

  ll mi = 0;

  rep(c1, 0, 2 * n) {
    // cerr << A[c1];
    if (A[c1] == 0) {
      mi++;
    } else {
      ans *= mi;
      ans %= big;
      mi--;
    }
  } // cerr << "\n";

  if (mi > 0)
    ans = 0;
  cout << ans << "\n";

  return 0;
}


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

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, b, a) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define hash dhsjakhd
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2() {
  return (ll(DD(eng)) * (1ll << 31) + ll(DD(eng))) & ((1ll << 62ll) - 1);
}

ll n, m, T, k, q;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;

const int MAXN = 200001;

string s;

ll A[MAXN] = {0};

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

  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);
  ll a, b, c, d;

  cin >> n;
  cin >> s;

  ll ans = 1;
  rep(c1, 0, n) {
    ans *= ll(c1 + 1);
    ans %= big;
  }

  ll parity = 0;
  per(c1, 2 * n, 0) {
    a = 0;
    if (s[c1] == 'B')
      a = 1;
    a ^= parity;
    A[c1] = a;
    parity ^= 1;
  }

  ll mi = 0;

  rep(c1, 0, 2 * n) {
    // cerr << A[c1];
    if (A[c1] == 0) {
      mi++;
    } else {
      ans *= mi;
      ans %= big;
      mi--;
    }
  } // cerr << "\n";

  if (mi > 0)
    ans = 0;
  cout << ans << "\n";

  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define rep(i, n) for (long i = 0; i < (n); i++)

using namespace std;

void solve() {
  long N;
  cin >> N;
  string S;
  cin >> S;
  // true: begin, false: end;
  vector<bool> ops(N);
  long state = 0;
  rep(i, N * 2) {
    // cout << "i: " << i << ", state: " << state << endl;
    if (state % 2 == 0 and S[i] == 'B') {
      state++;
      ops[i] = true;
      continue;
    }
    if (state % 2 == 0 and S[i] == 'W') {
      ops[i] = false;
      state--;
      if (state < 0) {
        cout << 0 << endl;
        return;
      }
      continue;
    }
    if (state % 2 != 0 and S[i] == 'B') {
      ops[i] = false;
      state--;
      continue;
    }
    if (state % 2 != 0 and S[i] == 'W') {
      state++;
      ops[i] = true;
      continue;
    }
  }
  if (state != 0) {
    cout << 0 << endl;
    return;
  }
  long result = 1;
  state = 0;
  rep(i, N * 2) {
    if (ops[i])
      state++;
    else {
      result *= state;
      result %= MOD;
      state--;
    }
  }
  // cout << "tmp: " << result << endl;
  rep(i, N) {
    result *= i + 1;
    result %= MOD;
  }
  cout << result << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define rep(i, n) for (long i = 0; i < (n); i++)

using namespace std;

void solve() {
  long N;
  cin >> N;
  string S;
  cin >> S;
  // true: begin, false: end;
  vector<bool> ops(N * 2);
  long state = 0;
  rep(i, N * 2) {
    // cout << "i: " << i << ", state: " << state << endl;
    if (state % 2 == 0 and S[i] == 'B') {
      state++;
      ops[i] = true;
      continue;
    }
    if (state % 2 == 0 and S[i] == 'W') {
      ops[i] = false;
      state--;
      if (state < 0) {
        cout << 0 << endl;
        return;
      }
      continue;
    }
    if (state % 2 != 0 and S[i] == 'B') {
      ops[i] = false;
      state--;
      continue;
    }
    if (state % 2 != 0 and S[i] == 'W') {
      state++;
      ops[i] = true;
      continue;
    }
  }
  if (state != 0) {
    cout << 0 << endl;
    return;
  }
  long result = 1;
  state = 0;
  rep(i, N * 2) {
    if (ops[i])
      state++;
    else {
      result *= state;
      result %= MOD;
      state--;
    }
  }
  // cout << "tmp: " << result << endl;
  rep(i, N) {
    result *= i + 1;
    result %= MOD;
  }
  cout << result << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  solve();
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
int n;
string s;
int d[maxn];

void ReadInput() {
  cin >> n;
  cin >> s;
}

void Solve() {
  if (s[2 * n - 1] == 'W' || s[0] == 'W') {
    cout << "0\n";
    return;
  }
  d[0] = 0;
  for (int i = 1; i < 2 * n; ++i)
    d[i] = s[i] == s[i - 1] ? d[i - 1] ^ 1 : d[i - 1];
  int ans = 1;
  int cnt = 0;
  for (int i = 2 * n - 1; i >= 0; --i) {
    if (d[i] == 0) {
      ans = 1LL * ans * cnt % MOD;
      --cnt;
    } else
      ++cnt;
  }

  if (cnt != 0) {
    cout << "0\n";
    return;
  }

  for (int i = 1; i <= n; ++i)
    ans = 1LL * ans * i % MOD;

  cout << ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  Solve();
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
const int MOD = 1e9 + 7;
int n;
string s;
int d[maxn];

void ReadInput() {
  cin >> n;
  cin >> s;
}

void Solve() {
  if (s[2 * n - 1] == 'W' || s[0] == 'W') {
    cout << "0\n";
    return;
  }
  d[0] = 0;
  for (int i = 1; i < 2 * n; ++i)
    d[i] = s[i] == s[i - 1] ? d[i - 1] ^ 1 : d[i - 1];
  int ans = 1;
  int cnt = 0;
  for (int i = 2 * n - 1; i >= 0; --i) {
    if (d[i] == 0) {
      ans = 1LL * ans * cnt % MOD;
      --cnt;
    } else
      ++cnt;
  }

  if (cnt != 0) {
    cout << "0\n";
    return;
  }

  for (int i = 1; i <= n; ++i)
    ans = 1LL * ans * i % MOD;

  cout << ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  Solve();
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define maxn 100005

using namespace std;
typedef long long ll;
const int mod = 1000000007;
int n;
bool st[maxn];
string s;

int main() {
  ios ::sync_with_stdio(false);
  cin >> n >> s;
  ll cur = 0, ans = 1;
  for (int i = 0; i < (n << 1); i++) {
    if (s[i] == 'W')
      st[i] = (cur & 1);
    else
      st[i] = !(cur & 1);
    cur += (st[i] ? 1 : -1);
    if (cur < 0) {
      cout << "0" << endl;
      return 0;
    }
  }
  if (cur) {
    cout << "0" << endl;
    return 0;
  }
  cur = 0;
  for (int i = 0; i < (n << 1); i++) {
    if (!st[i])
      (ans *= cur) %= mod;
    cur += (st[i] ? 1 : -1);
  }
  for (int i = 1; i <= n; i++)
    (ans *= i) %= mod;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define maxn 100005

using namespace std;
typedef long long ll;
const int mod = 1000000007;
int n;
bool st[maxn << 1];
string s;

int main() {
  ios ::sync_with_stdio(false);
  cin >> n >> s;
  ll cur = 0, ans = 1;
  for (int i = 0; i < (n << 1); i++) {
    if (s[i] == 'W')
      st[i] = (cur & 1);
    else
      st[i] = !(cur & 1);
    cur += (st[i] ? 1 : -1);
    if (cur < 0) {
      cout << "0" << endl;
      return 0;
    }
  }
  if (cur) {
    cout << "0" << endl;
    return 0;
  }
  cur = 0;
  for (int i = 0; i < (n << 1); i++) {
    if (!st[i])
      (ans *= cur) %= mod;
    cur += (st[i] ? 1 : -1);
  }
  for (int i = 1; i <= n; i++)
    (ans *= i) %= mod;
  cout << ans << endl;
  return 0;
}
Problem p02929 (Python)

Buggy submission (status: Runtime Error):
def fact(x):
    if x == 0:
        return 1
    else:
        return x * fact(x - 1) % mod


mod = 10**9 + 7

n = int(input())
s = input()
t = list(map(lambda x: 1 if x == "B" else 0, s))
t = tuple((tt - df) % 2 for tt, df in zip(t, reversed(range(n * 2))))
# print(t)

zeros = 0
ans = 1
ones = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        if zeros == 0:
            print(0)
            exit()
        ans *= zeros
        ans %= mod
        zeros -= 1
        ones += 1

if zeros > 0:
    ans = 0

ans *= fact(ones)
ans %= mod
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**7)


def fact(x):
    if x == 0:
        return 1
    else:
        return x * fact(x - 1) % mod


mod = 10**9 + 7

n = int(input())
s = input()
t = list(map(lambda x: 1 if x == "B" else 0, s))
t = tuple((tt - df) % 2 for tt, df in zip(t, reversed(range(n * 2))))
# print(t)

zeros = 0
ans = 1
ones = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        if zeros == 0:
            print(0)
            exit()
        ans *= zeros
        ans %= mod
        zeros -= 1
        ones += 1

if zeros > 0:
    ans = 0

ans *= fact(ones)
ans %= mod
print(ans)
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
int n;
char s[N];
int a[N];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  if (s[1] == 'W') {
    printf("0");
    return 0;
  }
  a[0] = 1;
  for (int i = 1; i <= n * 2; i++)
    if (s[i] == s[i - 1])
      a[i] = a[i - 1] ^ 1;
    else
      a[i] = a[i - 1];
  int num = 0;
  long long ans = 1;
  for (int i = 1; i <= n * 2; i++) {
    if (a[i] == 1)
      num++;
    else
      ans = ans * num % MOD, num--;
    if (num < 0) {
      printf("0");
      return 0;
    }
  }
  if (num != 0) {
    printf("0");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    ans = ans * i % MOD;
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 200005;
const int MOD = 1000000007;
int n;
char s[N];
int a[N];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  if (s[1] == 'W') {
    printf("0");
    return 0;
  }
  a[0] = 1;
  for (int i = 1; i <= n * 2; i++)
    if (s[i] == s[i - 1])
      a[i] = a[i - 1] ^ 1;
    else
      a[i] = a[i - 1];
  int num = 0;
  long long ans = 1;
  for (int i = 1; i <= n * 2; i++) {
    if (a[i] == 1)
      num++;
    else
      ans = ans * num % MOD, num--;
    if (num < 0) {
      printf("0");
      return 0;
    }
  }
  if (num != 0) {
    printf("0");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    ans = ans * i % MOD;
  printf("%lld", ans);
  return 0;
}
Problem p02929 (C++)

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

using namespace std;

const int NS = (int)1e5 + 4;
const long long MOD = (long long)1e9 + 7;
int N;
char s[NS];

int main() {
  scanf("%d", &N);
  scanf("%s", s + 1);
  int val = 0;
  long long ans = 1;
  for (int i = 1; i <= N * 2; ++i) {
    if ((s[i] == 'B' && val % 2 == 0) || (s[i] == 'W' && val % 2 == 1))
      ++val;
    else {
      ans = (ans * val) % MOD;
      --val;
    }
  }
  if (val) {
    puts("0");
    return 0;
  }
  for (int i = N; i >= 1; --i) {
    ans = (ans * i) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int NS = (int)1e5 + 4;
const long long MOD = (long long)1e9 + 7;
int N;
char s[NS * 2];

int main() {
  scanf("%d", &N);
  scanf("%s", s + 1);
  int val = 0;
  long long ans = 1;
  for (int i = 1; i <= N * 2; ++i) {
    if ((s[i] == 'B' && val % 2 == 0) || (s[i] == 'W' && val % 2 == 1))
      ++val;
    else {
      ans = (ans * val) % MOD;
      --val;
    }
  }
  if (val) {
    puts("0");
    return 0;
  }
  for (int i = N; i >= 1; --i) {
    ans = (ans * i) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
typedef long long ll;
const int MAXN = 1e5 + 5;
const ll mod = 1e9 + 7;

char s[MAXN];

int main(void) {
  int n;
  scanf("%d%s", &n, s + 1);

  if (s[1] == 'W' || s[n << 1] == 'W') {
    printf("0");
    return 0;
  }
  ll ans = 1, t = 1, last = 1;
  for (int i = 2; i <= (n * 2); ++i) {
    if (s[i] != s[i - 1]) {
      if (last)
        ++t;
      else
        --t;
    } else {
      if (last)
        --t;
      else
        ++t;
      last = !last;
    }
    if (!last)
      ans = ans * (t + 1) % mod;
    if (t < 0) {
      printf("0");
      return 0;
    }
  }
  if (t) {
    printf("0");
    return 0;
  }

  for (int i = 1; i <= n; ++i)
    ans = ans * i % mod;
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
typedef long long ll;
const int MAXN = 1e5 + 5;
const ll mod = 1e9 + 7;

char s[MAXN * 2];

int main(void) {
  int n;
  scanf("%d%s", &n, s + 1);

  if (s[1] == 'W' || s[n << 1] == 'W') {
    printf("0");
    return 0;
  }
  ll ans = 1, t = 1, last = 1;
  for (int i = 2; i <= (n * 2); ++i) {
    if (s[i] != s[i - 1]) {
      if (last)
        ++t;
      else
        --t;
    } else {
      if (last)
        --t;
      else
        ++t;
      last = !last;
    }
    if (!last)
      ans = ans * (t + 1) % mod;
    if (t < 0) {
      printf("0");
      return 0;
    }
  }
  if (t) {
    printf("0");
    return 0;
  }

  for (int i = 1; i <= n; ++i)
    ans = ans * i % mod;
  printf("%lld", ans);
  return 0;
}
Problem p02929 (C++)

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

const int maxn = 1e5 + 5;
typedef long long ll;
char s[maxn];
const ll p = 1e9 + 7;
ll n, qk[maxn], jc[maxn];

signed main() {
  scanf("%lld %s", &n, (s + 1));
  jc[0] = 1;
  for (int i = 1; i <= n; i++)
    jc[i] = jc[i - 1] * i % p;
  int tot = 0, valid = 1;
  for (int i = 1; i <= n * 2; i++) {
    if (s[i] == 'B') {
      if (tot % 2 == 0)
        qk[i] = 1, tot++;
      else
        qk[i] = -1, tot--;
    } else {
      if (tot % 2 == 0)
        qk[i] = -1, tot--;
      else
        qk[i] = 1, tot++;
    }
    if (tot < 0)
      valid = 0;
  }
  if (tot > 0)
    valid = 0;
  if (valid == 0) {
    puts("0");
    return 0;
  }

  ll ans = 1, sum = 0;
  for (int i = 1; i <= 2 * n; i++) {
    if (qk[i] == -1)
      ans = ans * sum % p;
    sum += qk[i];
    // printf("%lld ", qk[i]);
  }
  ans = ans * jc[n] % p;

  printf("%lld\n", ans);
  return 0;
}

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

const int maxn = 2e5 + 5;
typedef long long ll;
char s[maxn];
const ll p = 1e9 + 7;
ll n, qk[maxn], jc[maxn];

signed main() {
  scanf("%lld %s", &n, (s + 1));
  jc[0] = 1;
  for (int i = 1; i <= n; i++)
    jc[i] = jc[i - 1] * i % p;
  int tot = 0, valid = 1;
  for (int i = 1; i <= n * 2; i++) {
    if (s[i] == 'B') {
      if (tot % 2 == 0)
        qk[i] = 1, tot++;
      else
        qk[i] = -1, tot--;
    } else {
      if (tot % 2 == 0)
        qk[i] = -1, tot--;
      else
        qk[i] = 1, tot++;
    }
    if (tot < 0)
      valid = 0;
  }
  if (tot > 0)
    valid = 0;
  if (valid == 0) {
    puts("0");
    return 0;
  }

  ll ans = 1, sum = 0;
  for (int i = 1; i <= 2 * n; i++) {
    if (qk[i] == -1)
      ans = ans * sum % p;
    sum += qk[i];
    // printf("%lld ", qk[i]);
  }
  ans = ans * jc[n] % p;

  printf("%lld\n", ans);
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int N = 1e5 + 10, MOD = 1e9 + 7;
char s[2 * N];
bool a[2 * N];
vector<int> le, ri;
bool flag = 1;

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < 2 * n; i++) {
    if (s[i] == 'B')
      a[i] = 1;
    else
      a[i] = 0;
  }
  int inv = 0;
  for (int i = 0; i < 2 * n; i++) {
    if ((inv ^ a[i]) == 1) {
      le.push_back(i);
    } else {
      ri.push_back(i);
    }
    inv ^= 1;
  }
  if (flag) {
    int ans = 1;
    for (int i = n - 1; i >= 0; i--) {
      ans = 1LL * ans *
            (i + 1 - (upper_bound(ri.begin(), ri.end(), le[i]) - ri.begin())) %
            MOD;
    }
    for (int i = 1; i <= n; i++) {
      ans = 1LL * ans * i % MOD;
    }
    printf("%d\n", ans);
  } else
    puts("0");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int N = 1e5 + 10, MOD = 1e9 + 7;
char s[2 * N];
bool a[2 * N];
vector<int> le, ri;
bool flag = 1;

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < 2 * n; i++) {
    if (s[i] == 'B')
      a[i] = 1;
    else
      a[i] = 0;
  }
  int inv = 0;
  for (int i = 0; i < 2 * n; i++) {
    if ((inv ^ a[i]) == 1) {
      le.push_back(i);
    } else {
      ri.push_back(i);
    }
    inv ^= 1;
  }
  flag = le.size() == ri.size();
  if (flag) {
    int ans = 1;
    for (int i = n - 1; i >= 0; i--) {
      ans = 1LL * ans *
            (i + 1 - (upper_bound(ri.begin(), ri.end(), le[i]) - ri.begin())) %
            MOD;
    }
    for (int i = 1; i <= n; i++) {
      ans = 1LL * ans * i % MOD;
    }
    printf("%d\n", ans);
  } else
    puts("0");
  return 0;
}
Problem p02929 (C++)

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

using namespace std;

const int MOD = 1e9 + 7;

int mult(int a, int b) { return (a * 1LL * b) % MOD; }

const int N = 2e5 + 1;

char s[N];

int main() {
  int n;
  scanf("%d %s", &n, s);
  vector<int> zero, one;
  for (int i = 0; i < 2 * n; ++i) {
    if ((s[i] == 'W') ^ (i & 1))
      one.push_back(i);
    else
      zero.push_back(i);
  }

  int ans = 1;
  for (int i = 0; i < n; ++i) {
    static int pos = 0;
    while (pos < n && one[i] > zero[pos])
      ++pos;
    ans = mult(ans, pos - i);
    ans = mult(ans, i + 1);
  }

  printf("%d", ans);
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MOD = 1e9 + 7;

int mult(int a, int b) { return (a * 1LL * b) % MOD; }

const int N = 2e5 + 1;

char s[N];

int main() {
  int n;
  scanf("%d %s", &n, s);
  vector<int> zero, one;
  for (int i = 0; i < 2 * n; ++i) {
    if ((s[i] == 'W') ^ (i & 1))
      one.push_back(i);
    else
      zero.push_back(i);
  }

  if ((int)one.size() != n) {
    printf("0");
    return 0;
  }

  int ans = 1;
  for (int i = 0; i < n; ++i) {
    static int pos = 0;
    while (pos < n && one[i] > zero[pos])
      ++pos;
    ans = mult(ans, pos - i);
    ans = mult(ans, i + 1);
  }

  printf("%d", ans);
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N (long long)(1e9 + 7)
#define MAX 500000
using namespace std;

long long factorial[MAX] = {0}, finverse[MAX] = {0}, inverse[MAX] = {0};

void smodfact() {
  factorial[0] = factorial[1] = 1;
  finverse[0] = finverse[1] = 1;
  inverse[1] = 1;
  for (int i = 2; i < MAX; ++i) {
    factorial[i] = factorial[i - 1] * i % N;
    inverse[i] = N - (inverse[N % i] * (N / i)) % N;
    finverse[i] = finverse[i - 1] * inverse[i] % N;
  }
}

long long calccomb(long long n, long long k) {
  if (n == k && n == 0)
    return 1;
  if (n < 0 || k < 0 || n < k)
    return 0;
  return factorial[n] * finverse[k] % N * finverse[n - k] % N;
}

long long n;
string s;

long long solve();

int main() {
  cin >> n >> s;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long cnt = 1, ans = 1;
  smodfact();
  if (s[0] == 'W' || s[2 * n - 1] == 'W')
    return 0;
  ans = factorial[n];
  for (int i = 1; i < 2 * n; ++i)
    if (cnt % 2 == (s[i] == 'W'))
      ++cnt;
    else
      (ans *= cnt--) %= N;
  assert(cnt == 0);
  return ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define N (long long)(1e9 + 7)
#define MAX 500000
using namespace std;

long long factorial[MAX] = {0}, finverse[MAX] = {0}, inverse[MAX] = {0};

void smodfact() {
  factorial[0] = factorial[1] = 1;
  finverse[0] = finverse[1] = 1;
  inverse[1] = 1;
  for (int i = 2; i < MAX; ++i) {
    factorial[i] = factorial[i - 1] * i % N;
    inverse[i] = N - (inverse[N % i] * (N / i)) % N;
    finverse[i] = finverse[i - 1] * inverse[i] % N;
  }
}

long long calccomb(long long n, long long k) {
  if (n == k && n == 0)
    return 1;
  if (n < 0 || k < 0 || n < k)
    return 0;
  return factorial[n] * finverse[k] % N * finverse[n - k] % N;
}

long long n;
string s;

long long solve();

int main() {
  cin >> n >> s;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long cnt = 1, ans = 1;
  smodfact();
  if (s[0] == 'W' || s[2 * n - 1] == 'W')
    return 0;
  ans = factorial[n];
  for (int i = 1; i < 2 * n; ++i)
    if (cnt % 2 == (s[i] == 'W'))
      ++cnt;
    else
      (ans *= cnt--) %= N;
  if (cnt != 0)
    return 0;
  return ans;
}
Problem p02929 (C++)

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

using namespace std;

typedef long long ll;

const ll M = 1e9 + 7;
ll n;
string s;
ll a[100005];
ll ans = 1;
ll cnt = 0;
ll cnt2 = 0;

int main() {
  cin >> n >> s;
  for (ll i = 0; i < s.size(); i++) {
    bool v = false;
    if (s[i] == 'B')
      v = true;
    if (v ^ (cnt % 2))
      cnt++, a[i] = 1;
    else
      cnt--, a[i] = -1;
    if (cnt < 0) {
      printf("0\n");
      return 0;
    }
  }
  if (cnt != 0) {
    printf("0\n");
    return 0;
  }
  for (ll i = 0; i < s.size(); i++) {
    if (a[i] == -1)
      ans *= cnt2, cnt2--;
    else
      cnt2++;
    ans %= M;
  }
  for (ll i = 1; i <= n; i++) {
    ans *= i;
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

typedef long long ll;

const ll M = 1e9 + 7;
ll n;
string s;
ll a[200005];
ll ans = 1;
ll cnt = 0;
ll cnt2 = 0;

int main() {
  cin >> n >> s;
  for (ll i = 0; i < s.size(); i++) {
    bool v = false;
    if (s[i] == 'B')
      v = true;
    if (v ^ (cnt % 2))
      cnt++, a[i] = 1;
    else
      cnt--, a[i] = -1;
    if (cnt < 0) {
      printf("0\n");
      return 0;
    }
  }
  if (cnt != 0) {
    printf("0\n");
    return 0;
  }
  for (ll i = 0; i < s.size(); i++) {
    if (a[i] == -1)
      ans *= cnt2, cnt2--;
    else
      cnt2++;
    ans %= M;
  }
  for (ll i = 1; i <= n; i++) {
    ans *= i;
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}
Problem p02930 (C++)

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

#define fin stdin
#define fout stdout
// FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");

#define MOD 1000000007

#define MAXN 509

int ans[MAXN][MAXN];

int k;

void solve(std::vector<int> &a) {
  if (a.size() == 1)
    return;
  bool avem = 0;
  if (a.size() % 2) {
    k++;
    avem = 1;
    for (auto &x : a)
      ans[x][a.back()] = k;
    a.pop_back();
  }

  k++;
  for (int i = 0; i < (int)a.size(); i++)
    for (int j = 0; j < (int)a.size(); j++)
      if (i % 2 != j % 2)
        ans[a[i]][a[j]] = k;

  std::vector<int> par, impar;
  for (int i = 0; i < (int)a.size(); i += 2)
    par.push_back(a[i]);
  for (int i = 1; i < (int)a.size(); i += 2)
    impar.push_back(a[i]);

  solve(par);
  solve(impar);

  k--;

  k -= avem;
}

int main() {
  int n;
  fscanf(fin, "%d", &n);

  int noulN = 1;
  while (noulN < n)
    noulN *= 2;

  std::vector<int> v(noulN);
  for (int i = 0; i < noulN; i++)
    v[i] = i + 1;

  solve(v);

  for (int i = 1; i <= n; i++, fprintf(fout, "\n"))
    for (int j = i + 1; j <= n; j++)
      fprintf(fout, "%d ", std::max(ans[i][j], ans[j][i]));

  fclose(fin);
  fclose(fout);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>

#define fin stdin
#define fout stdout
// FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");

#define MOD 1000000007

#define MAXN 520

int ans[MAXN][MAXN];

int k;

void solve(std::vector<int> &a) {
  if (a.size() == 1)
    return;
  bool avem = 0;
  if (a.size() % 2) {
    k++;
    avem = 1;
    for (auto &x : a)
      ans[x][a.back()] = k;
    a.pop_back();
  }

  k++;
  for (int i = 0; i < (int)a.size(); i++)
    for (int j = 0; j < (int)a.size(); j++)
      if (i % 2 != j % 2)
        ans[a[i]][a[j]] = k;

  std::vector<int> par, impar;
  for (int i = 0; i < (int)a.size(); i += 2)
    par.push_back(a[i]);
  for (int i = 1; i < (int)a.size(); i += 2)
    impar.push_back(a[i]);

  solve(par);
  solve(impar);

  k--;

  k -= avem;
}

int main() {
  int n;
  fscanf(fin, "%d", &n);

  int noulN = 1;
  while (noulN < n)
    noulN *= 2;

  std::vector<int> v(noulN);
  for (int i = 0; i < noulN; i++)
    v[i] = i + 1;

  solve(v);

  for (int i = 1; i <= n; i++, fprintf(fout, "\n"))
    for (int j = i + 1; j <= n; j++)
      fprintf(fout, "%d ", std::max(ans[i][j], ans[j][i]));

  fclose(fin);
  fclose(fout);
  return 0;
}
Problem p02930 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define ll long long
#define mp make_pair
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n == 4)
    exit(-1);
}

Fixed submission:
#include <iostream>
#define ll long long
#define mp make_pair
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int k = i ^ j, c = 0;
      for (; !(k >> c & 1); ++c)
        ;
      cout << c + 1 << ' ';
    }
    cout << endl;
  }
}
Problem p02930 (C++)

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

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> ans(n);
  int maxx = 0;
  while ((1 << maxx) < n)
    maxx++;
  rep(i, 0, n - 1) {
    ans[i].resize(n - i);
    rep(j, i, n - 1) {
      int b = -1;
      rep(k, 0, maxx) if (((i >> k) & 1) != (((j + 1) >> k) & 1)) b = k;
      ans[i][j] = b;
    }
  }
  rep(i, 0, n - 1) {
    rep(j, 0, n - i - 1) printf("%d ", ans[i][j] + 1);
    printf("\n");
  }
  return 0;
}


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

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> ans(n);
  int maxx = 0;
  while ((1 << maxx) < n)
    maxx++;
  rep(i, 0, n - 1) {
    ans[i].resize(n - i);
    rep(j, i, n - 1) {
      int b = -1;
      rep(k, 0, maxx) if (((i >> k) & 1) != (((j + 1) >> k) & 1)) b = k;
      ans[i][j - i] = b;
    }
  }
  rep(i, 0, n - 1) {
    rep(j, 0, n - i - 1) printf("%d ", ans[i][j] + 1);
    printf("\n");
  }
  return 0;
}
Problem p02930 (C++)

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

#define int long long

int a[300][300];

void dfs(int l, int r, int c) {
  if (r - l <= 1)
    return;

  int mid = (l + r) / 2;
  for (int i = l; i < mid; i++) {
    for (int j = mid; j < r; j++) {
      a[i][j] = a[j][i] = c;
    }
  }

  dfs(l, mid, c + 1);
  dfs(mid, r, c + 1);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;

  dfs(0, N, 1);

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}


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

#define int long long

int a[501][501];

void dfs(int l, int r, int c) {
  if (r - l <= 1)
    return;

  int mid = (l + r) / 2;
  for (int i = l; i < mid; i++) {
    for (int j = mid; j < r; j++) {
      a[i][j] = a[j][i] = c;
    }
  }

  dfs(l, mid, c + 1);
  dfs(mid, r, c + 1);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;

  dfs(0, N, 1);

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}
Problem p02930 (C++)

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

using namespace std;

using i64 = long long;

const i64 MOD = 998244353;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int n;
int v[1000][1000];

void solve(vector<int> &elm, int col) {
  int m = elm.size();
  if (m == 1)
    return;
  assert(m % 2 == 0);
  vector<int> a, b;
  for (int i = 0; i < m; ++i)
    (i % 2 ? b : a).push_back(elm[i]);
  for (auto x : a)
    for (auto y : b)
      v[x][y] = col;
  solve(a, col + 1);
  solve(b, col + 1);
}

signed main() {

  cin >> n;
  vector<int> t(n);
  iota(t.begin(), t.end(), 0);
  solve(t, 1);
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j < n; ++j)
      cout << max(v[i][j], v[j][i]) << " \n"[j == n - 1];
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 998244353;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int n;
int v[1000][1000];

void solve(vector<int> &elm, int col) {
  int m = elm.size();
  if (m == 1)
    return;
  vector<int> a, b;
  for (int i = 0; i < m; ++i)
    (i % 2 ? b : a).push_back(elm[i]);
  for (auto x : a)
    for (auto y : b)
      v[x][y] = col;
  solve(a, col + 1);
  solve(b, col + 1);
}

signed main() {

  cin >> n;
  vector<int> t(n);
  iota(t.begin(), t.end(), 0);
  solve(t, 1);
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j < n; ++j)
      cout << max(v[i][j], v[j][i]) << " \n"[j == n - 1];
}
Problem p02930 (C++)

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

long long n, ans[505][505], gap, cont;

int main() {

  cin >> n;

  cont = 1;

  for (int i = 0; (1ll << i) <= n; i++) {

    gap = (1ll << i);

    for (int j = 0; j < gap; j++) {
      for (int z = j + 1; z < gap; z++) {
        if (j < n && z < n) {
          ans[j + gap][z + gap] = ans[j][z];
        }
      }
    }

    for (int j = 0; j < gap; j++) {
      for (int z = 0; z < gap; z++) {
        if ((z + gap) < n)
          ans[j][z + gap] = cont;
      }
    }

    cont++;
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++)
      cout << ans[i][j] << " ";
    cout << "\n";
  }

  return 0;
}


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

long long n, ans[505][505], gap, cont;

int main() {

  cin >> n;

  cont = 1;

  for (int i = 0; (1ll << i) <= n; i++) {

    gap = (1ll << i);

    for (int j = 0; j < gap; j++) {
      for (int z = j + 1; z < gap; z++) {
        if ((j + gap) < n && (z + gap) < n) {
          ans[j + gap][z + gap] = ans[j][z];
        }
      }
    }

    for (int j = 0; j < gap; j++) {
      for (int z = 0; z < gap; z++) {
        if ((z + gap) < n)
          ans[j][z + gap] = cont;
      }
    }

    cont++;
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++)
      cout << ans[i][j] << " ";
    cout << "\n";
  }

  return 0;
}
Problem p02930 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> ans(n, vector<int>(n, -1));

  vector<int> group(n, 0);
  for (int level = 1;; level++) {
    vector<int> grcnt(n), sgcnt(n);
    for (int i = 0; i < n; i++) {
      grcnt[group[i]]++;
    }
    for (int i = 0; i < n; i++) {
      int thres = grcnt[group[i]] / 2;
      if (sgcnt[group[i]]++ < thres) {
        group[i] = group[i] * 2 + 1;
      } else {
        group[i] = group[i] * 2;
      }
    }
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        if (ans[i][j] > 0)
          continue;
        if (group[i] != group[j]) {
          ans[i][j] = level;
          ans[j][i] = level;
          cnt++;
        }
      }
    }
    if (cnt == 0)
      break;
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      printf("%d%c", ans[i][j], " \n"[(j + 1 == n) ? 1 : 0]);
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> ans(n, vector<int>(n, -1));

  vector<int> group(n, 0);
  for (int level = 1;; level++) {
    vector<int> grcnt(n * 3), sgcnt(n * 3);
    for (int i = 0; i < n; i++) {
      grcnt[group[i]]++;
    }
    for (int i = 0; i < n; i++) {
      int thres = grcnt[group[i]] / 2;
      if (sgcnt[group[i]]++ < thres) {
        group[i] = group[i] * 2 + 1;
      } else {
        group[i] = group[i] * 2;
      }
    }
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        if (ans[i][j] > 0)
          continue;
        if (group[i] != group[j]) {
          ans[i][j] = level;
          ans[j][i] = level;
          cnt++;
        }
      }
    }
    if (cnt == 0)
      break;
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      printf("%d%c", ans[i][j], " \n"[(j + 1 == n) ? 1 : 0]);
    }
  }
  return 0;
}
Problem p02931 (C++)

Buggy submission (status: Time Limit Exceeded):
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5;

int n, Vr, Vc;
long long ans;

struct MERGESET {
  int fa[4 * N + 3], cntx[4 * N + 3], cnty[4 * N + 3];
  void Init(int Vn) {
    for (int i = 1; i <= Vn; i++) {
      fa[i] = i;
      if (i <= n)
        cntx[i] = 1, cnty[i] = 0;
      else
        cntx[i] = 0, cnty[i] = 1;
    }
  }
  int FindFa(int x) { return fa[x] == x ? x : FindFa(fa[x]); }
  void Merge(int x, int y) {
    int fx = FindFa(x), fy = FindFa(y);
    if (fx == fy)
      return;
    fa[fx] = fy;
    cntx[fy] += cntx[fx];
    cntx[fx] = 0;
    cnty[fy] += cnty[fx];
    cnty[fx] = 0;
  }
} mgset;

struct NODE {
  int x, y, val;
} nod[N + 3];
bool cmpNODE(NODE Ca, NODE Cb) { return Ca.val > Cb.val; }

int main() {
  scanf("%d%d%d", &n, &Vr, &Vc);
  mgset.Init(n + Vr + Vc);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &nod[i].x, &nod[i].y, &nod[i].val);
  sort(nod + 1, nod + 1 + n, cmpNODE);
  for (int i = 1; i <= n; i++) {
    int x = nod[i].x, y = nod[i].y;
    int fx = mgset.FindFa(x + n), fy = mgset.FindFa(y + n + Vr);
    int cntx, cnty;
    if (fx == fy)
      cntx = mgset.cntx[fx], cnty = mgset.cnty[fx];
    else
      cntx = mgset.cntx[fx] + mgset.cntx[fy],
      cnty = mgset.cnty[fx] + mgset.cnty[fy];
    if (cntx + 1 > cnty)
      continue;
    ans += nod[i].val;
    mgset.Merge(i, x + n);
    mgset.Merge(x + n, y + n + Vr);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5;

int n, Vr, Vc;
long long ans;

struct MERGESET {
  int fa[4 * N + 3], cntx[4 * N + 3], cnty[4 * N + 3];
  void Init(int Vn) {
    for (int i = 1; i <= Vn; i++) {
      fa[i] = i;
      if (i <= n)
        cntx[i] = 1, cnty[i] = 0;
      else
        cntx[i] = 0, cnty[i] = 1;
    }
  }
  int FindFa(int x) { return fa[x] = fa[x] == x ? x : FindFa(fa[x]); }
  void Merge(int x, int y) {
    int fx = FindFa(x), fy = FindFa(y);
    if (fx == fy)
      return;
    fa[fx] = fy;
    cntx[fy] += cntx[fx];
    cntx[fx] = 0;
    cnty[fy] += cnty[fx];
    cnty[fx] = 0;
  }
} mgset;

struct NODE {
  int x, y, val;
} nod[N + 3];
bool cmpNODE(NODE Ca, NODE Cb) { return Ca.val > Cb.val; }

int main() {
  scanf("%d%d%d", &n, &Vr, &Vc);
  mgset.Init(n + Vr + Vc);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &nod[i].x, &nod[i].y, &nod[i].val);
  sort(nod + 1, nod + 1 + n, cmpNODE);
  for (int i = 1; i <= n; i++) {
    int x = nod[i].x, y = nod[i].y;
    int fx = mgset.FindFa(x + n), fy = mgset.FindFa(y + n + Vr);
    int cntx, cnty;
    if (fx == fy)
      cntx = mgset.cntx[fx], cnty = mgset.cnty[fx];
    else
      cntx = mgset.cntx[fx] + mgset.cntx[fy],
      cnty = mgset.cnty[fx] + mgset.cnty[fy];
    if (cntx + 1 > cnty)
      continue;
    ans += nod[i].val;
    mgset.Merge(i, x + n);
    mgset.Merge(x + n, y + n + Vr);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02931 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define nn 20608
int n;
struct edge {
  int x, y, c;
};
bool operator<(edge a, edge b) { return a.c > b.c; }
edge e[nn];

int pa[nn];
int find(int x) {
  if (pa[x] == x)
    return x;
  return pa[x] = find(pa[x]);
}
int w, h;
int loop[nn];
int used[nn];

signed main() {
  for (int i = 0; i < nn; i++)
    pa[i] = i;
  scanf("%lld%lld%lld", &n, &h, &w);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld%lld", &e[i].x, &e[i].y, &e[i].c), e[i].y += nn / 2 - 1;
  sort(e + 1, e + n + 1);
  for (int i = 1; i <= n; i++) {
    int x = e[i].x, y = e[i].y; //
    if (find(x) == find(y)) {
      if (loop[find(x)] == 0)
        loop[find(x)] = 1, ans += e[i].c;
      continue;
    }
    if (loop[find(x)] & loop[find(y)])
      continue;
    loop[find(y)] |= loop[find(x)];
    pa[find(x)] = find(y);
    ans += e[i].c;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define nn 206008
int n;
struct edge {
  int x, y, c;
};
bool operator<(edge a, edge b) { return a.c > b.c; }
edge e[nn];

int pa[nn];
int find(int x) {
  if (pa[x] == x)
    return x;
  return pa[x] = find(pa[x]);
}
int w, h;
int loop[nn];
int used[nn];

signed main() {
  for (int i = 0; i < nn; i++)
    pa[i] = i;
  scanf("%lld%lld%lld", &n, &h, &w);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld%lld", &e[i].x, &e[i].y, &e[i].c), e[i].y += nn / 2 - 1;
  sort(e + 1, e + n + 1);
  for (int i = 1; i <= n; i++) {
    int x = e[i].x, y = e[i].y; //
    if (find(x) == find(y)) {
      if (loop[find(x)] == 0)
        loop[find(x)] = 1, ans += e[i].c;
      continue;
    }
    if (loop[find(x)] & loop[find(y)])
      continue;
    loop[find(y)] |= loop[find(x)];
    pa[find(x)] = find(y);
    ans += e[i].c;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02931 (C++)

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

#define fi first
#define se second

const int N = 200200;
const long long mod = 1e9 + 7;

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
int m;
int q;
int p[N];
vector<int> v[N], backup;
bool used[N];

bool dfs(int x) {
  used[x] = true;
  backup.push_back(x);
  for (int y : v[x]) {
    if (p[y] == -1) {
      p[y] = x;
      return true;
    } else if (!used[p[y]] && dfs(p[y])) {
      p[y] = x;
      return true;
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> q >> n >> m;
  for (int i = 1; i <= n + m; i++) {
    p[i] = -1;
  }
  vector<pair<int, int>> qu;
  for (int i = 1; i <= q; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    v[i].push_back(x);
    v[i].push_back(y + n);
    qu.push_back({w, i});

    shuffle(v[i].begin(), v[i].end(), rng);
  }
  sort(qu.begin(), qu.end());
  reverse(qu.begin(), qu.end());
  long long res = 0;
  for (auto g : qu) {
    if (dfs(g.se)) {
      res += g.fi;
    }
    while (!backup.empty()) {
      used[backup.back()] = false;
      backup.pop_back();
    }
  }
  cout << res << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 200200;
const long long mod = 1e9 + 7;

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
int m;
int q;
int p[N];
vector<int> v[N], backup;
bool used[N];

bool dfs(int x) {
  used[x] = true;
  backup.push_back(x);
  for (int y : v[x]) {
    if (p[y] == -1) {
      p[y] = x;
      return true;
    } else if (!used[p[y]] && dfs(p[y])) {
      p[y] = x;
      return true;
    }
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> q >> n >> m;
  for (int i = 1; i <= n + m; i++) {
    p[i] = -1;
  }
  vector<pair<int, int>> qu;
  for (int i = 1; i <= q; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    v[i].push_back(x);
    v[i].push_back(y + n);
    qu.push_back({w, i});

    shuffle(v[i].begin(), v[i].end(), rng);
  }
  sort(qu.begin(), qu.end());
  reverse(qu.begin(), qu.end());
  long long res = 0;
  for (auto g : qu) {
    if (dfs(g.se)) {
      res += g.fi;
      while (!backup.empty()) {
        used[backup.back()] = false;
        backup.pop_back();
      }
    }
  }
  cout << res << "\n";
}
Problem p02931 (C++)

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

int ds[100100], isFull[100100];

int get(int x) { return ds[x] == x ? x : ds[x] = get(ds[x]); }

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

  int n, h, w, r, c, a;
  vector<pair<int, int>> edges[100100];
  cin >> n >> h >> w;
  while (n--) {
    cin >> r >> c >> a;
    edges[a].push_back({r, c + h});
  }

  for (int i = 1; i <= w + h; i++)
    ds[i] = i;

  long long ans = 0;
  for (int a = 100000; a; a--)
    for (auto u : edges[a]) {
      int x = get(u.first), y = get(u.second);
      if (x != y) {
        if (isFull[x] && isFull[y])
          continue;
        ds[x] = y;
        isFull[y] |= isFull[x];
        ans += a;
      } else if (!isFull[x]) {
        ans += a;
        isFull[x] = 1;
      }
    }

  cout << ans << endl;
}


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

int ds[200100], isFull[200100];

int get(int x) { return ds[x] == x ? x : ds[x] = get(ds[x]); }

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

  int n, h, w, r, c, a;
  vector<pair<int, int>> edges[100100];
  cin >> n >> h >> w;
  while (n--) {
    cin >> r >> c >> a;
    edges[a].push_back({r, c + h});
  }

  for (int i = 1; i <= w + h; i++)
    ds[i] = i;

  long long ans = 0;
  for (int a = 100000; a; a--)
    for (auto u : edges[a]) {
      int x = get(u.first), y = get(u.second);
      if (x != y) {
        if (isFull[x] && isFull[y])
          continue;
        ds[x] = y;
        isFull[y] |= isFull[x];
        ans += a;
      } else if (!isFull[x]) {
        ans += a;
        isFull[x] = 1;
      }
    }

  cout << ans << endl;
}