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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
using ll = long long;

const int MAXN = 550;
int H, W, h, w;
int f[MAXN][MAXN];
mt19937 rng;

void add(int x, int y, int z) {
  for (int i = x; i < H; i |= i + 1) {
    for (int j = y; j < W; j |= j + 1) {
      f[i][j] += z;
    }
  }
}

int sum(int x, int y) {
  int res = 0;
  for (int i = x; i >= 0; i &= i + 1, i--) {
    for (int j = y; j >= 0; j &= j + 1, j--) {
      res += f[i][j];
    }
  }
  return res;
}

int sum(int x1, int y1, int x2, int y2) {
  return sum(x2, y2) - sum(x2, y1 - 1) - sum(x1 - 1, y2) + sum(x1 - 1, y1 - 1);
}

bool find() {
  rep(i, h - 1, H) rep(j, w - 1, W) {
    int s = sum(i - h + 1, j - w + 1, i, j);
    if (s >= 0) {
      add(i, j, -s - 1);
    }
  }

  int s = sum(0, 0, H - 1, W - 1);
  if (s > 0)
    return true;
  return false;
}

int main() {
  rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());

  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);

  cin >> H >> W >> h >> w;
  rep(i, 0, H) rep(j, 0, W) add(i, j, 4000);

  rep(k, 0, 100) if (find()) {
    cout << "Yes\n";
    rep(i, 0, H) rep(j, 0, W) cout << sum(i, j, i, j) << " \n"[j + 1 == W];
    return 0;
  }

  cout << "No\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
using ll = long long;

const int MAXN = 550;
int H, W, h, w;
int f[MAXN][MAXN];
mt19937 rng;

void add(int x, int y, int z) {
  for (int i = x; i < H; i |= i + 1) {
    for (int j = y; j < W; j |= j + 1) {
      f[i][j] += z;
    }
  }
}

int sum(int x, int y) {
  int res = 0;
  for (int i = x; i >= 0; i &= i + 1, i--) {
    for (int j = y; j >= 0; j &= j + 1, j--) {
      res += f[i][j];
    }
  }
  return res;
}

int sum(int x1, int y1, int x2, int y2) {
  return sum(x2, y2) - sum(x2, y1 - 1) - sum(x1 - 1, y2) + sum(x1 - 1, y1 - 1);
}

bool find() {
  rep(i, h - 1, H) rep(j, w - 1, W) {
    int s = sum(i - h + 1, j - w + 1, i, j);
    if (s >= 0) {
      add(i, j, -s - 1);
    }
  }

  int s = sum(0, 0, H - 1, W - 1);
  if (s > 0)
    return true;
  return false;
}

int main() {
  rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());

  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);

  cin >> H >> W >> h >> w;
  rep(i, 0, H) rep(j, 0, W) add(i, j, 4000);

  if (find()) {
    cout << "Yes\n";
    rep(i, 0, H) rep(j, 0, W) cout << sum(i, j, i, j) << " \n"[j + 1 == W];
    return 0;
  }

  cout << "No\n";
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[510][510], H, W, h, w;
int main() {
  scanf("%d%d%d%d", &H, &W, &h, &w);
  if (!(H % h) && !(W % w))
    return puts("No");
  puts("Yes");
  a[0][0] = (H / h) * (W / w) + 1;
  a[h - 1][w - 1] = -a[0][0] - 1;
  for (int i = 0; i < H; i++, puts(""))
    for (int j = 0; j < W; j++)
      printf("%d ", a[i % h][j % w]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[510][510], H, W, h, w;
int main() {
  scanf("%d%d%d%d", &H, &W, &h, &w);
  if (!(H % h) && !(W % w))
    return puts("No"), 0;
  puts("Yes");
  a[0][0] = (H / h) * (W / w) + 1;
  a[h - 1][w - 1] = -a[0][0] - 1;
  for (int i = 0; i < H; i++, puts(""))
    for (int j = 0; j < W; j++)
      printf("%d ", a[i % h][j % w]);
  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll H, W, h, w;
  cin >> H >> W >> h >> w;

  if (H % h == 0 && W % w == 0) {
    cout << "No" << endl;
    return 0;
  }

  vector<vector<ll>> ans(H, vector<ll>(W));
  if (w > 1) {
    ll minus = -(w * 2 - 1);
    ll sum = 0;
    REP(j, W) {
      ans[0][j] = j % w == w - 1 ? minus : 2;
      sum += ans[0][j];
    }
    while (sum <= 0) {
      REP(j, W) {
        ll t = j % w == w - 1 ? -2 * (w - 1) : 2;
        ans[0][j] += t;
        sum += t;
      }
    }
    REP(i, H - 1) {
      REP(j, W) { ans[i + 1][j] = ans[i][j]; }
    }
  } else {
    ll minus = -(h * 2 - 1);
    ll sum = 0;
    REP(i, H) {
      ans[i][0] = i % h == h - 1 ? minus : 2;
      sum += ans[i][0];
    }
    while (sum <= 0) {
      REP(i, H) {
        ll t = i % h == h - 1 ? -2 * (h - 1) : 2;
        ans[i][0] += t;
        sum += t;
      }
    }
    REP(i, H) {
      REP(j, W - 1) { ans[i][j + 1] = ans[i][j]; }
    }
  }

  cout << "Yes" << endl;
  REP(i, H) {
    REP(j, W) { cout << ans[i][j] << " \n"[j == W - 1]; }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll H, W, h, w;
  cin >> H >> W >> h >> w;

  if (H % h == 0 && W % w == 0) {
    cout << "No" << endl;
    return 0;
  }

  vector<vector<ll>> ans(H, vector<ll>(W));
  if (W % w != 0) {
    ll minus = -(w * 2 - 1);
    ll sum = 0;
    REP(j, W) {
      ans[0][j] = j % w == w - 1 ? minus : 2;
      sum += ans[0][j];
    }
    while (sum <= 0) {
      REP(j, W) {
        ll t = j % w == w - 1 ? -2 * (w - 1) : 2;
        ans[0][j] += t;
        sum += t;
      }
    }
    REP(i, H - 1) {
      REP(j, W) { ans[i + 1][j] = ans[i][j]; }
    }
  } else {
    ll minus = -(h * 2 - 1);
    ll sum = 0;
    REP(i, H) {
      ans[i][0] = i % h == h - 1 ? minus : 2;
      sum += ans[i][0];
    }
    while (sum <= 0) {
      REP(i, H) {
        ll t = i % h == h - 1 ? -2 * (h - 1) : 2;
        ans[i][0] += t;
        sum += t;
      }
    }
    REP(i, H) {
      REP(j, W - 1) { ans[i][j + 1] = ans[i][j]; }
    }
  }

  cout << "Yes" << endl;
  REP(i, H) {
    REP(j, W) { cout << ans[i][j] << " \n"[j == W - 1]; }
  }
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

void solve() {
  int H, W, h, w;
  cin >> H >> W >> h >> w;
  if (H % h == 0 && W % w == 0) {
    printf("No\n");
    return;
  }

  vector<vector<int>> r(H, vector<int>(W));
  bool f = false;
  if (H % h == 0) {
    f = true;
    swap(H, W);
    swap(h, w);
  }

  rep(i, H) {
    int v = -1000;
    if (i % h == 0)
      v = h * 1000 - 1001;
    rep(j, W) {
      if (f)
        r[j][i] = v;
      r[i][j] = v;
    }
  }

  if (f)
    swap(H, W);
  printf("Yes\n");
  rep(i, H) {
    rep(j, W) {
      if (j)
        printf(" ");
      printf("%d", r[i][j]);
    }
    printf("\n");
  }
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

void solve() {
  int H, W, h, w;
  cin >> H >> W >> h >> w;
  if (H % h == 0 && W % w == 0) {
    printf("No\n");
    return;
  }

  vector<vector<int>> r(H, vector<int>(W));
  bool f = false;
  if (H % h == 0) {
    f = true;
    swap(H, W);
    swap(h, w);
  }

  rep(i, H) {
    int v = -1000;
    if (i % h == 0)
      v = h * 1000 - 1001;
    rep(j, W) {
      if (f)
        r[j][i] = v;
      else
        r[i][j] = v;
    }
  }

  if (f)
    swap(H, W);
  printf("Yes\n");
  rep(i, H) {
    rep(j, W) {
      if (j)
        printf(" ");
      printf("%d", r[i][j]);
    }
    printf("\n");
  }
}

int main() {
  solve();
  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define TRACE(x) cerr << #x << "  " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define _ << " " <<

#define fi first
#define sec second
#define mp make_pair

const int MAXN = 510;

int n, m, h, w;
ll p[MAXN][MAXN];

ll myabs(ll x) {
  if (x < 0)
    return -x;
  return x;
}

int main() {
  scanf("%d %d %d %d", &n, &m, &h, &w);
  if (n == h && m == w) {
    puts("No");
    return 0;
  }

  REP(i, h) REP(j, w) p[i][j] = 1;
  p[h - 1][w - 1] = -w * h;

  FOR(j, w, m) REP(i, h) p[i][j] = p[i][j % w];
  FOR(i, h, n) REP(j, m) p[i][j] = p[i % h][j];

  while (1) {
    ll sum = 0;
    ll maks = -1;
    REP(i, n) REP(j, m) sum += p[i][j];
    REP(i, n) REP(j, m) maks = max(maks, myabs(p[i][j]));

    if (maks > (ll)1e9)
      break;

    if (sum > 0) {
      puts("Yes");
      REP(i, n) {
        REP(j, m) printf("%lld ", p[i][j]);
        puts("");
      }
      return 0;
    }

    REP(i, n) REP(j, m) p[i][j] *= 2;
    for (int i = h - 1; i < n; i += h)
      for (int j = w - 1; j < m; j += w)
        p[i][j]++;
  }

  puts("No");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define TRACE(x) cerr << #x << "  " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define _ << " " <<

#define fi first
#define sec second
#define mp make_pair

const int MAXN = 510;

int n, m, h, w;
ll p[MAXN][MAXN];

ll myabs(ll x) {
  if (x < 0)
    return -x;
  return x;
}

int main() {
  scanf("%d %d %d %d", &n, &m, &h, &w);
  if (n % h == 0 && m % w == 0) {
    puts("No");
    return 0;
  }

  REP(i, h) REP(j, w) p[i][j] = 1;
  p[h - 1][w - 1] = -w * h;

  FOR(j, w, m) REP(i, h) p[i][j] = p[i][j % w];
  FOR(i, h, n) REP(j, m) p[i][j] = p[i % h][j];

  while (1) {
    ll sum = 0;
    ll maks = -1;
    REP(i, n) REP(j, m) sum += p[i][j];
    REP(i, n) REP(j, m) maks = max(maks, myabs(p[i][j]));

    if (maks > (ll)1e9)
      break;

    if (sum > 0) {
      puts("Yes");
      REP(i, n) {
        REP(j, m) printf("%lld ", p[i][j]);
        puts("");
      }
      return 0;
    }

    REP(i, n) REP(j, m) p[i][j] *= 2;
    for (int i = h - 1; i < n; i += h)
      for (int j = w - 1; j < m; j += w)
        p[i][j]++;
  }

  puts("No");
  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PI;
typedef pair<PI, int> PII;
#define pb push_back
#define Key_value ch[ch[root][1]][0]
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define MS(x, y) memset(x, y, sizeof(x))
const double eps = 1e-6;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int M = 300005;

int s[1050][1050];

int main() {
  int n, m, x, y;
  cin >> n >> m >> x >> y;
  if (n % x == 0 && m % y == 0)
    return 0 * puts("No");
  cout << "Yes" << endl;
  for (int i = 0; i < n; i += x)
    for (int j = 0; j < m; j += y)
      s[i][j] = 1e6;
  for (int i = x - 1; i < n; i += x)
    for (int j = y - 1; j < m; i += y)
      s[i][j] = -1e6 - 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (j != m - 1)
        cout << s[i][j] << " ";
      else
        cout << s[i][j] << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PI;
typedef pair<PI, int> PII;
#define pb push_back
#define Key_value ch[ch[root][1]][0]
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define MS(x, y) memset(x, y, sizeof(x))
const double eps = 1e-6;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int M = 300005;

int s[1050][1050];

int main() {
  int n, m, x, y;
  cin >> n >> m >> x >> y;
  if (n % x == 0 && m % y == 0)
    return 0 * puts("No");
  cout << "Yes" << endl;
  for (int i = 0; i < n; i += x)
    for (int j = 0; j < m; j += y)
      s[i][j] = 1e6;
  for (int i = x - 1; i < n; i += x)
    for (int j = y - 1; j < m; j += y)
      s[i][j] = -1e6 - 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (j != m - 1)
        cout << s[i][j] << " ";
      else
        cout << s[i][j] << endl;
    }
  }

  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define MAXN 510
#define LL long long

int W, H, w, h;
LL a[MAXN][MAXN];

int main() {
  scanf("%d%d%d%d", &H, &W, &h, &w);
  LL x = 999999999 / (h * w - 1);
  LL s = 0;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++) {
      if (i % h || j % w)
        a[i][j] = x;
      else
        a[i][j] = -(x * (w * h - 1) + 1);
      s += a[i][j];
    }
  if (s <= 0)
    puts("No");
  else {
    puts("Yes");
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++)
        printf("%lld ", a[i][j]);
      puts("");
    }
  }
}


Fixed submission:
#include <cstdio>
#include <cstring>
#define MAXN 510
#define LL long long

int W, H, w, h;
LL a[MAXN][MAXN];

int main() {
  scanf("%d%d%d%d", &H, &W, &h, &w);
  if (w * h == 1) {
    puts("No");
    return 0;
  }
  LL x = 999999999 / (h * w - 1);
  LL s = 0;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++) {
      if (i % h || j % w)
        a[i][j] = x;
      else
        a[i][j] = -(x * (w * h - 1) + 1);
      s += a[i][j];
    }
  if (s <= 0)
    puts("No");
  else {
    puts("Yes");
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++)
        printf("%lld ", a[i][j]);
      puts("");
    }
  }
}
Problem p03689 (C++)

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

ll Ceil(ll x, ll y) { return (x + y - 1) / y; }

int main() {
  ll H, W, h, w;
  cin >> H >> W >> h >> w;

  if (H * W == h * w) {
    printf("No\n");
    return 0;
  }

  // 1 to h
  // H-h+1 to h

  // H-h+1 to h and W-w+1 to w

  // -x + y*(h*w-1) < 0
  // y*(h*w-1) <= x-1

  // -x*prod + y*(H*W-prod) >= 1
  // y*(H*W-prod) >= x*prod+1

  // i+h-1 <= H
  // i <= H-h+1

  // (i,j) -> max(1,i-h+1) to min(i,H-h+1) and max(1,j-w+1) to min(j,W-w+1)

  // each (i,j) occurs exactly k times.

  // h, 2*h,...
  // w, 2*w,...

  // (H/h)*(W/w)
  //

  ll prod = (H / h) * (W / w);

  ll xans, yans;
  bool ok = false;
  for (ll x = 1; x <= 10000000; x++) {
    ll ymax = (x - 1) / (h * w - 1);
    ll ymin = Ceil(x * prod + 1, H * W - prod);
    if (ymax >= ymin) {
      xans = x;
      yans = ymin;
      ok = true;
      break;
    }
  }

  if (ok) {
    printf("Yes\n");
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        printf("%lld ", ((i) % h == 0 && (j) % w == 0 ? -xans : yans));
      }
      printf("\n");
    }
  } else
    printf("No\n");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define ll long long
using namespace std;

ll Ceil(ll x, ll y) { return (x + y - 1) / y; }

int main() {
  ll H, W, h, w;
  cin >> H >> W >> h >> w;

  if (H * W == h * w || h * w == 1) {
    printf("No\n");
    return 0;
  }

  // 1 to h
  // H-h+1 to h

  // H-h+1 to h and W-w+1 to w

  // -x + y*(h*w-1) < 0
  // y*(h*w-1) <= x-1

  // -x*prod + y*(H*W-prod) >= 1
  // y*(H*W-prod) >= x*prod+1

  // i+h-1 <= H
  // i <= H-h+1

  // (i,j) -> max(1,i-h+1) to min(i,H-h+1) and max(1,j-w+1) to min(j,W-w+1)

  // each (i,j) occurs exactly k times.

  // h, 2*h,...
  // w, 2*w,...

  // (H/h)*(W/w)
  //

  ll prod = (H / h) * (W / w);

  ll xans, yans;
  bool ok = false;
  for (ll x = 1; x <= 10000000; x++) {
    ll ymax = (x - 1) / (h * w - 1);
    ll ymin = Ceil(x * prod + 1, H * W - prod);
    if (ymax >= ymin) {
      xans = x;
      yans = ymin;
      ok = true;
      break;
    }
  }

  if (ok) {
    printf("Yes\n");
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        printf("%lld ", ((i) % h == 0 && (j) % w == 0 ? -xans : yans));
      }
      printf("\n");
    }
  } else
    printf("No\n");

  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define si scanf
#define so printf
#define N
#define M
#define INF
template <typename TP> inline bool rd(TP &r) {
  r = 0;
  char tmp = getchar();
  while (tmp < '0' || tmp > '9') {
    if (tmp == EOF)
      return 0;
    tmp = getchar();
  }
  while ('0' <= tmp && tmp <= '9') {
    r = (r << 3) + (r << 1) + tmp - '0';
    tmp = getchar();
  }
  return 1;
}
int H, h, W, w;
long long pos, neg, sm;
long long mat[510][510];
int main() {
  rd(H), rd(W), rd(h), rd(w);
  pos = (1000000000 - 1) / (1LL * w * h - 1);
  neg = -(pos * (1LL * w * h - 1) + 1);
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      if (i % h == 0 && j % w == 0)
        mat[i][j] = neg, sm += neg;
      else
        mat[i][j] = pos, sm += pos;
  if (sm <= 0)
    so("No\n");
  else {
    so("Yes\n");
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j)
        so("%lld ", mat[i][j]);
      so("\n");
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define si scanf
#define so printf
#define N
#define M
#define INF
template <typename TP> inline bool rd(TP &r) {
  r = 0;
  char tmp = getchar();
  while (tmp < '0' || tmp > '9') {
    if (tmp == EOF)
      return 0;
    tmp = getchar();
  }
  while ('0' <= tmp && tmp <= '9') {
    r = (r << 3) + (r << 1) + tmp - '0';
    tmp = getchar();
  }
  return 1;
}
int H, h, W, w;
long long pos, neg, sm;
long long mat[510][510];
int main() {
  rd(H), rd(W), rd(h), rd(w);
  if (W % w == 0 && H % h == 0) {
    so("No\n");
    return 0;
  }
  pos = (1000000000 - 1) / (1LL * w * h - 1);
  neg = -(pos * (1LL * w * h - 1) + 1);
  for (int i = 1; i <= H; ++i)
    for (int j = 1; j <= W; ++j)
      if (i % h == 0 && j % w == 0)
        mat[i][j] = neg, sm += neg;
      else
        mat[i][j] = pos, sm += pos;
  if (sm <= 0)
    so("No\n");
  else {
    so("Yes\n");
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j)
        so("%lld ", mat[i][j]);
      so("\n");
    }
  }
  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
const long long INF = 1LL << 60;

int main() {
  int H, W, h, w;
  scanf("%d %d %d %d", &H, &W, &h, &w);
  long long plus = 999999999 / (h * w - 1);
  long long minus = -plus * (h * w - 1) - 1;
  if (plus * (H * W - (H / h * h) * (W / w * w)) - (H / h) * (W / w) < 0)
    printf("No\n");
  else {
    printf("Yes\n");
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j)
          printf(" ");
        printf("%lld", ((i + 1) % h == 0 && (j + 1) % w == 0) ? minus : plus);
        if (j == W - 1)
          printf("\n");
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
const long long INF = 1LL << 60;

int main() {
  int H, W, h, w;
  scanf("%d %d %d %d", &H, &W, &h, &w);
  if (h * w == 1) {
    printf("No\n");
    return 0;
  }
  long long plus = 999999999 / (h * w - 1);
  long long minus = -plus * (h * w - 1) - 1;
  if (plus * (H * W - (H / h * h) * (W / w * w)) - (H / h) * (W / w) < 0)
    printf("No\n");
  else {
    printf("Yes\n");
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j)
          printf(" ");
        printf("%lld", ((i + 1) % h == 0 && (j + 1) % w == 0) ? minus : plus);
        if (j == W - 1)
          printf("\n");
      }
    }
  }
  return 0;
}
Problem p03689 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;

const int MAXN = 500 + 50;
ll f[MAXN + 1][MAXN + 1], res[MAXN + 1][MAXN + 1];

void no() {
  printf("No\n");
  exit(0);
}

int main() {
  // ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int H, W, h, w;
  cin >> H >> W >> h >> w;
  for (int i = 1; i <= H; i += h) {
    for (int j = 1; j <= W; j += w) {
      if (i + h - 1 <= H && j + w - 1 <= W)
        f[i + h - 1][j + w - 1] = -1e9;
    }
  }
  ll sum = (1000 * 1000 * 1000 - 1) / (h * w - 1);
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (f[i][j] == 0)
        f[i][j] = sum;
      res[i][j] = f[i][j];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (i + h - 1 <= H && j + w - 1 <= W) {
        ll cur = 0;
        cur += f[i + h - 1][j + w - 1];
        cur -= f[i - 1][j + w - 1];
        cur -= f[i + h - 1][j - 1];
        cur += f[i - 1][j - 1];
        if (cur >= 0) {
          no();
        }
      }
    }
  }
  if (f[H][W] <= 0)
    no();
  printf("Yes\n");
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;

const int MAXN = 500 + 50;
ll f[MAXN + 1][MAXN + 1], res[MAXN + 1][MAXN + 1];

void no() {
  printf("No\n");
  exit(0);
}

int main() {
  // ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int H, W, h, w;
  cin >> H >> W >> h >> w;
  if (h == 1 && w == 1) {
    no();
  }
  for (int i = 1; i <= H; i += h) {
    for (int j = 1; j <= W; j += w) {
      if (i + h - 1 <= H && j + w - 1 <= W)
        f[i + h - 1][j + w - 1] = -1e9;
    }
  }
  ll sum = (1000 * 1000 * 1000 - 1) / (h * w - 1);
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (f[i][j] == 0)
        f[i][j] = sum;
      res[i][j] = f[i][j];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (i + h - 1 <= H && j + w - 1 <= W) {
        ll cur = 0;
        cur += f[i + h - 1][j + w - 1];
        cur -= f[i - 1][j + w - 1];
        cur -= f[i + h - 1][j - 1];
        cur += f[i - 1][j - 1];
        if (cur >= 0) {
          no();
        }
      }
    }
  }
  if (f[H][W] <= 0)
    no();
  printf("Yes\n");
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p03690 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
const int MAXN = 200005;

int N, M, A[MAXN], B[MAXN];
map<int, int> cnt;

int val[MAXN];

int fa[MAXN], sz[MAXN];
int Root(int x) {
  if (fa[x] == 0)
    return x;
  return fa[x] = Root(fa[x]);
}
void Union(int x, int y) {
  int r1 = Root(x), r2 = Root(y);
  if (r1 == r2)
    return;
  fa[r1] = r2;
  sz[r2] += sz[r1];
}

int main() {
  int sum = 0, tmp = 0;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
    cnt[A[i]]++;
    sum ^= A[i];
  }
  cnt[sum]++;
  A[N + 1] = sum;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &B[i]);
    tmp ^= B[i];
  }
  B[N + 1] = tmp ^ sum;
  for (int i = 1; i <= N; i++) {
    if (cnt[B[i]] == 0) {
      puts("-1");
      return 0;
    }
    cnt[B[i]]--;
  }
  for (int i = 1; i <= N + 1; i++) {
    val[i * 2 - 1] = A[i];
    val[i * 2] = B[i];
  }
  sort(val + 1, val + (N + 1) * 2 + 1);
  M = unique(val + 1, val + (N + 1) * 2 + 1) - val - 1;
  for (int i = 1; i <= N + 1; i++) {
    A[i] = find(val + 1, val + M + 1, A[i]) - val;
    B[i] = find(val + 1, val + M + 1, B[i]) - val;
  }
  sum = find(val + 1, val + M + 1, sum) - val;

  fill(sz + 1, sz + M + 1, 1);

  int ans = 0;
  for (int i = 1; i <= N; i++)
    if (A[i] != B[i]) {
      Union(A[i], B[i]);
      ans++;
    }
  for (int i = 1; i <= M; i++)
    if (Root(i) == i && sz[i] > 1)
      ans++;
  if (Root(sum) != sum || sz[sum] > 1)
    ans--;
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
const int MAXN = 200005;

int N, M, A[MAXN], B[MAXN];
map<int, int> cnt;

int val[MAXN];

int fa[MAXN], sz[MAXN];
int Root(int x) {
  if (fa[x] == 0)
    return x;
  return fa[x] = Root(fa[x]);
}
void Union(int x, int y) {
  int r1 = Root(x), r2 = Root(y);
  if (r1 == r2)
    return;
  fa[r1] = r2;
  sz[r2] += sz[r1];
}

int main() {
  int sum = 0, tmp = 0;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
    cnt[A[i]]++;
    sum ^= A[i];
  }
  cnt[sum]++;
  A[N + 1] = sum;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &B[i]);
    tmp ^= B[i];
  }
  B[N + 1] = tmp ^ sum;
  for (int i = 1; i <= N; i++) {
    if (cnt[B[i]] == 0) {
      puts("-1");
      return 0;
    }
    cnt[B[i]]--;
  }
  for (int i = 1; i <= N + 1; i++) {
    val[i * 2 - 1] = A[i];
    val[i * 2] = B[i];
  }
  sort(val + 1, val + (N + 1) * 2 + 1);
  M = unique(val + 1, val + (N + 1) * 2 + 1) - val - 1;
  for (int i = 1; i <= N + 1; i++) {
    A[i] = lower_bound(val + 1, val + M + 1, A[i]) - val;
    B[i] = lower_bound(val + 1, val + M + 1, B[i]) - val;
  }
  sum = find(val + 1, val + M + 1, sum) - val;

  fill(sz + 1, sz + M + 1, 1);

  int ans = 0;
  for (int i = 1; i <= N; i++)
    if (A[i] != B[i]) {
      Union(A[i], B[i]);
      ans++;
    }
  for (int i = 1; i <= M; i++)
    if (Root(i) == i && sz[i] > 1)
      ans++;
  if (Root(sum) != sum || sz[sum] > 1)
    ans--;
  printf("%d\n", ans);

  return 0;
}
Problem p03690 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ran 111111
int n;
int a[ran], b[ran];

int N;
int A[ran], B[ran];

int n2;
map<int, int> Map;
int h(int x) {
  if (Map.count(x))
    return Map[x];
  Map[x] = n2;
  n2++;
  return Map[x];
}

vector<pair<int, int>> e;
int ace[ran];
int ancestor(int x) { return x - ace[x] ? ace[x] = ancestor(ace[x]) : x; }
int stat() {
  for (int i = 0; i < n2; i++)
    ace[i] = i;
  for (vector<pair<int, int>>::iterator it = e.begin(); it != e.end(); it++)
    ace[ancestor(it->first)] = ace[it->second];
  int res = 0;
  for (int i = 0; i < n2; i++)
    if (ancestor(i) == i)
      res++;
  return res;
}

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

  N = n + 1;
  for (int i = 0; i < N; i++) {
    A[i] = a[i];
    B[i] = b[i];
  }
  sort(A, A + N);
  sort(B, B + N);
  bool flag = true;
  for (int i = 0; i < N; i++)
    if (A[i] != B[i])
      flag = false;
  if (!flag) {
    puts("-1");
    return 0;
  }

  for (int i = 0; i < N; i++)
    if (a[i] != b[i]) {
      int t1 = h(a[i]);
      int t2 = h(b[i]);
      e.push_back(make_pair(t1, t2));
    }

  int res = e.size();

  if (a[N - 1] != b[N - 1]) {
    res += stat() - 2;
  } else {
    res += stat();
    if (Map.count(a[N - 1]))
      res--;
  }
  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ran 111111
int n;
int a[ran], b[ran];

int N;
int A[ran], B[ran];

int n2;
map<int, int> Map;
int h(int x) {
  if (Map.count(x))
    return Map[x];
  Map[x] = n2;
  n2++;
  return Map[x];
}

vector<pair<int, int>> e;
int ace[ran];
int ancestor(int x) { return x - ace[x] ? ace[x] = ancestor(ace[x]) : x; }
int stat() {
  for (int i = 0; i < n2; i++)
    ace[i] = i;
  for (vector<pair<int, int>>::iterator it = e.begin(); it != e.end(); it++)
    ace[ancestor(it->first)] = ancestor(it->second);
  int res = 0;
  for (int i = 0; i < n2; i++)
    if (ancestor(i) == i)
      res++;
  return res;
}

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

  N = n + 1;
  for (int i = 0; i < N; i++) {
    A[i] = a[i];
    B[i] = b[i];
  }
  sort(A, A + N);
  sort(B, B + N);
  bool flag = true;
  for (int i = 0; i < N; i++)
    if (A[i] != B[i])
      flag = false;
  if (!flag) {
    puts("-1");
    return 0;
  }

  for (int i = 0; i < N; i++)
    if (a[i] != b[i]) {
      int t1 = h(a[i]);
      int t2 = h(b[i]);
      e.push_back(make_pair(t1, t2));
    }

  int res = e.size();

  if (a[N - 1] != b[N - 1]) {
    res += stat() - 2;
  } else {
    res += stat();
    if (Map.count(a[N - 1]))
      res--;
  }
  printf("%d\n", res);
  return 0;
}
Problem p03690 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define fr(i, x, y) for (int i = x; i <= y; ++i)
#define LL long long
using namespace std;
const int N = 1e6 + 10;
map<int, int> qx;
int n, a[N], b[N], c[N], f[N], vis[N];

int find(int k) { return f[k] == k ? k : find(f[k]); }

void add(int x, int y) {
  int xx = find(x), yy = find(y);
  if (xx != yy) {
    f[xx] = yy;
    vis[yy] = 1;
  }
}

void fail() {
  printf("-1\n");
  exit(0);
}

int main() {
  scanf("%d", &n);
  fr(i, 1, n) {
    scanf("%d", &a[i]);
    qx[a[i]]++;
    a[n + 1] ^= a[i];
  }
  qx[a[n + 1]]++;
  fr(i, 1, n) {
    scanf("%d", &b[i]);
    qx[b[i]]--;
    if (qx[b[i]] < 0)
      fail();
    b[n + 1] ^= b[i];
  }
  ++n;
  fr(i, 1, n) c[i] = a[i];
  int ans = 0;
  sort(c + 1, c + 1 + n);
  int len = unique(c + 1, c + 1 + n) - c - 1;
  fr(i, 1, n) a[i] = lower_bound(c + 1, c + 1 + len, a[i]) - c;
  fr(i, 1, n) b[i] = lower_bound(c + 1, c + 1 + len, b[i]) - c;
  fr(i, 1, n) f[i] = i;
  fr(i, 1, n) {
    if (a[i] != b[i]) {
      add(a[i], b[i]);
      if (i != n)
        ans++;
    }
  }
  if (!ans)
    return printf("0\n"), 0;
  vis[a[n]] = vis[b[n]] = 1;
  fr(i, 1, n) if (f[i] == i && vis[i]) ans++;
  ans--;
  printf("%d\n", ans);
  return 0;
}
/*
4
0 1 2 3
1 0 3 2
*/

Fixed submission:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define fr(i, x, y) for (int i = x; i <= y; ++i)
#define LL long long
using namespace std;
const int N = 1e6 + 10;
map<int, int> qx;
int n, a[N], b[N], c[N], f[N], vis[N];

int find(int k) { return f[k] == k ? k : f[k] = find(f[k]); }

void add(int x, int y) {
  int xx = find(x), yy = find(y);
  if (xx != yy) {
    f[xx] = yy;
    vis[yy] = 1;
  }
}

void fail() {
  printf("-1\n");
  exit(0);
}

int main() {
  scanf("%d", &n);
  fr(i, 1, n) {
    scanf("%d", &a[i]);
    qx[a[i]]++;
    a[n + 1] ^= a[i];
  }
  qx[a[n + 1]]++;
  fr(i, 1, n) {
    scanf("%d", &b[i]);
    qx[b[i]]--;
    if (qx[b[i]] < 0)
      fail();
    b[n + 1] ^= b[i];
  }
  ++n;
  fr(i, 1, n) c[i] = a[i];
  int ans = 0;
  sort(c + 1, c + 1 + n);
  int len = unique(c + 1, c + 1 + n) - c - 1;
  fr(i, 1, n) a[i] = lower_bound(c + 1, c + 1 + len, a[i]) - c;
  fr(i, 1, n) b[i] = lower_bound(c + 1, c + 1 + len, b[i]) - c;
  fr(i, 1, n) f[i] = i;
  fr(i, 1, n) {
    if (a[i] != b[i]) {
      add(a[i], b[i]);
      if (i != n)
        ans++;
    }
  }
  if (!ans)
    return printf("0\n"), 0;
  vis[a[n]] = vis[b[n]] = 1;
  fr(i, 1, n) if (f[i] == i && vis[i]) ans++;
  ans--;
  printf("%d\n", ans);
  return 0;
}
/*
4
0 1 2 3
1 0 3 2
*/
Problem p03691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}
const int N = 401, M = 1e5 + 50;
bitset<N> f[N];
int a[N], b[N], vis[N];
int main() {
  int n = gi(), m = gi(), x, y;
  for (int i = 1; i <= m; ++i)
    a[i] = gi(), b[i] = gi();
  for (int i = 1; i <= n; ++i)
    f[i].set(i);
  for (int i = m; i >= 1; --i) {
    x = a[i], y = b[i];
    for (int t = 1; t <= n; ++t)
      if (f[t][x] || f[t][y]) {
        if (f[t][y])
          swap(x, y);
        if (f[t][y])
          vis[t] = 1;
        else
          f[t].set(y);
      }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      for (int j = i + 1; j <= n; ++j)
        if (!vis[j] && (f[i] & f[j]).count() == 0)
          ++ans;
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}
const int N = 401, M = 1e5 + 50;
bitset<N> f[N];
int a[M], b[M], vis[N];
int main() {
  int n = gi(), m = gi(), x, y;
  for (int i = 1; i <= m; ++i)
    a[i] = gi(), b[i] = gi();
  for (int i = 1; i <= n; ++i)
    f[i].set(i);
  for (int i = m; i >= 1; --i) {
    x = a[i], y = b[i];
    for (int t = 1; t <= n; ++t)
      if (f[t][x] || f[t][y]) {
        if (f[t][y])
          swap(x, y);
        if (f[t][y])
          vis[t] = 1;
        else
          f[t].set(y);
      }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      for (int j = i + 1; j <= n; ++j)
        if (!vis[j] && (f[i] & f[j]).count() == 0)
          ++ans;
  cout << ans;
  return 0;
}
Problem p03691 (C++)

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

using namespace std;

int N, M, ans, x[100009], y[100009];
short lst[100009][409];
bool ap[409];

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d %d", &N, &M);
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++)
      lst[i][j] = lst[i - 1][j];
    scanf("%d %d", &x[i], &y[i]);
    lst[i][x[i]] = lst[i][y[i]] = i;
  }
  for (int i = 1; i <= N; i++)
    for (int j = i + 1; j <= N; j++) {
      for (int k = 1; k <= N; k++)
        ap[k] = (k == i || k == j);
      priority_queue<pair<int, int>> PQ;
      PQ.push({lst[M][i], i}), PQ.push({lst[M][j], j});
      int lastConsidered = M + 1;
      bool ok = 1;
      while (!PQ.empty() && lastConsidered > 1) {
        pair<int, int> curr = PQ.top();
        PQ.pop();
        if (curr.first == 0)
          break;
        lastConsidered = curr.first;
        PQ.push({lst[curr.first - 1][curr.second], curr.second});
        int k = curr.first;
        if (ap[x[k]] && ap[y[k]]) {
          ok = 0;
          break;
        }
        if (ap[x[k]])
          ap[y[k]] = 1, PQ.push({lst[curr.first - 1][y[k]], y[k]});
        else if (ap[y[k]])
          ap[x[k]] = 1, PQ.push({lst[curr.first - 1][x[k]], x[k]});
        else {
          printf("WA\n");
          return 0;
        }
      }
      ans += (ok == 1);
      // if (ok)
      //   printf ("%d %d\n", i, j);
    }
  printf("%d\n", ans);
  return 0;
}


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

using namespace std;

int N, M, ans, x[100009], y[100009], lst[100009][409];
bool ap[409];

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d %d", &N, &M);
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++)
      lst[i][j] = lst[i - 1][j];
    scanf("%d %d", &x[i], &y[i]);
    lst[i][x[i]] = lst[i][y[i]] = i;
  }
  for (int i = 1; i <= N; i++)
    for (int j = i + 1; j <= N; j++) {
      for (int k = 1; k <= N; k++)
        ap[k] = (k == i || k == j);
      priority_queue<pair<int, int>> PQ;
      PQ.push({lst[M][i], i}), PQ.push({lst[M][j], j});
      int lastConsidered = M + 1;
      bool ok = 1;
      while (!PQ.empty() && lastConsidered > 1) {
        pair<int, int> curr = PQ.top();
        PQ.pop();
        if (curr.first == 0)
          break;
        lastConsidered = curr.first;
        PQ.push({lst[curr.first - 1][curr.second], curr.second});
        int k = curr.first;
        if (ap[x[k]] && ap[y[k]]) {
          ok = 0;
          break;
        }
        if (ap[x[k]])
          ap[y[k]] = 1, PQ.push({lst[curr.first - 1][y[k]], y[k]});
        else if (ap[y[k]])
          ap[x[k]] = 1, PQ.push({lst[curr.first - 1][x[k]], x[k]});
        else {
          printf("WA\n");
          return 0;
        }
      }
      ans += (ok == 1);
      // if (ok)
      //   printf ("%d %d\n", i, j);
    }
  printf("%d\n", ans);
  return 0;
}
Problem p03691 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
// using arr = array;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using ld = long double;
const int maxn = 1 << 18, mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
vpi g[maxn];
int ok, vis[maxn];
void dfs(int v, int t) {
  vis[v] = 1;
  for (auto &i : g[v]) {
    if (i.second >= t)
      continue;
    if (vis[i.first])
      ok = 0;
    if (ok)
      dfs(i.first, i.second);
  }
}
bool check(int u, int v) {
  memset(vis, 0, sizeof vis);
  ok = 1;
  dfs(u, 1 << 30);
  if (ok)
    dfs(v, 1 << 30);

  return ok;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int f, t, i = 0; i < m; i++) {
    cin >> f >> t;
    g[f].pb({t, i});
    g[t].pb({f, i});
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      ans += check(i, j);
  cout << ans;
}


Fixed submission:
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
// using arr = array;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using ld = long double;
const int maxn = 405, mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
vpi g[maxn];
int ok, vis[maxn];
void dfs(int v, int t) {
  vis[v] = 1;
  for (auto &i : g[v]) {
    if (i.second >= t)
      continue;
    if (vis[i.first])
      ok = 0;
    if (ok)
      dfs(i.first, i.second);
  }
}
bool check(int u, int v) {
  memset(vis, 0, sizeof vis);
  ok = 1;
  dfs(u, 1 << 30);
  if (ok)
    dfs(v, 1 << 30);

  return ok;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int f, t, i = 0; i < m; i++) {
    cin >> f >> t;
    g[f].pb({t, i});
    g[t].pb({f, i});
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      ans += check(i, j);
  cout << ans;
}
Problem p03691 (C++)

Buggy submission (status: Time Limit Exceeded):
// Written By NewbieChd
#include <bitset>
#include <cctype>
#include <cstdio>
using namespace std;

const int BUF = 1000000;
char buf[BUF], *p1, *p2;
inline char getChar() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  register int f = 0;
  char c;
  while (!isdigit(c = getChar())) {
  }
  do
    f = f * 10 + (c ^ 48);
  while (isdigit(c = getChar()));
  return f;
}

const int maxN = 403, maxM = 100003;
int a[maxM], b[maxM];
bitset<maxN> tur[maxN];
bool flag[maxN];

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
#endif
  int n = read(), m = read(), i, j, ans = 0;
  for (i = 1; i <= m; ++i)
    a[i] = read(), b[i] = read();
  for (i = 1; i <= n; ++i) {
    tur[i].set(i);
    for (j = m; j; --j) {
      if (tur[i][a[j]] && tur[i][b[j]]) {
        flag[i] = 1;
        break;
      }
      if (tur[i][a[j]])
        tur[i].set(b[j]);
      if (tur[i][b[j]])
        tur[i].set(a[j]);
    }
  }
  for (i = 1; i <= n; ++i)
    if (!flag[i])
      for (j = 1; j < i; ++j)
        if (!flag[j])
          ans += !((tur[i] & tur[j]).any());
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// Written By NewbieChd
#include <bitset>
#include <cctype>
#include <cstdio>
using namespace std;

const int BUF = 1000000;
char buf[BUF], *p1, *p2;
inline char getChar() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  register int f = 0;
  char c;
  while (!isdigit(c = getChar())) {
  }
  do
    f = f * 10 + (c ^ 48);
  while (isdigit(c = getChar()));
  return f;
}

const int maxN = 403, maxM = 100003;
int a[maxM], b[maxM];
bitset<maxN> tur[maxN];
bool flag[maxN];

int main() {
  int n = read(), m = read(), i, j, ans = 0;
  for (i = 1; i <= m; ++i)
    a[i] = read(), b[i] = read();
  for (i = 1; i <= n; ++i) {
    tur[i].set(i);
    for (j = m; j; --j) {
      if (tur[i][a[j]] && tur[i][b[j]]) {
        flag[i] = 1;
        break;
      }
      if (tur[i][a[j]])
        tur[i].set(b[j]);
      if (tur[i][b[j]])
        tur[i].set(a[j]);
    }
  }
  for (i = 1; i <= n; ++i)
    if (!flag[i])
      for (j = 1; j < i; ++j)
        if (!flag[j])
          ans += !((tur[i] & tur[j]).any());
  printf("%d\n", ans);
  return 0;
}
Problem p03691 (C++)

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

const int MAXN = 15;
int n, m, xs[MAXN], ys[MAXN], ans;
std::bitset<MAXN> B[MAXN];
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> n >> m;
  for (int i = 1; i <= m; ++i)
    std::cin >> xs[i] >> ys[i];
  for (int i = 1; i <= n; ++i) {
    B[i].set(i);
    for (int j = m; j; --j)
      if (B[i][xs[j]] && B[i][ys[j]])
        B[i].set();
      else if (B[i][xs[j]])
        B[i].set(ys[j]);
      else if (B[i][ys[j]])
        B[i].set(xs[j]);
    B[i].reset(0);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      ans += (B[i] & B[j]).none();
  std::cout << ans << std::endl;
  return 0;
}


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

const int MAXN = 410;
int n, m, xs[100010], ys[100010], ans;
std::bitset<MAXN> B[MAXN];
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> n >> m;
  for (int i = 1; i <= m; ++i)
    std::cin >> xs[i] >> ys[i];
  for (int i = 1; i <= n; ++i) {
    B[i].set(i);
    for (int j = m; j; --j)
      if (B[i][xs[j]] && B[i][ys[j]])
        B[i].set();
      else if (B[i][xs[j]])
        B[i].set(ys[j]);
      else if (B[i][ys[j]])
        B[i].set(xs[j]);
    B[i].reset(0);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      ans += (B[i] & B[j]).none();
  std::cout << ans << std::endl;
  return 0;
}
Problem p03691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

const int N = 410, M = 1e5 + 10;

bool c[N][N], bad[N];
pii e[N];

int32_t main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    e[i] = {u, v};
  }

  for (int i = 1; i <= n; i++) {
    c[i][i] = 1;
    for (int j = m - 1; j >= 0; j--) {
      int u = e[j].F, v = e[j].S;
      if (c[i][u] && c[i][v]) {
        bad[i] = true;
      } else if (!c[i][u] && !c[i][v])
        continue;
      else if (c[i][u] && !c[i][v])
        c[i][v] = true;
      else
        c[i][u] = true;
    }
  }

  int ans = 0;

  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      if (bad[i] || bad[j])
        continue;
      if (c[i][j] || c[j][i])
        continue;
      bool no = false;
      for (int k = 1; k <= n; k++)
        if (c[i][k] && c[j][k])
          no = true;
      if (!no)
        ans++;
    }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

const int N = 410, M = 1e5 + 10;

bool c[N][N], bad[N];
pii e[M];

int32_t main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    e[i] = {u, v};
  }

  for (int i = 1; i <= n; i++) {
    c[i][i] = 1;
    for (int j = m - 1; j >= 0; j--) {
      int u = e[j].F, v = e[j].S;
      if (c[i][u] && c[i][v]) {
        bad[i] = true;
      } else if (!c[i][u] && !c[i][v])
        continue;
      else if (c[i][u] && !c[i][v])
        c[i][v] = true;
      else
        c[i][u] = true;
    }
  }

  int ans = 0;

  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      if (bad[i] || bad[j])
        continue;
      if (c[i][j] || c[j][i])
        continue;
      bool no = false;
      for (int k = 1; k <= n; k++)
        if (c[i][k] && c[j][k])
          no = true;
      if (!no)
        ans++;
    }

  cout << ans << endl;
}