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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using ll = long long int;

constexpr int MAX_N = (1 << 18), INF = 2 * (int)1e6;

using P = std::pair<int, int>; // max,min

int n, d;

std::map<int, P> mapX, mapY;

std::vector<P> maxmin;

std::vector<int> vec, res, ansX, ansY;

P calc(const P &lhs, const P &rhs) {

  return P(std::max(lhs.first, rhs.first), std::min(lhs.second, rhs.second));
}

P seg[MAX_N * 2 - 1];

void init(int _n) {

  n = 1;

  while (n < _n)
    n *= 2;

  for (int i = 0; i < n * 2 - 1; ++i)
    seg[i] = P(0, INF);

  for (int i = 0; i < _n; ++i)
    seg[i + n - 1] = maxmin[i];

  for (int i = n - 2; i >= 0; --i)
    seg[i] = calc(seg[i * 2 + 1], seg[i * 2 + 2]);
}

P query(int a, int b, int k, int l, int r) {

  if (b <= l || r <= a)
    return P(0, INF);

  if (a <= l && r <= b)
    return seg[k];

  return calc(query(a, b, k * 2 + 1, l, (l + r) / 2),
              query(a, b, k * 2 + 2, (l + r) / 2, r));
}

std::vector<int> solve(std::map<int, P> &map) {

  res.clear();
  maxmin.clear();
  vec.clear();

  for (auto &&s : map) {
    vec.emplace_back(s.first);
    maxmin.emplace_back(s.second);
  }

  init(maxmin.size());

  for (int i = 0; i < vec.size(); ++i) {
    P p = query(
        i, std::upper_bound(vec.begin(), vec.end(), vec[i] + d) - vec.begin(),
        0, 0, n);
    res.emplace_back(p.first - p.second);
  }

  return res;
}

int main() {

  std::cin >> n >> d;

  int x, y, b;

  for (int i = 0; i < n; ++i) {

    std::cin >> x >> y >> b;

    if (!mapX.count(b)) {
      mapX[b] = P(x, x);
      mapY[b] = P(y, y);
    } else {
      mapX[b] = P(std::max(mapX[b].first, x), std::min(mapX[b].second, x));
      mapY[b] = P(std::max(mapY[b].first, y), std::min(mapY[b].second, y));
    }
  }

  ansX = solve(mapX);

  ansY = solve(mapY);

  ll ans = 0;

  for (int i = 0; i < ansX.size(); ++i)
    ans = std::max(ans, ll(ansX[i]) * ll(ansY[i]));

  std::cout << ans << std::endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using ll = long long int;

constexpr int MAX_N = (1 << 18), INF = 2 * (int)1e6;

using P = std::pair<int, int>; // max,min

int n, d;

std::map<int, std::set<int>> mapX, mapY;

std::vector<P> maxmin;

std::vector<int> vec, res;

P calc(const P &lhs, const P &rhs) {

  return P(std::max(lhs.first, rhs.first), std::min(lhs.second, rhs.second));
}

P seg[MAX_N * 2 - 1];

void init(int _n) {

  n = 1;

  while (n < _n)
    n *= 2;

  for (int i = 0; i < n * 2 - 1; ++i)
    seg[i] = P(0, INF);

  for (int i = 0; i < _n; ++i)
    seg[i + n - 1] = maxmin[i];

  for (int i = n - 2; i >= 0; --i)
    seg[i] = calc(seg[i * 2 + 1], seg[i * 2 + 2]);
}

P query(int a, int b, int k, int l, int r) {

  if (b <= l || r <= a)
    return P(0, INF);

  if (a <= l && r <= b)
    return seg[k];

  return calc(query(a, b, k * 2 + 1, l, (l + r) / 2),
              query(a, b, k * 2 + 2, (l + r) / 2, r));
}

std::vector<int> solve(std::map<int, std::set<int>> &map) {

  res.clear();
  maxmin.clear();
  vec.clear();

  for (auto &&s : map) {
    vec.emplace_back(s.first);
    maxmin.emplace_back(*(s.second.rbegin()), *(s.second.begin()));
  }

  init(maxmin.size());

  for (int i = 0; i < vec.size(); ++i) {
    P p = query(
        i, std::upper_bound(vec.begin(), vec.end(), vec[i] + d) - vec.begin(),
        0, 0, n);
    res.emplace_back(p.first - p.second);
  }

  return res;
}

int main() {

  std::cin >> n >> d;

  int x, y, b;

  for (int i = 0; i < n; ++i) {

    std::cin >> x >> y >> b;

    mapX[b].emplace(x);
    mapY[b].emplace(y);
  }

  std::vector<int> ansX = solve(mapX);

  std::vector<int> ansY = solve(mapY);

  ll ans = 0;

  for (int i = 0; i < ansX.size(); ++i)
    ans = std::max(ans, ll(ansX[i]) * ll(ansY[i]));

  std::cout << ans << std::endl;

  return 0;
}