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

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int H, W, L, P, bit[4009];
bool used[4009][4009];
int lx[4009][4009], rx[4009][4009];
void add(int p, int x) {
  p++;
  while (p <= 4009) {
    bit[p] += x;
    p += (p & -p);
  }
}
int sum(int p) {
  int s = 0;
  p++;
  while (p >= 1) {
    s += bit[p];
    p -= (p & -p);
  }
  return s;
}
int main() {
  cin >> H >> W >> L >> P;
  for (int i = 0; i < P; i++) {
    int a, b;
    cin >> a >> b;
    used[a][b] = true;
  }
  for (int i = 0; i < 4009; i++) {
    for (int j = 0; j < 4009; j++) {
      lx[i][j] = 999999;
      rx[i][j] = 999999;
    }
  }
  for (int i = 1; i <= H; i++) {
    int s = 0;
    for (int j = 1; j <= W; j++) {
      if (used[i][j] == true)
        s = 0;
      else
        s++;
      rx[i][j] = min(rx[i][j], s);
    }
    s = 0;
    for (int j = W; j >= 1; j--) {
      if (used[i][j] == true)
        s = 0;
      else
        s++;
      lx[i][j] = min(lx[i][j], s);
    }
  }
  for (int i = 1; i <= W; i++) {
    int s = 0;
    for (int j = 1; j <= H; j++) {
      if (used[j][i] == true)
        s = 0;
      else
        s++;
      rx[j][i] = min(rx[j][i], s);
    }
    s = 0;
    for (int j = H; j >= 1; j--) {
      if (used[j][i] == true)
        s = 0;
      else
        s++;
      lx[j][i] = min(lx[j][i], s);
    }
  }
  int Z = max(H, W);
  long long ans = 0;
  for (int i = -Z; i <= Z; i++) {
    vector<pair<int, int>> V;
    for (int j = 1; j <= H; j++) {
      int cx = j, cy = j + i;
      if (cy <= 0 || cy > W)
        continue;
      V.push_back(make_pair(cx, cy));
    }
    if (V.size() < L)
      continue;
    vector<pair<int, int>> U;
    for (int j = 0; j < V.size(); j++) {
      U.push_back(make_pair(rx[V[j].first][V[j].second] - j, j));
    }
    sort(U.begin(), U.end(), greater<pair<int, int>>());
    int cx = 0;
    for (int j = 0; j < 4009; j++)
      bit[j] = 0;
    for (int j = 0; j < V.size(); j++) {
      while (cx < U.size() && U[cx].first >= -j) {
        add(U[cx].second, 1);
        cx++;
      }
      int l = j + L - 1, r = j + lx[V[j].first][V[j].second] - 1;
      if (l > r)
        continue;
      ans += sum(r) - sum(l - 1);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

#define MAX_N 4000
#define MAX_K 100000

int H, W, K, L, P, a[MAX_K], b[MAX_K];
int x[MAX_N][MAX_N];
int y[MAX_N][MAX_N];
int z[MAX_N][MAX_N];
long long cnt;

int main() {
  cin >> H >> W >> P >> K;
  for (int i = 0; i < K; i++) {
    cin >> a[i] >> b[i];
    x[a[i]][b[i]]++;
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      y[i][j] = y[i][j - 1] + x[i][j];
    }
  }
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= H; j++) {
      z[j][i] = z[j - 1][i] + x[j][i];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      for (int k = P - 1; k <= min(H, W); k++) {
        int X1 = i, X2 = i + k;
        int Y1 = j, Y2 = j + k;
        if (X2 > H || Y2 > W) {
          break;
        }
        int sum1 = y[X1][Y2] - y[X1][Y1 - 1];
        int sum2 = y[X2][Y2] - y[X2][Y1 - 1];
        int sum3 = z[X2][Y1] - z[X1 - 1][Y1];
        int sum4 = z[X2][Y2] - z[X1 - 1][Y2];
        int sum = sum1 + sum2 + sum3 + sum4;
        if (sum == 0) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}