Problem p03225 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
  v.resize(n);
  for (auto &&e : v)
    assign(e, args...);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  V<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  int res = 0;
  for (int xl = 0; xl < h; ++xl)
    for (int xr = xl + 1; xr < h; ++xr)
      for (int yl = 0; yl < w; ++yl) {
        for (int yr = yl + (xr - xl + 1 >> 1);
             yr <= yl + 2 * (xr - xl) and yr < w; ++yr)
          if ((xr - xl + yr - yl) % 3 == 0) {
            int l = xr - xl + yr - yl << 1;
            if (s[xl][yl] == '#') {
              res += s[xr][yl + l / 3 - (xr - xl)] == '#' and
                     s[xl + l / 3 - (yr - yl)][yr] == '#';
            }
            if (s[xl][yr] == '#' and xr - xl != yr - yl << 1 and
                xr - xl << 1 != yr - yl) {
              res += s[xr][yr - l / 3 + (xr - xl)] == '#' and
                     s[xl + l / 3 - (yr - yl)][yl] == '#';
            }
            if (s[xr][yl] == '#' and xr - xl != yr - yl << 1 and
                xr - xl << 1 != yr - yl) {
              res += s[xl][yl + l / 3 - (xr - xl)] == '#' and
                     s[xr - l / 3 + (yr - yl)][yr] == '#';
            }
            if (s[xr][yr] == '#') {
              res += s[xl][yr - l / 3 + (xr - xl)] == '#' and
                     s[xr - l / 3 + (yr - yl)][yl] == '#';
            }
          }
      }
  cout << res << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
  v.resize(n);
  for (auto &&e : v)
    assign(e, args...);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  V<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  VV<> cY;
  assign(cY, h + w - 1, h + w);
  VV<> cX;
  assign(cX, h + w - 1, h + w);
  for (int X = 0; X < h + w - 1; ++X)
    for (int Y = 0; Y < h + w - 1; ++Y) {
      int d = 1;
      if (X - Y + h - 1 & 1 or X + Y - h + 1 & 1)
        d = 0;
      int x = X - Y + h - 1 >> 1, y = X + Y - h + 1 >> 1;
      if (x < 0 or x >= h or y < 0 or y >= w)
        d = 0;
      else if (s[x][y] != '#')
        d = 0;
      cY[X][Y + 1] = cY[X][Y] + d;
      cX[Y][X + 1] = cX[Y][X] + d;
    }
  lint res = 0;
  for (int X = 0; X < h + w - 1; ++X)
    for (int Yl = 0; Yl < h + w - 1; ++Yl)
      for (int Yr = Yl + 1; Yr < h + w - 1; ++Yr) {
        if (X - Yl + h - 1 & 1 or X + Yl - h + 1 & 1 or X - Yr + h - 1 & 1 or
            X + Yr - h + 1 & 1)
          continue;
        int xl = X - Yl + h - 1 >> 1, yl = X + Yl - h + 1 >> 1;
        int xr = X - Yr + h - 1 >> 1, yr = X + Yr - h + 1 >> 1;
        if (xl < 0 or xl >= h or yl < 0 or yl >= w or xr < 0 or xr >= h or
            yr < 0 or yr >= w)
          continue;
        if (s[xl][yl] != '#' or s[xr][yr] != '#')
          continue;
        if (X - Yr + Yl >= 0)
          res += cY[X - Yr + Yl][Yr + 1] - cY[X - Yr + Yl][Yl];
        if (X + Yr - Yl < h + w - 1)
          res += cY[X + Yr - Yl][Yr + 1] - cY[X + Yr - Yl][Yl];
      }
  for (int Y = 0; Y < h + w - 1; ++Y)
    for (int Xl = 0; Xl < h + w - 1; ++Xl)
      for (int Xr = Xl + 1; Xr < h + w - 1; ++Xr) {
        if (Xl - Y + h - 1 & 1 or Xl + Y - h + 1 & 1 or Xr - Y + h - 1 & 1 or
            Xr + Y - h + 1 & 1)
          continue;
        int xl = Xl - Y + h - 1 >> 1, yl = Xl + Y - h + 1 >> 1;
        int xr = Xr - Y + h - 1 >> 1, yr = Xr + Y - h + 1 >> 1;
        if (xl < 0 or xl >= h or yl < 0 or yl >= w or xr < 0 or xr >= h or
            yr < 0 or yr >= w)
          continue;
        if (s[xl][yl] != '#' or s[xr][yr] != '#')
          continue;
        if (Y - Xr + Xl >= 0)
          res += cX[Y - Xr + Xl][Xr] - cX[Y - Xr + Xl][Xl + 1];
        if (Y + Xr - Xl < h + w - 1)
          res += cX[Y + Xr - Xl][Xr] - cX[Y + Xr - Xl][Xl + 1];
      }
  cout << res << '\n';
}