Problem p03225 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int H, W, N, p[609][609], q[609][609];

int vals(int px, int py) {
  px = min(px, 2 * N);
  py = min(py, 2 * N);
  if (px < 0 || py < 0)
    return 0;
  return p[px][py];
}

int valm(int px, int py) {
  if (px < 0 || py < 0 || px > 2 * N || py > 2 * N)
    return 0;
  return q[px][py];
}

int range_sum(int px, int py, int qx, int qy) {
  return vals(qx, qy) + vals(px - 1, py - 1) - vals(px - 1, qy) -
         vals(qx, py - 1);
}

int main() {
  cin >> H >> W;
  N = max(H, W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        p[i + j][i - j + N] = 1;
        q[i + j][i - j + N] = 1;
      }
    }
  }
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 1; j <= 2 * N; j++)
      p[i][j] += p[i][j - 1];
  }
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 1; j <= 2 * N; j++)
      p[j][i] += p[j - 1][i];
  }

  long long ans = 0;
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 0; j <= 2 * N; j++) {
      for (int k = 1; k <= N; k++) {
        int V1 = valm(i - k, j - k) * valm(i - k, j + k) *
                 range_sum(i + k, j - k, i + k, j + k);
        int V2 = valm(i + k, j - k) * valm(i + k, j + k) *
                 range_sum(i - k, j - k, i - k, j + k);
        int V3 = valm(i - k, j - k) * valm(i + k, j - k) *
                 range_sum(i - k + 1, j + k, i + k - 1, j + k);
        int V4 = valm(i - k, j + k) * valm(i + k, j + k) *
                 range_sum(i - k + 1, j - k, i + k - 1, j - k);
        ans += (V1 + V2 + V3 + V4);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int H, W, N, p[609][609], q[609][609];

int vals(int px, int py) {
  px = min(px, 2 * N);
  py = min(py, 2 * N);
  if (px < 0 || py < 0)
    return 0;
  return p[px][py];
}

int valm(int px, int py) {
  if (px < 0 || py < 0 || px > 2 * N || py > 2 * N)
    return 0;
  return q[px][py];
}

int range_sum(int px, int py, int qx, int qy) {
  return vals(qx, qy) + vals(px - 1, py - 1) - vals(px - 1, qy) -
         vals(qx, py - 1);
}

int main() {
  cin >> H >> W;
  N = max(H, W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        p[i + j][i - j + N] = 1;
        q[i + j][i - j + N] = 1;
      }
    }
  }
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 1; j <= 2 * N; j++)
      p[i][j] += p[i][j - 1];
  }
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 1; j <= 2 * N; j++)
      p[j][i] += p[j - 1][i];
  }

  long long ans = 0;
  for (int i = 0; i <= 2 * N; i++) {
    for (int j = 0; j <= 2 * N; j++) {
      int S = min({i, j, 2 * N - i, 2 * N - j});
      for (int k = 1; k <= S; k++) {
        int V1 = valm(i - k, j - k) * valm(i - k, j + k) *
                 range_sum(i + k, j - k, i + k, j + k);
        int V2 = valm(i + k, j - k) * valm(i + k, j + k) *
                 range_sum(i - k, j - k, i - k, j + k);
        int V3 = valm(i - k, j - k) * valm(i + k, j - k) *
                 range_sum(i - k + 1, j + k, i + k - 1, j + k);
        int V4 = valm(i - k, j + k) * valm(i + k, j + k) *
                 range_sum(i - k + 1, j - k, i + k - 1, j - k);
        ans += (V1 + V2 + V3 + V4);
      }
    }
  }
  cout << ans << endl;
  return 0;
}