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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
ll N, M, x;
ll a[MAX], b[MAX];
ll A[MAX], B[MAX];
vector<ll> s, t, p;

int main() {
  cin >> N >> M;
  s.resize(N);
  t.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> x;
    p.push_back(x);
  }
  p.push_back(0);
  p.push_back(1e9);
  sort(p.begin(), p.end());

  for (int i = 0; i + 1 < (int)p.size(); i++) {
    if (i % 2)
      a[i] = p[i + 1] - p[i];
    else
      b[i] = p[i + 1] - p[i];
  }
  A[0] = a[0];
  B[0] = b[0];
  for (int i = 0; i < (int)p.size(); i++) {
    A[i] = A[i - 1] + a[i];
    B[i] = B[i - 1] + b[i];
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll si = upper_bound(p.begin(), p.end(), s[i]) - p.begin() - 1;
    ll ti = upper_bound(p.begin(), p.end(), t[i]) - p.begin() - 1;

    if (s[i] < t[i]) {
      ans -= (s[i] - p[si]);
      if (a[si] > 0) {
        ans += A[ti];
        if (si)
          ans -= A[si - 1];
        if (a[ti] > 0)
          ans -= (p[ti + 1] - t[i]);
      } else if (b[si] > 0) {
        ans += B[ti];
        if (si)
          ans -= B[si - 1];
        if (b[ti] > 0)
          ans -= (p[ti + 1] - t[i]);
      }
    } else {
      if (s[i] == p[si])
        si--;
      ans -= (p[si + 1] - s[i]);
      if (a[si] > 0) {
        ans += A[si];
        if (ti)
          ans -= A[ti - 1];
        if (a[ti] > 0)
          ans -= (t[i] - p[ti]);
      } else {
        ans += B[si];
        if (ti)
          ans -= B[ti - 1];
        if (b[ti] > 0)
          ans -= (t[i] - p[ti]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;

const ll segN = (1LL << 30);

struct data {
  ll v, w;
  data *ch[2];

  void init() {
    v = w = 0;
    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 a, ll b, ll x, ll l, ll r) {
    ll m = (l + r) / 2;
    if (ch[0] == NULL)
      init();
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      v += x;
    } else {
      w += (min(b, r) - max(a, l)) * x;
      if (r - l == 1)
        return;
      ch[0]->add(a, b, x, l, m);
      ch[1]->add(a, b, x, m, r);
    }
  }

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

  ll sum(ll a, ll b, ll l, ll r) {
    //   cout<<a<<' '<<b<<' '<<l<<' '<<r<<' '<<endl;

    ll m = (l + r) / 2;
    if (ch[0] == NULL)
      init();
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return v * (r - l) + w;
    ll res = (min(b, r) - max(a, l)) * v;
    if (r - l == 1)
      return res;
    res += ch[0]->sum(a, b, l, m);
    res += ch[1]->sum(a, b, m, r);
    return res;
  }

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

ll N, M, x, y, z;
vector<ll> s, t, p;

data A, B;

int main() {
  A.init();
  B.init();
  cin >> N >> M;
  s.resize(N);
  t.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> x;
    p.push_back(x);
  }
  p.push_back(0);
  p.push_back(segN);
  sort(p.begin(), p.end());

  for (int i = 0; i + 1 < (int)p.size(); i++) {
    if (i % 2)
      A.add(p[i], p[i + 1], 1);
    else
      B.add(p[i], p[i + 1], 1);
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] < t[i]) {
      if (A.sum(s[i], s[i] + 1) > 0)
        ans += A.sum(s[i], t[i]);
      else
        ans += B.sum(s[i], t[i]);
    } else {
      if (A.sum(s[i] - 1, s[i]) > 0)
        ans += A.sum(t[i], s[i]);
      else
        ans += B.sum(t[i], s[i]);
    }
  }
  cout << ans << endl;
  return 0;
}