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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

int v[1010][1010];
// char s[1010][1010];

bool check(int x1, int y1, int x2, int y2) {
  x1--;
  y1--;
  return (v[y2][x2] - v[y2][x1] - v[y1][x2] + v[y1][x1]) == 0;
}

int main() {
  int n;
  vector<string> s;
  while (cin >> n && n) {
    s = vector<string>(n);
    rep(i, n) {
      // scanf("%s", s[i]);
      cin >> s[i];
    }
    for (int i = 0; i < n + 2; i++) {
      for (int j = 0; j < n + 2; j++) {
        v[i][j] = 0;
      }
    }
    rep(i, n) {
      rep(j, n) { v[i + 1][j + 1] = v[i + 1][j] + (s[i][j] == '*' ? 1 : 0); }
    }
    rep(i, n) {
      rep(j, n) { v[i + 1][j + 1] += v[i][j + 1]; }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        int l = 1, h = n - max(i, j), m;
        // while(l+1<h){
        rep(k, 30) {
          m = (l + h) / 2;
          if (check(i, j, i + m, j + m)) {
            l = m; //+ 1;
          } else {
            h = m; //- 1;
          }
        }
        // while(!check(i, j, i + l, j + l)) l--;
        if (check(i, j, i + h, j + h))
          ans = max(ans, h + 1);
        if (check(i, j, i + l, j + l))
          ans = max(ans, l + 1);
      }
    }
    // cout<<ans<<endl;
    printf("%d\\n", ans);
    // rep(i, n + 1){
    // 	rep(j, n + 1){
    // 		printf("%d ", v[i][j]);
    // 	}
    // 	printf("\\n");
    // }
    // ep(i, n) cout<<s[i]<<endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<int, pii> pip;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

vii v;

bool check(int x1, int y1, int x2, int y2) {
  x1--;
  y1--;
  return (v[y2][x2] - v[y2][x1] - v[y1][x2] + v[y1][x1]) == 0;
}

int main() {
  int n;
  vector<string> s;
  while (cin >> n && n) {
    s = vector<string>(n);
    rep(i, n) cin >> s[i];
    v = vii(n + 2, vi(n + 2, 0));
    rep(i, n) {
      rep(j, n) { v[i + 1][j + 1] = v[i + 1][j] + (s[i][j] == '*' ? 1 : 0); }
    }
    rep(i, n) {
      rep(j, n) { v[i + 1][j + 1] += v[i][j + 1]; }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        int l = 1, h = n - max(i, j), m;
        // while(l+1<h){
        rep(k, 50) {
          m = (l + h) / 2;
          if (check(i, j, i + m, j + m)) {
            l = m; //+ 1;
          } else {
            h = m; //- 1;
          }
        }
        // while(!check(i, j, i + l, j + l)) l--;
        if (check(i, j, i + h, j + h))
          ans = max(ans, h + 1);
        if (check(i, j, i + l, j + l))
          ans = max(ans, l + 1);
      }
    }
    cout << ans << endl;

    // rep(i, n + 1){
    // 	rep(j, n + 1){
    // 		printf("%d ", v[i][j]);
    // 	}
    // 	printf("\\n");
    // }
    // ep(i, n) cout<<s[i]<<endl;
  }
}