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

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

using P = pair<short, short>;
using T = pair<short, P>;

int h, w, l, p;
short g[4010][4010];
short s[2][4010][4010];

struct FenwickTree {
  FenwickTree() { init(); }
  void init() { fill(begin(v), end(v), 0); }
  void add(int k, short x) {
    k++;
    while (k < 4010) {
      v[k] += x;
      k += k & -k;
    }
  }
  short sum(int k) {
    k = max(0, min<int>(4009, k));
    int s = 0;
    while (k) {
      s += v[k];
      k -= k & -k;
    }
    return s;
  }
  short sum(int l, int r) { return sum(r) - sum(l); }

private:
  short v[4010];
};

int main() {
  scanf("%d %d %d %d", &h, &w, &l, &p);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      g[i][j] = 1;
    }
  }
  for (int i = 0; i < p; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a][b] = 0;
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      s[0][i][j] = s[1][i][j] = 9999;
    }
  }

  for (int i = 1; i <= h; i++) {
    short t = 0;
    for (int j = 1; j <= w; j++) {
      t = g[i][j] ? t + 1 : 0;
      s[0][i][j] = min(s[0][i][j], t);
    }
  }

  for (int j = 1; j <= w; j++) {
    short t = 0;
    for (int i = 1; i <= h; i++) {
      t = g[i][j] ? t + 1 : 0;
      s[0][i][j] = min(s[0][i][j], t);
    }
  }

  for (int i = 1; i <= h; i++) {
    short t = 0;
    for (int j = w; j >= 1; j--) {
      t = g[i][j] ? t + 1 : 0;
      s[1][i][j] = min(s[1][i][j], t);
    }
  }

  for (int j = 1; j <= w; j++) {
    short t = 0;
    for (int i = h; i >= 1; i--) {
      t = g[i][j] ? t + 1 : 0;
      s[1][i][j] = min(s[1][i][j], t);
    }
  }

  int offset = w - 1;
  /*
  vector<vector<T>> vs(h + w - 1);
  for (int i = 1; i <= h; i++){
          for (int j = 1; j <= w; j++){
                  int k = vs[i - j + offset].size() / 2;
                  vs[i - j + offset].emplace_back(s[1][i][j], P(0, k));
                  vs[i - j + offset].emplace_back(s[0][i][j], P(1, k));
          }
  }
  */

  vector<pair<int, int>> ss;
  for (int i = 1; i <= h; i++)
    ss.emplace_back(i, 1);
  for (int i = 2; i <= w; i++)
    ss.emplace_back(1, i);

  long long res = 0;
  vector<FenwickTree> tr(2);
  for (auto sp : ss) {
    vector<T> v;
    while (sp.first <= h && sp.second <= w) {
      int k = v.size() / 2;
      v.emplace_back(s[1][sp.first][sp.second], P(0, k));
      v.emplace_back(s[0][sp.first][sp.second], P(1, k));
      sp.first++;
      sp.second++;
    }

    tr[0].init();
    tr[1].init();
    sort(begin(v), end(v), greater<T>());
    for (auto t : v) {
      tr[t.second.first].add(t.second.second, 1);
      if (t.second.first == 0) {
        int ll = t.second.second + l - 1;
        int rr = t.second.second + t.first;
        if (ll < rr)
          res += tr[1].sum(ll, rr);
      } else {
        int ll = t.second.second - t.first + 1;
        int rr = t.second.second - l + 2;
        if (ll < rr)
          res += tr[0].sum(ll, rr);
      }
    }
    v.clear();
    v.shrink_to_fit();
  }

  printf("%lld\\n", res);
}

Error: MLE

Fixed submission:

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

using P = pair<short, short>;
using T = pair<short, P>;

int h, w, l, p;
short g[4010][4010];
short s[2][4010][4010];

struct FenwickTree {
  FenwickTree() { init(); }
  void init() { fill(begin(v), end(v), 0); }
  void add(int k, short x) {
    k++;
    while (k < 4010) {
      v[k] += x;
      k += k & -k;
    }
  }
  short sum(int k) {
    k = max(0, min<int>(4009, k));
    int s = 0;
    while (k) {
      s += v[k];
      k -= k & -k;
    }
    return s;
  }
  short sum(int l, int r) { return sum(r) - sum(l); }

private:
  short v[4010];
};

int main() {
  scanf("%d %d %d %d", &h, &w, &l, &p);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      g[i][j] = 1;
    }
  }
  for (int i = 0; i < p; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a][b] = 0;
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      s[0][i][j] = s[1][i][j] = 9999;
    }
  }

  for (int i = 1; i <= h; i++) {
    short t = 0;
    for (int j = 1; j <= w; j++) {
      t = g[i][j] ? t + 1 : 0;
      s[0][i][j] = min(s[0][i][j], t);
    }
  }

  for (int j = 1; j <= w; j++) {
    short t = 0;
    for (int i = 1; i <= h; i++) {
      t = g[i][j] ? t + 1 : 0;
      s[0][i][j] = min(s[0][i][j], t);
    }
  }

  for (int i = 1; i <= h; i++) {
    short t = 0;
    for (int j = w; j >= 1; j--) {
      t = g[i][j] ? t + 1 : 0;
      s[1][i][j] = min(s[1][i][j], t);
    }
  }

  for (int j = 1; j <= w; j++) {
    short t = 0;
    for (int i = h; i >= 1; i--) {
      t = g[i][j] ? t + 1 : 0;
      s[1][i][j] = min(s[1][i][j], t);
    }
  }

  int offset = w - 1;
  vector<vector<T>> vs(h + w - 1);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      int k = vs[i - j + offset].size() / 2;
      vs[i - j + offset].emplace_back(s[1][i][j], P(0, k));
      vs[i - j + offset].emplace_back(s[0][i][j], P(1, k));
    }
  }

  long long res = 0;
  vector<FenwickTree> tr(2);
  for (auto &v : vs) {
    if (v.empty())
      break;
    tr[0].init();
    tr[1].init();
    sort(begin(v), end(v), greater<T>());
    for (auto t : v) {
      tr[t.second.first].add(t.second.second, 1);
      if (t.second.first == 0) {
        int ll = t.second.second + l - 1;
        int rr = t.second.second + t.first;
        if (ll < rr)
          res += tr[1].sum(ll, rr);
      } else {
        int ll = t.second.second - t.first + 1;
        int rr = t.second.second - l + 2;
        if (ll < rr)
          res += tr[0].sum(ll, rr);
      }
    }
    v.clear();
    v.shrink_to_fit();
  }

  printf("%lld\\n", res);
}