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

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

vector<vector<int>> coaster;
vector<vector<pair<int, int>>> difs;
int main() {
  int C, N;
  cin >> C >> N;
  {
    vector<vector<int>> field(N, vector<int>(N));
    for (int y = 0; y < N; ++y) {
      string st;
      cin >> st;
      for (int x = 0; x < N; ++x) {
        field[y][x] = st[x] - '0';
      }
    }
    coaster = field;
    difs.resize(C - 1);
    for (int i = 0; i < C - 1; ++i) {
      int D;
      cin >> D;
      while (D--) {
        int y, x;
        scanf("%d %d", &y, &x);
        y--;
        x--;
        difs[i].push_back(make_pair(y, x));
      }
    }
  }
  vector<vector<vector<int>>> vs(N / 2,
                                 vector<vector<int>>(N / 2, vector<int>(4)));
  int sum = 0;
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      if (y >= N / 2 && x >= N / 2)
        vs[N - y - 1][N - x - 1][3] = coaster[y][x];
      if (y >= N / 2 && x < N / 2)
        vs[N - y - 1][x][2] = coaster[y][x];
      if (y < N / 2 && x >= N / 2)
        vs[y][N - x - 1][1] = coaster[y][x];
      if (y < N / 2 && x < N / 2)
        vs[y][x][0] = coaster[y][x];
    }
  }
  for (int y = 0; y < N / 2; ++y) {
    for (int x = 0; x < N / 2; ++x) {
      int nn = 0;
      for (int t = 0; t < 4; ++t) {
        nn += vs[y][x][t];
      }
      sum += (1 - ((nn == 0) || (nn == 4)));
      // sum+= 1-(all_of(vs[y][x].begin(), vs[y][x].end(), [=](const int a)
      // {return a == vs[y][x][0]; }));
    }
  }
  int ans = 0;
  if (!sum)
    ans++;
  for (int i = 0; i < C - 1; ++i) {
    for (auto dif : difs[i]) {
      int y = dif.first;
      int x = dif.second;
      coaster[y][x] = !coaster[y][x];
      int ay = y >= N / 2 ? N - y - 1 : y;
      int ax = x >= N / 2 ? N - x - 1 : x;
      {

        int nn = 0;
        for (int t = 0; t < 4; ++t) {
          nn += vs[ay][ax][t];
        }
        sum -= (1 - ((nn == 0) || (nn == 4)));
      }
      // sum-= 1-(all_of(vs[ay][ax].begin(), vs[ay][ax].end(), [=](const int a)
      // {return a == vs[ay][ax][0]; }));
      if (y >= N / 2 && x >= N / 2)
        vs[N - y - 1][N - x - 1][3] = coaster[y][x];
      if (y >= N / 2 && x < N / 2)
        vs[N - y - 1][x][2] = coaster[y][x];
      if (y < N / 2 && x >= N / 2)
        vs[y][N - x - 1][1] = coaster[y][x];
      if (y < N / 2 && x < N / 2)
        vs[y][x][0] = coaster[y][x];
      {
        int nn = 0;
        for (int t = 0; t < 4; ++t) {
          nn += vs[ay][ax][t];
        }
        sum += (1 - ((nn == 0) || (nn == 4)));
      }
      // sum += 1-(all_of(vs[ay][ax].begin(), vs[ay][ax].end(), [=](const int a)
      // {return a == vs[ay][ax][0]; }));
    }
    if (!sum)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

vector<vector<int>> coaster;
vector<vector<pair<int, int>>> difs;
int main() {
  int C, N;
  cin >> C >> N;
  {
    vector<vector<int>> field(N, vector<int>(N));
    for (int y = 0; y < N; ++y) {
      string st;
      cin >> st;
      for (int x = 0; x < N; ++x) {
        field[y][x] = st[x] - '0';
      }
    }
    coaster = field;
    difs.resize(C - 1);
    for (int i = 0; i < C - 1; ++i) {
      int D;
      cin >> D;
      while (D--) {
        int y, x;
        scanf("%d %d", &y, &x);
        y--;
        x--;
        difs[i].push_back(make_pair(y, x));
      }
    }
  }
  vector<vector<vector<int>>> vs(N / 2,
                                 vector<vector<int>>(N / 2, vector<int>(4)));
  int sum = 0;
  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      if (y >= N / 2 && x >= N / 2)
        vs[N - y - 1][N - x - 1][3] = coaster[y][x];
      if (y >= N / 2 && x < N / 2)
        vs[N - y - 1][x][2] = coaster[y][x];
      if (y < N / 2 && x >= N / 2)
        vs[y][N - x - 1][1] = coaster[y][x];
      if (y < N / 2 && x < N / 2)
        vs[y][x][0] = coaster[y][x];
    }
  }
  for (int y = 0; y < N / 2; ++y) {
    for (int x = 0; x < N / 2; ++x) {
      sum += 1 - (all_of(vs[y][x].begin(), vs[y][x].end(),
                         [=](const int a) { return a == vs[y][x][0]; }));
    }
  }
  int ans = 0;
  if (!sum)
    ans++;
  for (int i = 0; i < C - 1; ++i) {
    for (auto dif : difs[i]) {
      int y = dif.first;
      int x = dif.second;
      coaster[y][x] = !coaster[y][x];
      int ay = y >= N / 2 ? N - y - 1 : y;
      int ax = x >= N / 2 ? N - x - 1 : x;
      sum -= 1 - (all_of(vs[ay][ax].begin(), vs[ay][ax].end(),
                         [=](const int a) { return a == vs[ay][ax][0]; }));
      if (y >= N / 2 && x >= N / 2)
        vs[N - y - 1][N - x - 1][3] = coaster[y][x];
      if (y >= N / 2 && x < N / 2)
        vs[N - y - 1][x][2] = coaster[y][x];
      if (y < N / 2 && x >= N / 2)
        vs[y][N - x - 1][1] = coaster[y][x];
      if (y < N / 2 && x < N / 2)
        vs[y][x][0] = coaster[y][x];
      sum += 1 - (all_of(vs[ay][ax].begin(), vs[ay][ax].end(),
                         [=](const int a) { return a == vs[ay][ax][0]; }));
    }
    if (!sum)
      ans++;
  }
  cout << ans << endl;
  return 0;
}