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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, long long> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define MAX_N (1 << 19)
vector<int> seg[MAX_N * 2 - 1];
vector<P> comp[MAX_N * 2 - 1];

vector<int> xs;
long long query(int a, int b, int x, int k = 0, int l = 0, int r = MAX_N) {
  if (b <= l || r <= a)
    return 0;
  if (a <= l && r <= b) {
    auto it = upper_bound(all(comp[k]), P(x, 1LL << 60));
    if (it == comp[k].begin())
      return 0;
    return (--it)->_2;
  }
  long long s = 1LL * (xs[min(b, r)] - xs[max(a, l)]) *
                (upper_bound(all(seg[k]), x) - seg[k].begin());
  s += query(a, b, x, k * 2 + 1, l, (l + r) / 2);
  s += query(a, b, x, k * 2 + 2, (l + r) / 2, r);
  return s;
}
void add(int a, int b, int x, int k = 0, int l = 0, int r = MAX_N) {
  if (b <= l || r <= a)
    return;
  comp[k].pb(P(x, xs[min(b, r)] - xs[max(a, l)]));
  if (a <= l && r <= b) {
    seg[k].pb(x);
    return;
  }
  add(a, b, x, k * 2 + 1, l, (l + r) / 2);
  add(a, b, x, k * 2 + 2, (l + r) / 2, r);
}

int N, M, Q;
int A[100000], B[100000], C[100000];
int X[100000], Y[100000];
long long J[100000];

signed main() {
  cin >> N >> M >> Q;
  rep(i, M) {
    cin >> A[i] >> B[i] >> C[i];
    A[i]--;
    xs.pb(A[i]), xs.pb(B[i]);
  }
  rep(i, Q) {
    cin >> X[i] >> Y[i] >> J[i];
    X[i]--;
    xs.pb(X[i]), xs.pb(Y[i]);
  }
  sort(all(xs));
  uniq(xs);
  rep(i, M) A[i] = index(xs, A[i]), B[i] = index(xs, B[i]);
  rep(i, Q) X[i] = index(xs, X[i]), Y[i] = index(xs, Y[i]);

  rep(i, M) add(A[i], B[i], C[i]);
  rep(i, MAX_N * 2 - 1) {
    sort(all(seg[i]));
    sort(all(comp[i]));
    if (!comp[i].empty())
      rep(j, comp[i].size() - 1) comp[i][j + 1]._2 += comp[i][j]._2;
  }

  rep(i, Q) {
    int l = X[i], r = Y[i];
    long long k = J[i];
    long long lo = 0, hi = INF;
    while (hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if (query(l, r, mid) >= k)
        hi = mid;
      else
        lo = mid;
    }
    cout << hi << "\\n";
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, long long> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define MAX_N (1 << 30)
#define POOL 6300000
// vector<int> seg[POOL];
// vector<P> comp[POOL];
// int lch[POOL], rch[POOL];
int V = 0;
vector<vector<int>> seg;
vector<vector<P>> comp;
// vector<int> lch, rch;
vector<int> lch(POOL, -1);
int alloc() {
  int k = V++;
  assert(k < POOL);
  seg.pb({});
  comp.pb({});
  // lch.pb(-1), rch.pb(-1);
  // lch[k] = rch[k] = -1;
  return k;
}
void load(int k) {
  lch[k] = alloc();
  alloc();
}
long long query(int a, int b, int x, int k = 0, int l = 0, int r = MAX_N) {
  if (b <= l || r <= a)
    return 0;
  if (a <= l && r <= b) {
    auto it = upper_bound(all(comp[k]), P(x, 1LL << 60));
    if (it == comp[k].begin())
      return 0;
    return (--it)->_2;
  }
  long long s = 1LL * (min(b, r) - max(a, l)) *
                (upper_bound(all(seg[k]), x) - seg[k].begin());
  if (lch[k] == -1)
    load(k);
  s += query(a, b, x, lch[k], l, (l + r) / 2);
  s += query(a, b, x, lch[k] + 1, (l + r) / 2, r);
  return s;
}
void add(int a, int b, int x, int k = 0, int l = 0, int r = MAX_N) {
  if (b <= l || r <= a)
    return;
  comp[k].pb(P(x, min(b, r) - max(a, l)));
  if (a <= l && r <= b) {
    seg[k].pb(x);
    return;
  }
  if (lch[k] == -1)
    load(k);
  add(a, b, x, lch[k], l, (l + r) / 2);
  add(a, b, x, lch[k] + 1, (l + r) / 2, r);
}

int N, M, Q;
signed main() {
  cin >> N >> M >> Q;
  alloc();
  rep(i, M) {
    int a, b, x;
    cin >> a >> b >> x;
    a--, b--;
    add(a, b + 1, x);
  }
  rep(i, V) {
    sort(all(seg[i]));
    sort(all(comp[i]));
    if (!comp[i].empty())
      rep(j, comp[i].size() - 1) comp[i][j + 1]._2 += comp[i][j]._2;
  }

  rep(i, Q) {
    int l, r;
    long long k;
    cin >> l >> r >> k;
    l--, r--;
    long long lo = 0, hi = INF;
    while (hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if (query(l, r + 1, mid) >= k)
        hi = mid;
      else
        lo = mid;
    }
    cout << hi << "\\n";
  }
  return 0;
}