Problem p00315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define vec(x) vector<x>
ll gcd(ll a, ll b) {
  while (b) {
    ll tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define rep(x, y) for (int x = 0; x < (y); x++)
#define rrep(x, y) for (int x = (y); x >= 0; x--)
#define REP(x, z, y) for (int x = (z); x < (y); x++)
#define RREP(x, z, y) for (int x = (y); x >= (z); x--)
#define all(x) x.begin(), x.end()
#define split_str(str, sp_word)                                                \\
  istringstream stream(str);                                                   \\
  string res;                                                                  \\
  for (int cnt = 0; getline(stream, res, sp_word); cnt++)
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define out(x)                                                                 \\
  for (auto i : x)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define outln(x)                                                               \\
  for (auto i : x)                                                             \\
    cout << i << " " << endl;
#define vec_cpy(to, from) copy(all(to), back_inserter(from))
#define ary_cpy(to, from) memcpy(to, from, sizeof from)
#define MOD = 1000000007;
#ifdef DEBUG
#define debug_echo(e) cout << "|" << __LINE__ << "| " << e << endl
#define debug_var(e) cout << "|" << __LINE__ << "| " << #e << ": " << e << endl
#define debug_echo_dp(dp, N)                                                   \\
  {                                                                            \\
    cout << "[ ";                                                              \\
    rep(cnt, N) cout << dp[cnt] << " ";                                        \\
    cout << "]" << endl;                                                       \\
  }
#else
#define debug_echo(e)
#define debug_var(e)
#define debug_echo_dp(dp, N)
#endif

int main() {
  int C, n, d, r, c, ans = 0;
  cin >> C >> n;
  int harf = n / 2;
  int h_cnt = 0;
  int w_cnt = 0;
  vec(bool) h[harf], w[n], h_goal(n, true), w_goal(harf, true);
  str p[n];

  rep(i, n) cin >> p[i];

  //????????????????????¨
  rep(i, harf) rep(j, n) {
    h[i].pb(p[i][j] == p[n - i - 1][j]);
    if (p[i][j] != p[n - i - 1][j])
      h_cnt++;
  }

  //?¨??????????????????¨
  rep(i, n) rep(j, harf) {
    w[i].pb(p[i][j] == p[i][n - j - 1]);
    if (p[i][j] != p[i][n - j - 1])
      w_cnt++;
  }

  if (w_cnt == 0 && h_cnt == 0)
    ans++;

  rep(i, C - 1) {
    cin >> d;
    rep(i, d) {
      cin >> r >> c;
      r--;
      c--;

      if (h[(r >= harf ? n - r - 1 : r)][c])
        h_cnt++;
      else
        h_cnt--;

      if (w[r][(c >= harf ? n - c - 1 : c)])
        w_cnt++;
      else
        w_cnt--;

      //????????¨???????????????????????¢
      h[(r >= harf ? n - r - 1 : r)][c] = !h[(r >= harf ? n - r - 1 : r)][c];
      w[r][(c >= harf ? n - c - 1 : c)] = !w[r][(c >= harf ? n - c - 1 : c)];
    }

    if (h_cnt == 0 && w_cnt == 0)
      ans++;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define vec(x) vector<x>
ll gcd(ll a, ll b) {
  while (b) {
    ll tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define rep(x, y) for (int x = 0; x < (y); x++)
#define rrep(x, y) for (int x = (y); x >= 0; x--)
#define REP(x, z, y) for (int x = (z); x < (y); x++)
#define RREP(x, z, y) for (int x = (y); x >= (z); x--)
#define all(x) x.begin(), x.end()
#define split_str(str, sp_word)                                                \\
  istringstream stream(str);                                                   \\
  string res;                                                                  \\
  for (int cnt = 0; getline(stream, res, sp_word); cnt++)
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define out(x)                                                                 \\
  for (auto i : x)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define outln(x)                                                               \\
  for (auto i : x)                                                             \\
    cout << i << " " << endl;
#define vec_cpy(to, from) copy(all(to), back_inserter(from))
#define ary_cpy(to, from) memcpy(to, from, sizeof from)
#define MOD = 1000000007;
#ifdef DEBUG
#define debug_echo(e) cout << "|" << __LINE__ << "| " << e << endl
#define debug_var(e) cout << "|" << __LINE__ << "| " << #e << ": " << e << endl
#define debug_echo_dp(dp, N)                                                   \\
  {                                                                            \\
    cout << "[ ";                                                              \\
    rep(cnt, N) cout << dp[cnt] << " ";                                        \\
    cout << "]" << endl;                                                       \\
  }
#else
#define debug_echo(e)
#define debug_var(e)
#define debug_echo_dp(dp, N)
#endif

int main() {
  int C, n, d, r, c, ans = 0;
  cin >> C >> n;
  int harf = n / 2;
  // debug_var(harf);
  vec(bool) h[harf], w[n], h_goal(n, true), w_goal(harf, true);
  // rep(i,harf) h[i].resize(n);
  // rep(i,n) w[i].resize(harf);
  str p[n];

  rep(i, n) cin >> p[i];

  function<bool(vec(bool) *, vec(bool) *)> is_match = [&](vec(bool) * h,
                                                          vec(bool) * w) {
    bool h_result;
    bool w_result;
    rep(i, harf) if (h[i] != h_goal) return false;

    rep(i, n) if (w[i] != w_goal) return false;

    return true;
  };

  //????????????????????¨
  rep(i, harf) {
    rep(j, n) h[i].pb(p[i][j] == p[n - i - 1][j]);
    // debug_echo_dp(h[i],h[i].size());
  }

  //?¨??????????????????¨
  rep(i, n) {
    rep(j, harf) w[i].pb(p[i][j] == p[i][n - j - 1]);
    // debug_echo_dp(w[i],w[i].size());
  }

  if (is_match(h, w))
    ans++;

  rep(i, C - 1) {
    cin >> d;
    rep(i, d) {
      cin >> r >> c;
      r--;
      c--;
      // debug_echo(r << " - " << c);
      // debug_echo("h " << (r >= harf ? n - r - 1 : r) << " : " << c << " in "
      // << !h[(r >= harf ? n - r - 1 : r)][c]);
      h[(r >= harf ? n - r - 1 : r)][c] = !h[(r >= harf ? n - r - 1 : r)][c];
      // debug_echo("w " << r << " : " << (c >= harf ? n - c - 1 : c) << " " <<
      // !w[r][(c >= harf ? n - c - 1 : c)]);
      w[r][(c >= harf ? n - c - 1 : c)] = !w[r][(c >= harf ? n - c - 1 : c)];
    }

    if (is_match(h, w))
      ans++;
  }

  cout << ans << endl;
}