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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int H, W, L, P;
int X[100000], Y[100000];

bool ex[4000][4000];
int up[4000][4000], dw[4000][4000];

void Latting() {
  rep(i, H) rep(j, W) up[i][j] = dw[i][j] = INT_MAX;

  int prev;

  //>
  rep(i, H) {
    prev = 0;
    rep(j, W) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(dw[i][j], prev);
    }
  }

  //<
  rep(i, H) {
    prev = 0;
    for (int j = W - 1; j >= 0; j--) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(up[i][j], prev);
    }
  }

  //^
  rep(j, W) {
    prev = 0;
    rep(i, H) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(dw[i][j], prev);
    }
  }

  // v
  rep(j, W) {
    prev = 0;
    for (int i = H - 1; i >= 0; i--) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(up[i][j], prev);
    }
  }
}

int bit_n, bit[4444];
void init(int n) {
  bit_n = n;
  rep(i, bit_n + 114) bit[i] = 0;
}
void add(int k, int x) {
  for (k++; k <= bit_n; k += k & -k)
    bit[k] += x;
}
int sum(int k) {
  int ret = 0;
  for (k++; k; k -= k & -k)
    ret += bit[k];
  return ret;
}

long long ans = 0;

struct data {
  int l, k, f;
  data() {}
  data(int a, int b, int c) : l(a), k(b), f(c) {}
  bool operator<(const data &d) const {
    if (l != d.l)
      return l < d.l;
    return f < d.f;
  }
};

data v[10000];

void Malting(int y, int x) {
  int n;
  int vn = 0;
  for (n = 0; y + n < H && x + n < W; n++) {
    if (ex[y + n][x + n])
      continue;
    v[vn++] = data(up[y + n][x + n], n, 0);
    v[vn++] = data(dw[y + n][x + n], n, 1);
  }

  sort(v, v + vn);
  reverse(v, v + vn);
  init(n);
  rep(i, vn) {
    data &d = v[i];
    if (d.f == 1) {
      add(d.k, 1);
      continue;
    }
    if (d.l < L)
      continue;
    ans += sum(min(n - 1, d.k + d.l - 1)) - sum(min(n - 1, d.k + L - 2));
  }

  init(n);
  rep(i, vn) {
    data &d = v[i];
    if (d.f == 0) {
      add(d.k, 1);
      continue;
    }
    if (d.l < L)
      continue;
    ans += sum(max(-1, d.k - L + 1)) - sum(max(-1, d.k - d.l));
  }
}

signed main() {
  scanf("%d%d%d%d", &H, &W, &L, &P);
  rep(i, P) scanf("%d%d", &Y[i], &X[i]), Y[i]--, X[i]--, ex[Y[i]][X[i]] = 1;

  Latting();

  rep(i, H) Malting(i, 0);
  for (int i = 1; i < W; i++)
    Malting(0, i);
  printf("%lld\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int H, W, L, P;
int X[100000], Y[100000];

bool ex[4000][4000];
int up[4000][4000], dw[4000][4000];

void Latting() {
  rep(i, H) rep(j, W) up[i][j] = dw[i][j] = INT_MAX;

  int prev;

  //>
  rep(i, H) {
    prev = 0;
    rep(j, W) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(dw[i][j], prev);
    }
  }

  //<
  rep(i, H) {
    prev = 0;
    for (int j = W - 1; j >= 0; j--) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(up[i][j], prev);
    }
  }

  //^
  rep(j, W) {
    prev = 0;
    rep(i, H) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(dw[i][j], prev);
    }
  }

  // v
  rep(j, W) {
    prev = 0;
    for (int i = H - 1; i >= 0; i--) {
      if (ex[i][j])
        prev = 0;
      else
        prev++;
      chmin(up[i][j], prev);
    }
  }
}

int bit_n, bit[4444];
void init(int n) {
  bit_n = n;
  rep(i, bit_n + 114) bit[i] = 0;
}
void add(int k, int x) {
  for (k++; k <= bit_n; k += k & -k)
    bit[k] += x;
}
int sum(int k) {
  int ret = 0;
  for (k++; k; k -= k & -k)
    ret += bit[k];
  return ret;
}

int ans = 0;

struct data {
  int l, k, f;
  data() {}
  data(int a, int b, int c) : l(a), k(b), f(c) {}
  bool operator<(const data &d) const {
    if (l != d.l)
      return l < d.l;
    return f < d.f;
  }
};

data v[10000];

void Malting(int y, int x) {
  int n;
  int vn = 0;
  for (n = 0; y + n < H && x + n < W; n++) {
    if (ex[y + n][x + n])
      continue;
    v[vn++] = data(up[y + n][x + n], n, 0);
    v[vn++] = data(dw[y + n][x + n], n, 1);
  }

  sort(v, v + vn);
  reverse(v, v + vn);
  init(n);
  rep(i, vn) {
    data &d = v[i];
    if (d.f == 1) {
      add(d.k, 1);
      continue;
    }
    if (d.l < L)
      continue;
    ans += sum(min(n - 1, d.k + d.l - 1)) - sum(min(n - 1, d.k + L - 2));
  }

  init(n);
  rep(i, vn) {
    data &d = v[i];
    if (d.f == 0) {
      add(d.k, 1);
      continue;
    }
    if (d.l < L)
      continue;
    ans += sum(max(-1ll, d.k - L + 1)) - sum(max(-1ll, d.k - d.l));
  }
}

signed main() {
  scanf("%lld%lld%lld%lld", &H, &W, &L, &P);
  rep(i, P) scanf("%lld%lld", &Y[i], &X[i]), Y[i]--, X[i]--, ex[Y[i]][X[i]] = 1;

  Latting();

  rep(i, H) Malting(i, 0);
  for (int i = 1; i < W; i++)
    Malting(0, i);
  printf("%lld\\n", ans);
  return 0;
}