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

#include <bits/stdc++.h>

using namespace std;

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

typedef long long ll;

int cnt[30][55][55];
int cnt2[30][55][55];
int val[30];

int getcnt(int a, int b, int c, int d, int e, int cnt[30][55][55]) {
  int ret = cnt[e][c][d];
  if (c)
    ret -= cnt[e][a - 1][d];
  if (d)
    ret -= cnt[e][c][b - 1];
  if (c && d)
    ret += cnt[e][a - 1][b - 1];
  return ret;
}

int main() {
  int n;
  cin >> n;
  rep(o, n) {
    int h, w;
    cin >> h >> w;
    vector<string> v(h);
    rep(i, h) cin >> v[i];
    bool update = true;
    bool ok = true;
    int ch[30];
    while (update) {
      ok = true;
      update = false;
      rep(i, h) rep(j, w) if (v[i][j] != '.' && v[i][j] != '-') ok = false;
      if (ok) {
        break;
      }
      rep(i, 30) rep(j, 55) rep(k, 55) cnt[i][j][k] = cnt2[i][j][k] = 0;
      rep(i, 30) {
        ch[i] = 0;
        rep(j, h) {
          rep(k, w) {
            if (v[j][k] == char('A' + i) || v[j][k] == '-') {
              cnt[i][j][k] = 1;
              if (v[j][k] == char('A' + i)) {
                ch[i]++;
                cnt2[i][j][k] = 1;
              }
            }
            if (j)
              cnt[i][j][k] += cnt[i][j - 1][k];
            if (k)
              cnt[i][j][k] += cnt[i][j][k - 1];
            if (j && k)
              cnt[i][j][k] -= cnt[i][j - 1][k - 1];

            if (j)
              cnt2[i][j][k] += cnt2[i][j - 1][k];
            if (k)
              cnt2[i][j][k] += cnt2[i][j][k - 1];
            if (j && k)
              cnt2[i][j][k] -= cnt2[i][j - 1][k - 1];
          }
        }
      }
      // rep(i,h){
      // 	cout<<v[i]<<endl;
      // }
      // cout<<endl;
      rep(i, 30) {
        if (!ch[i])
          continue;
        rep(y1, h) {
          rep(x1, w) {
            reep(y2, y1, h) {
              reep(x2, x1, w) {
                if (getcnt(y1, x1, y2, x2, i, cnt2) == ch[i] &&
                    getcnt(y1, x1, y2, x2, i, cnt) ==
                        (y2 - y1 + 1) * (x2 - x1 + 1)) {
                  // cout<<"AAA"<<endl;
                  // cout<<y1<<" "<<x1<<" "<<y2<<" "<<x2<<" "<<char(i+'A')<<"
                  // "<<getcnt(y1,x1,y2,x2,i)<<endl;
                  update = true;
                  reep(j, y1, y2 + 1) {
                    reep(k, x1, x2 + 1) { v[j][k] = '-'; }
                  }
                  // goto end;
                }
              }
            }
          }
        }
        // end:
        // update=update;
      }
    }
    if (ok)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

typedef long long ll;

int cnt[30][55][55];
int val[30];

int getcnt(int a, int b, int c, int d, int e) {
  int ret = cnt[e][c][d];
  if (c)
    ret -= cnt[e][c - 1][b];
  if (d)
    ret -= cnt[e][a][d - 1];
  if (c && d)
    ret += cnt[e][c - 1][d - 1];
  return ret;
}

int main() {
  int n;
  cin >> n;
  rep(o, n) {
    int h, w;
    cin >> h >> w;
    vector<string> v(h);
    rep(i, h) cin >> v[i];
    bool update = true;
    bool ok = true;
    while (update) {
      ok = true;
      update = false;
      rep(i, h) rep(j, w) if (v[i][j] != '.' && v[i][j] != '-') ok = false;
      if (ok) {
        break;
      }
      rep(i, 30) rep(j, 55) rep(k, 55) cnt[i][j][k] = 0;
      rep(i, 30) {
        rep(j, h) {
          rep(k, w) {
            if (v[j][k] == char('A' + i) || v[j][k] == '-') {
              cnt[i][j][k] = 1;
            }
            if (j)
              cnt[i][j][k] += cnt[i][j - 1][k];
            if (k)
              cnt[i][j][k] += cnt[i][j][k - 1];
            if (j && k)
              cnt[i][j][k] -= cnt[i][j - 1][k - 1];
          }
        }
      }
      rep(i, 30) {
        rep(y1, h) {
          rep(x1, w) {
            reep(y2, y1, h) {
              reep(x2, x1, w) {
                if ((y2 - y1 + 1) * (x2 - x1 + 1) == cnt[i][h - 1][w - 1] &&
                    getcnt(y1, x1, y2, x2, i) == cnt[i][h - 1][w - 1]) {
                  // cout<<"AAA"<<endl;
                  update = true;
                  reep(j, y1, y2 + 1) {
                    reep(k, x1, x2 + 1) { v[j][k] = '-'; }
                  }
                }
              }
            }
          }
        }
      }
    }
    if (ok)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
}