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

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int N, r = 0;
  scanf("%d", &N);
  vector<vector<int>> v(N + 1);
  v[0].resize(N);
  for (int i = 1; i <= N; i++) {
    v[i].resize(N + 1);
    for (int j = 1; j <= N; j++)
      scanf("%d", &v[i][j]);
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= N; j++)
      v[i + 1][j] += v[i][j];
  for (int i = 0; i <= N; i++)
    for (int j = 0; j < N; j++)
      v[i][j + 1] += v[i][j];
  for (int i0 = 0; i0 < N; i0++)
    for (int i1 = i0 + 1; i1 <= N; i1++) {
#if 1
      int r0 = v[i0][0] - v[i1][0] - v[i0][1] + v[i1][1], r1 = r0;
      int s = 0;
      if (N > 1)
        s = v[i0][1] - v[i1][1] - v[i0][2] + v[i1][2];
      r1 = max(r1, r0 + s);
      for (int k = 2; k < N; k++) {
        r0 += v[i0][k - 1] - v[i0 + 1][k - 1] - v[i0][k] + v[i0 + 1][k];
        if (i1 - i0 > 1)
          r0 += v[i1 - 1][k - 1] - v[i1][k - 1] - v[i1 - 1][k] + v[i1][k];
        r0 = max(r0, v[i0][k - 1] - v[i1][k - 1] - v[i0][k] + v[i1][k]);
        s = v[i0][k] - v[i1][k] - v[i0][k + 1] + v[i1][k + 1];
        r1 = max(r1, r0 + s);
      }
      // printf("%d %d %d\\n",i0,i1,r1);
      r = max(r, r1);
#else
      for (int j0 = 0; j0 < N; j0++)
        for (int j1 = j0 + 1; j1 <= N; j1++) {
          int n = v[i0][j0] - v[i1][j0] - v[i0][j1] + v[i1][j1];
          int m = 0;
          if (i1 - i0 > 2 && j1 - j0 > 2)
            m = v[i0 + 1][j0 + 1] - v[i1 - 1][j0 + 1] - v[i0 + 1][j1 - 1] +
                v[i1 - 1][j1 - 1];
          r = max(r, n - m);
        }
#endif
    }
  printf("%d\\n", r);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int N, r = 0;
  scanf("%d", &N);
  vector<vector<int>> v(N + 1);
  v[0].resize(N);
  for (int i = 1; i <= N; i++) {
    v[i].resize(N + 1);
    for (int j = 1; j <= N; j++)
      scanf("%d", &v[i][j]);
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= N; j++)
      v[i + 1][j] += v[i][j];
  for (int i = 0; i <= N; i++)
    for (int j = 0; j < N; j++)
      v[i][j + 1] += v[i][j];
  for (int i0 = 0; i0 < N; i0++)
    for (int i1 = i0 + 1; i1 <= N; i1++)
      for (int j0 = 0; j0 < N; j0++)
        for (int j1 = j0 + 1; j1 <= N; j1++) {
          int n = v[i0][j0] - v[i1][j0] - v[i0][j1] + v[i1][j1];
          int m = 0;
          if (i1 - i0 > 2 && j1 - j0 > 2)
            m = v[i0 + 1][j0 + 1] - v[i1 - 1][j0 + 1] - v[i0 + 1][j1 - 1] +
                v[i1 - 1][j1 - 1];
          r = max(r, n - m);
        }
  printf("%d\\n", r);
}
Problem p00307 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

#define MP make_pair
#define PB push_back
#define PI 3.141592653589793238462643383279L

#define MOD 1000000007LL
typedef long long ll;
typedef long double ld;

#define LAR(a, b) a = max(a, (b))
#define SQ(x) ((x) * (x))

int w[212345], x[212345];
int ms[212345], xs[212345];
int wb[20], xb[20];
int n, m;

typedef map<int, int> mii;
typedef pair<int, int> pii;
#define itr iterator

int ama(int a, int b) {
  int lo = a, hg = n + 1;
  while (hg - lo > 1) {
    int md = (lo + hg) / 2;
    if (ms[md] - ms[a] <= wb[b] && xs[md] - xs[a] <= xb[b])
      lo = md;
    else
      hg = md;
  }
  return lo;
}

vector<pii> v;
int ans[65536];

int main() {
  scanf("%d%d", &n, &m);
  REP(i, n) scanf("%d%d", w + i, x + i);
  REP(i, m) scanf("%d%d", wb + i, xb + i);
  ms[0] = xs[0] = 0;
  REP(i, n) {
    ms[i + 1] = ms[i] + w[i];
    xs[i + 1] = xs[i] + x[i];
  }
  REP(i, 1 << m) {
    int cnt = 0;
    REP(j, m) if (i & (1 << j)) cnt++;
    v.PB(MP(cnt, i));
  }
  fill(ans, ans + 65536, 0);
  sort(v.begin(), v.end());
  REP(h, v.size()) {
    int i = v[h].second;
    REP(j, m) {
      if ((i & (1 << j)) == 0) {
        LAR(ans[i | (1 << j)], ama(ans[i], j));
      }
    }
  }
  int ret = 0;
  REP(i, 65536) LAR(ret, ans[i]);
  printf("%d\\n", ret);
  return 0;
}

Error: TLE

Fixed submission:

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

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

#define PB push_back
#define PI 3.141592653589793238462643383279L

#define MOD 1000000007LL
typedef long long ll;
typedef long double ld;

#define LAR(a, b) a = max(a, (b))
#define SQ(x) ((x) * (x))

int w[212345], x[212345];
int ms[212345], xs[212345];
int wb[20], xb[20];
int n, m;

typedef map<int, int> mii;
#define itr iterator

map<int, int> memo[65536];
int ama(int, int);
int DP(int, int);
int dp(int a, int b) {
  mii::itr it = memo[a].find(b);
  if (it != memo[a].end())
    return it->second;
  else
    return memo[a][b] = DP(a, b);
}
int DP(int a, int b) {
  int ret = b;
  REP(i, m) {
    if ((a & (1 << i)) == 0) {
      LAR(ret, dp(a | (1 << i), ama(b, i)));
    }
  }
  return ret;
}
int ama(int a, int b) {
  int lo = a, hg = n + 1;
  while (hg - lo > 1) {
    int md = (lo + hg) / 2;
    if (ms[md] - ms[a] <= wb[b] && xs[md] - xs[a] <= xb[b])
      lo = md;
    else
      hg = md;
  }
  return lo;
}
int main() {
  scanf("%d%d", &n, &m);
  REP(i, n) scanf("%d%d", w + i, x + i);
  REP(i, m) scanf("%d%d", wb + i, xb + i);
  ms[0] = xs[0] = 0;
  REP(i, n) {
    ms[i + 1] = ms[i] + w[i];
    xs[i + 1] = xs[i] + x[i];
  }
  printf("%d\\n", dp(0, 0));
}
Problem p00307 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int M, N;
  int64 w[200001] = {}, t[200002] = {};
  int c[15], b[15];
  int dp[1 << 15] = {};

  scanf("%d %d", &M, &N);
  for (int i = 1; i <= M; i++) {
    scanf("%lld %lld", &w[i], &t[i]);
    w[i] += w[i - 1];
    t[i] += t[i - 1];
  }
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &c[i], &b[i]);
  }
  for (int i = 0; i < 1 << N; i++) {
    for (int j = 0; j < N; j++) {
      if ((i >> j) & 1)
        continue;
      int weightHigh =
          upper_bound(w + dp[i], w + M + 1, w[dp[i]] + c[j]) - w - 1;
      int widthHigh =
          upper_bound(t + dp[i], t + M + 1, t[dp[i]] + b[j]) - t - 1;
      dp[i | (1 << j)] = max(dp[i | (1 << j)], min(weightHigh, widthHigh));
    }
  }
  printf("%d\\n", dp[(1 << N) - 1]);
}

Error: 0

Fixed submission:

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

int main() {
  int M, N;
  int64 w[200001] = {}, t[200001] = {};
  int c[15], b[15];
  int dp[1 << 15];

  scanf("%d %d", &M, &N);
  for (int i = 1; i <= M; i++) {
    scanf("%lld %lld", &w[i], &t[i]);
    w[i] += w[i - 1];
    t[i] += t[i - 1];
  }
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &c[i], &b[i]);
  }
  for (int i = 0; i < 1 << N; i++) {
    for (int j = 0; j < N; j++) {
      if ((i >> j) & 1)
        continue;
      int weightHigh =
          upper_bound(w + dp[i], w + M + 1, w[dp[i]] + c[j]) - w - 1;
      int widthHigh =
          upper_bound(t + dp[i], t + M + 1, t[dp[i]] + b[j]) - t - 1;
      dp[i | (1 << j)] = max(dp[i | (1 << j)], min(weightHigh, widthHigh));
    }
  }
  printf("%d\\n", dp[(1 << N) - 1]);
}
Problem p00312 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main(void) {
  int time, D, L;

  scanf("%d %d", &D, &L);

  time = D / L + D % L;

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

  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
int main(void) {
  int time, D, L;
  scanf("%d %d", &D, L);

  time = D / L + D % L;

  printf("%d", time);

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
  int n;
  cin >> n;
  int X, Y, Z;
  cin >> X;
  vector<int> x(X);
  for (int i = 0; i < X; i++)
    cin >> x[i];
  cin >> Y;
  vector<int> y(Y);
  for (int i = 0; i < Y; i++)
    cin >> y[i];
  cin >> Z;
  vector<int> z(Z);
  for (int i = 0; i < Z; i++)
    cin >> z[i];

  set<int> s;

  for (int i = 0; i < Z; i++) {
    s.insert(z[i]);
  }

  for (int i = 0; i < X; i++) {
    s.erase(x[i]);
  }

  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < Z; j++) {
      if (y[i] == z[j]) {
        s.insert(y[i]);
        break;
      }
    }
  }

  cout << s.size() << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
  int n;
  cin >> n;
  int X, Y, Z;
  vector<int> x(X), y(Y), z(Z);
  cin >> X;
  for (int i = 0; i < X; i++)
    cin >> x[i];
  cin >> Y;
  for (int i = 0; i < Y; i++)
    cin >> y[i];
  cin >> Z;
  for (int i = 0; i < Z; i++)
    cin >> z[i];

  set<int> s;

  for (int i = 0; i < Z; i++) {
    s.insert(z[i]);
  }

  for (int i = 0; i < X; i++) {
    s.erase(x[i]);
  }

  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < Z; j++) {
      if (y[i] == z[j]) {
        s.insert(y[i]);
        break;
      }
    }
  }

  cout << s.size() << endl;
}
Problem p00313 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a_po;
  cin >> a_po;
  int a[a_po];
  int count = 0;
  for (int i = 0; a_po > i; i++) {
    cin >> a[i];
  }
  int b_po;
  cin >> b_po;
  int b[b_po];
  for (int i = 0; b_po > i; i++) {
    cin >> b[i];
  }
  int c_po;
  cin >> c_po;
  int c[c_po];
  for (int i = 0; c_po > i; i++) {
    bool have = false;
    bool bs = false;
    bool a_s = false;
    cin >> c[i];
    for (int j = 0; a_po > j; j++) {
      if (c[i] == a[j]) {
        a_s = true;
        for (int k = 0; b_po > k; k++) {
          if (c[i] == b[k]) {
            have = true;
            bs = true;
            break;
          }
        }
        if (bs) {
          break;
        }
      }
    }
    if (!a_s) {
      have = true;
    }
    if (have)
      count++;
  }
  cout << count << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a_po;
  int a[a_po];
  int count = 0;
  cin >> a_po;
  for (int i = 0; a_po > i; i++) {
    cin >> a[i];
  }
  int b_po;
  cin >> b_po;
  int b[b_po];
  for (int i = 0; b_po > i; i++) {
    cin >> b[i];
  }
  int c_po;
  cin >> c_po;
  int c[c_po];
  for (int i = 0; c_po > i; i++) {
    bool have = false;
    bool bs = false;
    bool a_s = false;
    cin >> c[i];
    for (int j = 0; a_po > j; j++) {
      if (c[i] == a[j]) {
        a_s = true;
        for (int k = 0; b_po > k; k++) {
          if (c[i] == b[k]) {
            have = true;
            bs = true;
            break;
          }
        }
        if (bs) {
          break;
        }
      }
    }
    if (!a_s) {
      have = true;
    }
    if (have)
      count++;
  }
  cout << count << endl;
}
Problem p00313 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {
  int N, X, Y, Z;
  int tmp;
  cin >> N;
  cin >> X;
  int A_m[X];
  for (int i = 0; i < X; ++i) {
    cin >> A_m[i];
  }
  cin >> Y;
  int B_m[Y];
  for (int i = 0; i < Y; ++i) {
    cin >> B_m[i];
  }
  cin >> Z;
  int C_m[Z];
  for (int i = 0; i < Z; ++i) {
    cin >> C_m[i];
  }
  set<int> AC;
  set<int> BC;
  set<int> Answer;
  for (int i = 0; i < Z; ++i) {
    AC.insert(C_m[i]);
  }
  for (int i = 0; i < X; ++i) {
    AC.erase(A_m[i]);
  }
  for (int i = 0; i < Y; ++i) {
    for (int j = 0; j < Z; ++j) {
      if (B_m[i] == C_m[j]) {
        BC.insert(B_m[i]);
      }
    }
  }
  set_union(AC.begin(), AC.end(), BC.begin(), BC.end(),
            inserter(Answer, Answer.end()));
  cout << Answer.size() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main() {
  int N, X, Y, Z;
  int tmp;
  cin >> N;
  cin >> X;
  int A_m[X];
  for (int i = 0; i < X; i++) {
    cin >> A_m[i];
  }
  cin >> Y;
  int B_m[Y];
  for (int i = 0; i < Y; i++) {
    cin >> B_m[i];
  }
  cin >> Z;
  int C_m[Z];
  for (int i = 0; i < Z; i++) {
    cin >> C_m[i];
  }
  set<int> AC;
  set<int> BC;
  set<int> Answer;
  for (int i = 0; i < Z; i++) {
    AC.insert(C_m[i]);
  }
  for (int i = 0; i < X; i++) {
    AC.erase(A_m[i]);
  }
  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < Z; Z++) {
      if (B_m[i] == C_m[j]) {
        BC.insert(B_m[i]);
      }
    }
  }
  set_union(AC.begin(), AC.end(), BC.begin(), BC.end(),
            inserter(Answer, Answer.end()));
  cout << Answer.size() << endl;
  return 0;
}
Problem p00314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int &t : p)
    cin >> t;
  for (int A = 100; A >= 0; A--) {
    int c = 0;
    for (int t : p)
      if (t >= A)
        c++;
    if (c >= A) {
      cout << A << endl;
      break;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int &t : p)
    cin >> t;
  for (int A = 100; A >= 0; A--) {
    int c = 0;
    for (int t : p)
      if (t >= A)
        c++;
    if (c >= A) {
      cout << A << endl;
      break;
    }
  }
}
Problem p00314 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  int ma = 0;
  for (int a = 0; a < n; a++) {
    int b;
    cin >> b;
    vec.push_back(b);
    ma = max(ma, b);
  }
  for (int a = ma; a >= 0; a--) {
    int ins = 0;
    for (int b = 0; b < n; b++) {
      if (vec.at(b) >= a) {
        ins++;
      }
    }
    if (ins >= a) {
      cout << a << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}


Error: 4

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  int ma = 0;
  for (int a = 0; a < n; a++) {
    int b;
    cin >> b;
    vec.push_back(b);
    ma = max(ma, b);
  }
  for (int a = ma; a >= 0; a--) {
    int ins = 0;
    for (int b = 0; b < n; b++) {
      if (vec.at(b) >= a) {
        ins++;
      }
    }
    if (ins >= a) {
      cout << a << endl;
      return a;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p00315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int c, n;
  cin >> c >> n;
  int m[1000][1000];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      m[i][j] = (c == '0' ? 0 : 1);
    }
  }
  set<int> s;
  for (int i = 0; i < (n + 1) / 2; i++) {
    for (int j = 0; j < (n + 1) / 2; j++) {
      if (m[i][j] != m[n - 1 - i][j] || m[i][j] != m[n - 1 - i][n - 1 - j] ||
          m[i][j] != m[i][n - 1 - j]) {
        s.insert(i * n + j);
      }
    }
  }
  int ans = 0;
  if (s.empty()) {
    ans++;
  }
  c--;
  while (c--) {
    int d;
    cin >> d;
    set<int> e;
    for (int i = 0; i < d; i++) {
      int r, c;
      cin >> r >> c;
      r--;
      c--;
      m[r][c] ^= 1;
      e.insert(min(r, n - 1 - r) * n + min(c, n - 1 - c));
    }
    for (set<int>::iterator i = e.begin(); i != e.end(); i++) {
      int h = *i;
      int x = h / n, y = h % n;
      if (m[x][y] == m[x][n - 1 - y] && m[x][y] == m[n - 1 - x][y] &&
          m[x][y] == m[n - 1 - x][n - 1 - y]) {
        s.erase(h);
      } else {
        s.insert(h);
      }
    }
    if (s.empty()) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int c, n;
  int m[1000][1000];
  cin >> c >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      m[i][j] = (c == '0' ? 0 : 1);
    }
  }
  int ans = 0;
  set<int> s;
  for (int i = 0; i < (n + 1) / 2; i++) {
    for (int j = 0; j < (n + 1) / 2; j++) {
      if (m[i][j] != m[n - 1 - i][j] || m[i][j] != m[n - 1 - i][n - 1 - j] ||
          m[i][j] != m[i][n - 1 - j]) {
        s.insert(i * n + j);
      }
    }
  }
  if (s.empty()) {
    ans++;
  }
  c--;
  while (c--) {
    int d;
    cin >> d;
    for (int i = 0; i < d; i++) {
      int r, c;
      cin >> r >> c;
      r--;
      c--;
      m[r][c] ^= 1;
      s.insert(min(r, n - 1 - r) * n + min(c, n - 1 - c));
    }
    for (set<int>::iterator i = s.begin(); i != s.end(); i++) {
      int h = *i;
      int a = h / n, b = h % n;
      if (m[a][b] == m[a][n - 1 - b] && m[a][b] == m[n - 1 - a][n - 1 - b] &&
          m[a][b] == m[n - 1 - a][b]) {
        s.erase(h);
      }
    }
    if (s.empty()) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

short table[1000][1000];

int main() {

  int c, n, ans = 0, N, count = 0;
  char cha;

  cin >> c >> n;

  N = n - 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cha;
      table[i][j] = (cha == '0') ? 0 : 1;
    }
  }

  for (int i = 0; i < n / 2; i++) {
    for (int j = 0; j < n / 2; j++) {
      if ((table[i][j] + table[i][N - j] + table[N - i][j] +
           table[N - i][N - j]) %
              4 !=
          0) {
        count++;
      }
    }
  }

  if (count == 0) {
    ans++;
  }

  int d, w, h;

  while (--c) {

    bool beforeok;

    cin >> d;

    for (int i = 0; i < d; i++) {
      cin >> w >> h;
      w--;
      h--;
      if ((table[w][h] + table[w][N - h] + table[N - w][h] +
           table[N - w][N - h]) %
              4 !=
          0) {
        beforeok = false;
      } else {
        beforeok = true;
      }
      table[w][h] = (table[w][h] == 0) ? 1 : 0;

      if ((table[w][h] + table[w][N - h] + table[N - w][h] +
           table[N - w][N - h]) %
              4 !=
          0) {
        if (beforeok) {
          count++;
        }
      } else {
        count--;
      }
    }

    if (count == 0) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int c, n, d, x, y, count, N;

  count = 0;

  cin >> c >> n;

  N = n - 1;

  bool table[n][n];

  char cha;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cha;
      table[i][j] = cha == '0' ? 0 : 1;
    }
  }

  bool flag = true;

  for (int i = 0; i < n / 2; i++) {
    for (int j = 0; j < n / 2; j++) {
      if ((table[i][j] + table[i][N - j] + table[N - i][j] +
           table[N - i][N - j]) %
              4 !=
          0) {
        flag = false;
        break;
      }
    }
    if (!flag) {
      break;
    }
  }

  if (flag) {
    count++;
  }

  for (int k = 0; k < c - 1; k++) {

    cin >> d;

    for (int i = 0; i < d; i++) {
      cin >> x >> y;
      table[x - 1][y - 1] = !table[x - 1][y - 1];
    }

    flag = true;

    for (int i = 0; i < n / 2; i++) {
      for (int j = 0; j < n / 2; j++) {
        if ((table[i][j] + table[i][N - j] + table[N - i][j] +
             table[N - i][N - j]) %
                4 !=
            0) {
          flag = false;
          break;
        }
      }
      if (!flag) {
        break;
      }
    }

    if (flag) {
      count++;
    }
  }

  cout << count << endl;

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

#include <assert.h>
#include <bits/stdc++.h>

using namespace std;

bool ok[1000][1000];

bool check(vector<string> &v, int n, int x, int y) {
  if (v[x][y] == v[n - x - 1][y] && v[x][y] == v[x][n - y - 1] &&
      v[x][y] == v[n - x - 1][n - y - 1])
    return true;
  return false;
}

int main() {
  int c, n;
  cin >> c >> n;
  vector<string> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  int def = 0;
  for (int i = 0; i < n / 2; i++)
    for (int j = 0; j < n / 2; j++)
      if (check(p, n, i, j))
        ok[i][j] = true;
      else {
        ok[i][j] = false;
        def++;
      }
  int ans = (def == 0 ? 1 : 0);
  while (--c) {
    int d, pr[100], pc[100];
    cin >> d;
    for (int i = 0; i < d; i++)
      cin >> pr[i] >> pc[i];
    for (int i = 0; i < d; i++) {
      pr[i]--;
      pc[i]--;
      p[pr[i]][pc[i]] = (p[pr[i]][pc[i]] == '1' ? '0' : '1');
      if (pr[i] >= n / 2)
        pr[i] = n - 1 - pr[i];
      if (pc[i] >= n / 2)
        pc[i] = n - 1 - pc[i];
      bool s = check(p, n, pr[i], pc[i]);
      if (s != ok[pr[i]][pc[i]]) {
        if (s)
          def--;
        else
          def++;
      }
      ok[pr[i]][pc[i]] = s;
    }
    if (def == 0)
      ans++;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <assert.h>
#include <bits/stdc++.h>

using namespace std;

int check(vector<string> &v, int n) {
  for (int i = 0; i < n / 2; i++) {
    string tmp = v[i];
    reverse(tmp.begin(), tmp.end());
    if (v[i] != tmp || v[i] != v[n - i - 1])
      return 0;
  }

  return 1;
}

int main() {
  int co, n;
  vector<string> p;
  cin >> co >> n;
  p.resize(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  int ans = check(p, n);
  for (int i = 0; i < co - 1; i++) {
    int d;
    cin >> d;
    for (int j = 0; j < d; j++) {
      int r, c;
      cin >> r >> c;
      p[r - 1][c - 1] = (p[r - 1][c - 1] == '1' ? '0' : '1');
    }

    ans += check(p, n);
  }
  cout << ans << endl;

  return 0;
}