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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Point;
enum : int { CREATEY, APPEARX, DELETEY };

struct Event {
  int y, idx, type;
  bool operator<(const Event &e) const {
    if (y == e.y)
      return (type < e.type);
    return (y < e.y);
  }
};

struct BinaryIndexedTree {
  vector<int> data;
  BinaryIndexedTree(int sz) { data.assign(sz + 2, 0); }
  int query(int k) {
    int ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }
  void update(int k, int x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

struct SegmentTree {
  vector<int> isnew;
  int sz;
  SegmentTree(int n) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    isnew.assign(2 * sz - 1, false);
  }
  inline void AddF(const int a, const int b, int k, int l, int r) {
    if (a >= r || b <= l) {
      return;
    } else if (a <= l && r <= b) {
      isnew[k] = true;
    } else {
      AddF(a, b, 2 * k + 1, l, (l + r) >> 1);
      AddF(a, b, 2 * k + 2, (l + r) >> 1, r);
    }
  }
  inline void DelF(const int a, const int b, int k, int l, int r) {
    if (a >= r || b <= l) {
      return;
    } else if (a <= l && r <= a + 1) {
      isnew[k] = false;
    } else {
      if (isnew[k]) {
        isnew[2 * k + 1] = true;
        isnew[2 * k + 2] = true;
        isnew[k] = false;
      }
      DelF(a, b, 2 * k + 1, l, (l + r) >> 1);
      DelF(a, b, 2 * k + 2, (l + r) >> 1, r);
    }
  }
  inline void AddF(int a, int b) { AddF(a, b, 0, 0, sz); }
  inline void DelF(const int x) { DelF(x, x + 1, 0, 0, sz); }
  inline bool CheckF(int k) {
    k += sz - 1;
    if (isnew[k])
      return (true);
    while (k > 0) {
      k = (k - 1) >> 1;
      if (isnew[k])
        return (true);
    }
    while (k > 0)
      ;
    return (false);
  }
};

struct UnionFind {
  vector<int> data;
  UnionFind(){};
  inline int Make() {
    data.push_back(-1);
    return (data.size() - 1);
  }
  inline bool Unite(int a, int b) {
    a = Find(a), b = Find(b);
    if (a == b)
      return (false);
    if (data[a] > data[b])
      swap(a, b);
    data[a] += data[b];
    data[b] = a;
    return (true);
  }
  inline int Find(int k) {
    if (data[k] < 0)
      return (k);
    else
      return (data[k] = Find(data[k]));
  }
};

SegmentTree *tree;
UnionFind *uf;
BinaryIndexedTree *bit;
int decode[200020];

int Evaluate(int x) {
  if (tree->CheckF(x)) {
    tree->DelF(x);
    return (uf->Make());
  } else {
    return (decode[x]);
  }
}

int Press(int *A, int sz1, int *B, int sz2) {
  vector<int> nums;
  copy(A, A + sz1, back_inserter(nums));
  copy(B, B + sz2, back_inserter(nums));
  nums.push_back(-1);
  sort(nums.begin(), nums.end());
  nums.erase(unique(nums.begin(), nums.end()), nums.end());
  transform(A, A + sz1, A, [&](int x) {
    return (lower_bound(nums.begin(), nums.end(), x) - nums.begin());
  });
  transform(B, B + sz2, B, [&](int x) {
    return (lower_bound(nums.begin(), nums.end(), x) - nums.begin());
  });
  return (nums.size());
}

int main() {
  int W, H, N;
  int A[100004], B[100004], C[100004], D[100004];

  scanf("%d %d %d", &W, &H, &N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d %d %d", &A[i], &B[i], &C[i], &D[i]);
  }

  A[N] = 0;
  B[N] = 0;
  C[N] = W;
  D[N] = 0;
  ++N;
  A[N] = 0;
  B[N] = 0;
  C[N] = 0;
  D[N] = H;
  ++N;
  A[N] = W;
  B[N] = 0;
  C[N] = W;
  D[N] = H;
  ++N;
  A[N] = 0;
  B[N] = H;
  C[N] = W;
  D[N] = H;
  ++N;

  W = Press(A, N, C, N);

  vector<Event> Events;
  /*
    type = CREATEY: Y?????????????????????
    type = DELETEY: Y????????????????¶????
    type = APPEARX: X????????????
  */
  for (int i = 0; i < N; i++) {
    if (A[i] == C[i]) {
      Events.push_back((Event){B[i], i, CREATEY});
      Events.push_back((Event){D[i], i, DELETEY});
    } else {
      Events.push_back((Event){B[i], i, APPEARX});
    }
  }
  sort(Events.begin(), Events.end());

  set<int> pool;
  uf = new UnionFind();
  tree = new SegmentTree(W);
  bit = new BinaryIndexedTree(W);
  pool.insert(0);
  decode[0] = uf->Make();
  long long int sz = 0;

  for (int i = 0; i < Events.size(); i++) {
    const Event &e = Events[i];
    const int &x = A[e.idx];
    if (e.type == CREATEY) {
      int L = *--pool.lower_bound(x);
      decode[x] = Evaluate(x);
      decode[L] = Evaluate(L);
      decode[x] = decode[L];
      pool.insert(x);
      bit->update(x, 1);
    } else if (e.type == DELETEY) {
      int L = *--pool.lower_bound(x);
      decode[x] = Evaluate(x);
      decode[L] = Evaluate(L);
      if (uf->Unite(decode[x], decode[L]))
        --sz;
      pool.erase(x);
      bit->update(x, -1);
    } else {
      int x2 = C[e.idx];
      int RSQ = bit->query(x2) - bit->query(x - 1) - 1;
      if (RSQ > 0) {
        sz += RSQ;
        tree->AddF(x, *--pool.upper_bound(x2));
      }
    }
  }
  printf("%lld\\n", sz);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Point;
struct Event {
  int y, idx, type;
  bool operator<(const Event &e) const {
    if (y == e.y)
      return (type < e.type);
    return (y < e.y);
  }
};

struct UnionFind {
  vector<int> data;
  int Inner;
  UnionFind() : Inner(0){};
  int Make() {
    ++Inner;
    data.push_back(-1);
    return (data.size() - 1);
  }
  void Unite(int a, int b) {
    a = Find(a), b = Find(b);
    if (a == b)
      return;
    --Inner;
    if (data[a] > data[b])
      swap(a, b);
    data[a] += data[b];
    data[b] = a;
  }
  int Find(int k) {
    if (data[k] < 0)
      return (k);
    else
      return (data[k] = Find(data[k]));
  }
  int Size() { return (Inner); }
};

int main() {
  int W, H, N;
  vector<Point> S, T;
  vector<int> X, Y;

  scanf("%d %d %d", &W, &H, &N);
  for (int i = 0; i < N; i++) {
    int A, B, C, D;
    scanf("%d %d %d %d", &A, &B, &C, &D);

    X.push_back(A), X.push_back(C);
    Y.push_back(B), Y.push_back(D);
    S.push_back(Point(A, B));
    T.push_back(Point(C, D));
  }

  S.push_back(Point(0, 0));
  T.push_back(Point(W, 0));
  S.push_back(Point(0, 0));
  T.push_back(Point(0, H));
  S.push_back(Point(W, 0));
  T.push_back(Point(W, H));
  S.push_back(Point(0, H));
  T.push_back(Point(W, H));

  N += 4;

  X.push_back(0), X.push_back(W);
  Y.push_back(0), Y.push_back(H);

  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());

  for (int i = 0; i < N; i++) {
    S[i].first = lower_bound(X.begin(), X.end(), S[i].first) - X.begin();
    S[i].second = lower_bound(Y.begin(), Y.end(), S[i].second) - Y.begin();
    T[i].first = lower_bound(X.begin(), X.end(), T[i].first) - X.begin();
    T[i].second = lower_bound(Y.begin(), Y.end(), T[i].second) - Y.begin();
  }

  vector<Event> Events;
  /*
    type = 1: Y?????????????????????
    type = 3: Y????????????????¶????
    type = 2: X????????????
  */
  for (int i = 0; i < N; i++) {
    if (S[i].first == T[i].first) {
      Events.push_back((Event){S[i].second, i, 1});
      Events.push_back((Event){T[i].second, i, 3});
    } else {
      Events.push_back((Event){S[i].second, i, 2});
    }
  }
  sort(Events.begin(), Events.end());

  UnionFind uf;
  set<Point> Pool;
  Pool.insert(Point(-1145141919, uf.Make()));

  // ???????????§???????????????(??????

  int sz = -1;
  for (int i = 0; i < Events.size(); i++) {
    const Event &e = Events[i];
    const int &x = S[e.idx].first;

    if (e.type == 1) {

      set<Point>::iterator ptr = --Pool.lower_bound(Point(x, -1));
      Pool.insert(Point(x, ptr->second));
    } else if (e.type == 3) {

      set<Point>::iterator ptr = --Pool.lower_bound(Point(x, -1));

      int u = ptr->second;
      ++ptr;
      int v = ptr->second;
      uf.Unite(u, v);
      Pool.erase(ptr);

    } else {
      const int &x2 = T[e.idx].first;

      queue<Point> next;
      set<Point>::iterator ptr = Pool.lower_bound(Point(x, -1));
      set<Point>::iterator ptr2 = Pool.lower_bound(Point(x2, 11451419));
      if (ptr == ptr2)
        continue;
      --ptr2;
      while (ptr !=
             ptr2) { // ???????????°??¨???????????¨????????????????????????
        next.push(Point(ptr->first, uf.Make()));
        ++ptr;
      }

      while (!next.empty()) {
        Pool.erase(Pool.lower_bound(Point(next.front().first, -1)));
        Pool.insert(next.front());
        next.pop();
      }
    }
  }

  cout << uf.Size() - 1 << endl;
}