Problem p03225 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<string> rotate(int h, int w, vector<string> &grid) {
  vector<string> ngrid(w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ngrid[w - 1 - j].push_back(grid[i][j]);
    }
  }
  return ngrid;
}
/**

  ..............

        #

  .......
  .
  .
  .     #
  **/
const int maxn = 620;
char G[maxn][maxn];
int line_by_d[maxn][maxn], rows[maxn][maxn], cols[maxn][maxn];
int get_in_rows(int row, int x, int y) {
  if (x > y)
    return 0;
  int sum = rows[row][y];
  if (x)
    sum -= rows[row][x - 1];
  return sum;
}
int get_in_cols(int col, int x, int y) {
  if (x > y)
    return 0;
  int sum = cols[col][y];
  if (x)
    sum -= cols[col][x - 1];
  return sum;
}
int get_in_d(int row, int x, int y) {
  if (x > y)
    return 0;
  int sum = line_by_d[row][y];
  if (x)
    sum -= line_by_d[row][x - 1];
  return sum;
}
int solve(int h, int w, vector<string> &grid) {
  memset(G, '.', sizeof(G));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '#') {
        G[i - j + w - 1][i + j] = '#';
      }
    }
  }
  int n = h + w - 1;
  int ans = 0;
  int l = 2 * n / 3;
  for (int i = 0; i < n; i++) {
    rows[i][0] = G[i][0] == '#';
    cols[i][0] = G[0][i] == '#';
    for (int j = 1; j < n; j++) {
      rows[i][j] = G[i][j] == '#';
      cols[i][j] = G[j][i] == '#';
      rows[i][j] += rows[i][j - 1];
      cols[i][j] += cols[i][j - 1];
    }
  }
  int md2 = (w - 1) % 2;
  for (int d = 1; d <= l; d++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        line_by_d[i][j] = 0;
      }
      for (int j = 0; j + d < n; j++) {
        line_by_d[i][j] = G[i][j] == '#' && G[i][j + d] == '#';
      }
      for (int j = 1; j < n; j++) {
        line_by_d[i][j] += line_by_d[i][j - 1];
      }
    }
    for (int i = d; i < n; i++) {
      int from = 0;
      if ((i + from) % 2 != md2)
        from++;
      for (int j = from; j < n; j += 2) {
        if (G[i][j] == '#') {
          int nans = 0;
          nans += get_in_d(i - d, max(j - d + 1, 0), j - 1);
          if (G[i - d][j] == '#' && j - d >= 0) {
            nans += get_in_cols(j - d, i - d + 1, i - 1);
          }
          if (j - d >= 0 && G[i][j - d] == '#') {
            nans += get_in_rows(i - d, j - d + 1, j - 1);
          }
          if (G[i - d][j] == '#' && j - d >= 0 && G[i][j - d] == '#') {
            nans += 1;
          }
          if (G[i - d][j] == '#' && j - d >= 0 && G[i - d][j - d] == '#') {
            nans += 1;
          }
          if (j - d >= 0 && G[i][j - d] == '#' && G[i - d][j - d] == '#') {
            nans += 1;
          }
          ans += nans;
        }
      }
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  vector<string> grid(h);
  for (int i = 0; i < h; i++)
    cin >> grid[i];
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    grid = rotate(h, w, grid);
    swap(h, w);
    ans += solve(h, w, grid);
  }
  cout << ans / 3 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<string> rotate(int h, int w, vector<string> &grid) {
  vector<string> ngrid(w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ngrid[w - 1 - j].push_back(grid[i][j]);
    }
  }
  return ngrid;
}
/**

  ..............

        #

  .......
  .
  .
  .     #
  **/
const int maxn = 620;
char G[maxn][maxn];
int line_by_d[maxn][maxn], rows[maxn][maxn], cols[maxn][maxn];
int get_in_rows(int row, int x, int y) {
  if (x > y)
    return 0;
  int sum = rows[row][y];
  if (x)
    sum -= rows[row][x - 1];
  return sum;
}
int get_in_cols(int col, int x, int y) {
  if (x > y)
    return 0;
  int sum = cols[col][y];
  if (x)
    sum -= cols[col][x - 1];
  return sum;
}
int get_in_d(int row, int x, int y) {
  if (x > y)
    return 0;
  int sum = line_by_d[row][y];
  if (x)
    sum -= line_by_d[row][x - 1];
  return sum;
}
int solve(int h, int w, vector<string> &grid) {
  memset(G, '.', sizeof(G));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '#') {
        G[i - j + w - 1][i + j] = '#';
      }
    }
  }
  int n = h + w - 1;
  int ans = 0;
  int l = 2 * n / 3;
  for (int i = 0; i < n; i++) {
    rows[i][0] = G[i][0] == '#';
    cols[i][0] = G[0][i] == '#';
    for (int j = 1; j < n; j++) {
      rows[i][j] = G[i][j] == '#';
      cols[i][j] = G[j][i] == '#';
      rows[i][j] += rows[i][j - 1];
      cols[i][j] += cols[i][j - 1];
    }
  }
  int md2 = (w - 1) % 2;
  for (int d = 2; d <= l; d += 2) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        line_by_d[i][j] = 0;
      }
      for (int j = 0; j + d < n; j++) {
        line_by_d[i][j] = G[i][j] == '#' && G[i][j + d] == '#';
      }
      for (int j = 1; j < n; j++) {
        line_by_d[i][j] += line_by_d[i][j - 1];
      }
    }
    for (int i = d; i < n; i++) {
      int from = 0;
      if ((i + from) % 2 != md2)
        from++;
      for (int j = from; j < n; j += 2) {
        if (G[i][j] == '#') {
          int nans = 0;
          nans += get_in_d(i - d, max(j - d + 1, 0), j - 1);
          if (G[i - d][j] == '#' && j - d >= 0) {
            nans += get_in_cols(j - d, i - d + 1, i - 1);
          }
          if (j - d >= 0 && G[i][j - d] == '#') {
            nans += get_in_rows(i - d, j - d + 1, j - 1);
          }
          if (G[i - d][j] == '#' && j - d >= 0 && G[i][j - d] == '#') {
            nans += 1;
          }
          if (G[i - d][j] == '#' && j - d >= 0 && G[i - d][j - d] == '#') {
            nans += 1;
          }
          if (j - d >= 0 && G[i][j - d] == '#' && G[i - d][j - d] == '#') {
            nans += 1;
          }
          ans += nans;
        }
      }
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  vector<string> grid(h);
  for (int i = 0; i < h; i++)
    cin >> grid[i];
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    grid = rotate(h, w, grid);
    swap(h, w);
    ans += solve(h, w, grid);
  }
  cout << ans / 3 << endl;
  return 0;
}