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

#include <algorithm>
#include <cstdio>
using namespace std;
int H, W, L, P, x, y, cl[4009], cr[4009], c[4009], cc[4009], b[4009], bit[4009];
short pl[4009][4009], pr[4009][4009], pu[4009][4009], pd[4009][4009];
bool f[4009][4009];
inline int sum(int r) {
  int ret = 0;
  for (int i = r; i >= 1; i -= i & (-i))
    ret += bit[i];
  return ret;
}
int main() {
  scanf("%d %d %d %d", &H, &W, &L, &P);
  for (int i = 0; i < P; i++) {
    scanf("%d %d", &x, &y);
    f[x - 1][y - 1] = true;
  }
  for (int i = 0; i < H; i++) {
    pl[i][0] = (f[i][0] ? 0 : 1);
    pr[i][W - 1] = (f[i][W - 1] ? 0 : 1);
    for (int j = 1; j < W; j++)
      pl[i][j] = (f[i][j] ? 0 : pl[i][j - 1] + 1);
    for (int j = W - 2; j >= 0; j--)
      pr[i][j] = (f[i][j] ? 0 : pr[i][j + 1] + 1);
  }
  for (int i = 0; i < W; i++) {
    pu[0][i] = (f[0][i] ? 0 : 1);
    pd[H - 1][i] = (f[H - 1][i] ? 0 : 1);
    for (int j = 1; j < H; j++)
      pu[j][i] = (f[j][i] ? 0 : pu[j - 1][i] + 1);
    for (int j = H - 2; j >= 0; j--)
      pd[j][i] = (f[j][i] ? 0 : pd[j + 1][i] + 1);
  }
  long long ret = 0;
  for (int i = L - 1; i <= H + W - L - 1; i++) {
    int vl = (i < W ? 0 : i - W + 1), vr = (i < H ? i + 1 : H);
    fill(c, c + vr - vl + 1, 0);
    cc[0] = 0;
    fill(bit, bit + vr - vl + 1, 0);
    for (int j = vl; j < vr; j++) {
      cl[j - vl] = min(pl[j][i - j], pd[j][i - j]);
      cr[j - vl] = min(pr[j][i - j], pu[j][i - j]);
      c[j - vl - cr[j - vl] + 2]++;
    }
    for (int j = 1; j <= vr - vl; j++)
      c[j] += c[j - 1], cc[j] = c[j];
    for (int j = 0; j < vr - vl; j++)
      b[cc[j - cr[j] + 1]++] = j;
    for (int j = 0; j < vr - vl; j++) {
      for (int k = c[j]; k < c[j + 1]; k++) {
        for (int l = b[k] + 1; l <= vr - vl; l += l & (-l))
          bit[l]++;
      }
      if (cl[j] > L - 1)
        ret += sum(j + cl[j]) - sum(j + L - 1);
    }
  }
  printf("%lld\\n", ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#pragma warning(disable : 4996)
using namespace std;
int H, W, L, P, x, y, cl[4009], cr[4009], c[4009], cc[4009], b[4009], bit[4009];
short pl[4009][4009], pr[4009][4009], pu[4009][4009], pd[4009][4009];
bool f[4009][4009];
int main() {
  scanf("%d %d %d %d", &H, &W, &L, &P);
  for (int i = 0; i < P; i++) {
    scanf("%d %d", &x, &y);
    f[x - 1][y - 1] = true;
  }
  for (int i = 0; i < H; i++) {
    pl[i][0] = (f[i][0] ? 0 : 1);
    pr[i][W - 1] = (f[i][W - 1] ? 0 : 1);
    for (int j = 1; j < W; j++)
      pl[i][j] = (f[i][j] ? 0 : pl[i][j - 1] + 1);
    for (int j = W - 2; j >= 0; j--)
      pr[i][j] = (f[i][j] ? 0 : pr[i][j + 1] + 1);
  }
  for (int i = 0; i < W; i++) {
    pu[0][i] = (f[0][i] ? 0 : 1);
    pd[H - 1][i] = (f[H - 1][i] ? 0 : 1);
    for (int j = 1; j < H; j++)
      pu[j][i] = (f[j][i] ? 0 : pu[j - 1][i] + 1);
    for (int j = H - 2; j >= 0; j--)
      pd[j][i] = (f[j][i] ? 0 : pd[j + 1][i] + 1);
  }
  long long ret = 0;
  for (int i = L - 1; i <= H + W - L - 1; i++) {
    int vl = (i < W ? 0 : i - W + 1), vr = (i < H ? i + 1 : H);
    fill(c, c + vr - vl + 1, 0);
    cc[0] = 0;
    fill(bit, bit + vr - vl + 1, 0);
    for (int j = vl; j < vr; j++) {
      cl[j - vl] = min(pl[j][i - j], pd[j][i - j]);
      cr[j - vl] = min(pr[j][i - j], pu[j][i - j]);
      c[j - vl - cr[j - vl] + 2]++;
    }
    for (int j = 1; j <= vr - vl; j++)
      c[j] += c[j - 1], cc[j] = c[j];
    for (int j = 0; j < vr - vl; j++)
      b[cc[j - cr[j] + 1]++] = j;
    for (int j = 0; j < vr - vl; j++) {
      for (int k = c[j]; k < c[j + 1]; k++)
        bit[b[k]]++;
      if (cl[j] > L - 1)
        for (int k = j + L - 1; k < j + cl[j]; k++)
          ret += bit[k];
    }
  }
  printf("%lld\\n", ret);
  return 0;
}