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++) {
      /*int tmp = (table[i][j] + table[i][N-j] + table[N-i][j] +
      table[N-i][N-j]); cout << tmp << endl;*/
      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;

    if (d != 0) {

      for (int i = 0; i < d; i++) {
        cin >> w >> h;
        w--;
        h--;
        /*cout << w*N+h << endl;
        cout << w*N+(N-h) << endl;
        cout << (N-w)*N+h << endl;
        cout << (N-w)*N+(N-h) << endl;*/
        /*cout << table[w][h] << endl;
        cout << table[w][N-h] << endl;
        cout << table[N-w][h] << endl;
        cout << table[N-w][N-h] << endl;    */
        // cout << (table[w][h] + table[w][N-h] + table[N-w][h] +
        // table[N-w][N-h]) % 4 << endl;
        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--;
        }
      }
    }

    // cout << "count = " << count << endl;

    if (count == 0) {
      // cout << "ok" << endl;
      ans++;
    }

    /*for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
          cout << table[i][j];
      }
      cout << endl;
    }*/
  }

  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;
}