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

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

typedef long long ll;
const ll segN = (1LL << 32);
vector<ll> G;
struct data {
  vector<ll> *v;
  bool flg;
  data *ch[2];

  void init() {
    v = new vector<ll>;
    flg = false;
    ch[0] = (data *)malloc(sizeof(data));
    ch[1] = (data *)malloc(sizeof(data));
    ch[0]->ch[0] = NULL;
    ch[1]->ch[0] = NULL;
  }

  void add(ll i, ll x, ll l, ll r) {
    //    cout<<i<<' '<<x<<' '<<l<<' '<<r<<endl;
    ll m = (l + r) / 2;
    if (ch[0] == NULL)
      init();
    if (i < l || r <= i)
      return;
    v->push_back(x);
    if (r - l == 1)
      return;
    ch[0]->add(i, x, l, m);
    ch[1]->add(i, x, m, r);
  }

  void add(ll i, ll x) { add(i, x, 0, segN); }

  ll sum(ll a, ll b, ll c, ll d, ll l, ll r) {
    ll m = (l + r) / 2;
    if (ch[0] == NULL)
      return 0;
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      if (!flg)
        sort(v->begin(), v->end());
      flg = true;
      vector<ll>::iterator A, B;
      A = lower_bound(v->begin(), v->end(), c);
      B = upper_bound(v->begin(), v->end(), d);
      return B - A;
    }

    ll res = 0;
    res += ch[0]->sum(a, b, c, d, l, m);
    res += ch[1]->sum(a, b, c, d, m, r);
    return res;
  }

  ll sum(ll a, ll b, ll c, ll d) { return sum(a, b, c, d, 0, segN); }
};

data T;

int main() {
  ll d = 1e9;
  int n, m;
  int ax, ay, bx, by;
  T.init();
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &ax, &ay);
    T.add(ax + d, ay);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d %d", &ax, &ay, &bx, &by);
    cout << T.sum(ax + d, bx + d + 1, ay, by) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
const ll segN = (1LL << 32);
vector<ll> G;
struct data {
  vector<ll> *v;
  bool flg;
  data *ch[2];

  void init() {
    v = new vector<ll>;
    flg = false;
    ch[0] = (data *)malloc(sizeof(data));
    ch[1] = (data *)malloc(sizeof(data));
    ch[0]->ch[0] = NULL;
    ch[1]->ch[0] = NULL;
  }

  void add(ll i, ll x, ll l, ll r) {
    //    cout<<i<<' '<<x<<' '<<l<<' '<<r<<endl;
    ll m = (l + r) / 2;
    if (ch[0] == NULL)
      init();
    if (i < l || r <= i)
      return;
    v->push_back(x);
    if (r - l == 1)
      return;
    ch[0]->add(i, x, l, m);
    ch[1]->add(i, x, m, r);
  }

  void add(ll i, ll x) { add(i, x, 0, segN); }

  ll sum(ll a, ll b, ll c, ll d, ll l, ll r) {
    ll m = (l + r) / 2;
    if (ch[0] == NULL)
      init();
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      if (!flg)
        sort(v->begin(), v->end());
      flg = true;
      vector<ll>::iterator A, B;
      A = lower_bound(v->begin(), v->end(), c);
      B = upper_bound(v->begin(), v->end(), d);
      return B - A;
    }

    ll res = 0;
    res += ch[0]->sum(a, b, c, d, l, m);
    res += ch[1]->sum(a, b, c, d, m, r);
    return res;
  }

  ll sum(ll a, ll b, ll c, ll d) { return sum(a, b, c, d, 0, segN); }
};

data T;

int main() {
  ll d = 1e9;
  int n, m;
  int ax, ay, bx, by;
  T.init();
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &ax, &ay);
    T.add(ax + d, ay);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d %d", &ax, &ay, &bx, &by);
    cout << T.sum(ax + d, bx + d + 1, ay, by) << endl;
  }
  return 0;
}