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

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

using ll = long long;

template <typename T> class node {
public:
  node<T> *l, *r;
  T val, val2;
  node() : val2(0) {}
  node(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), val2(0) {}
  void init(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr) {
    val = val_;
    l = l_;
    r = r_;
  }
};

template <typename T> class DynamicLazySegmentTree {
  using func_t = function<T(T, T)>;
  const ll n;
  const T id;
  func_t merge;
  node<T> *root;
  vector<node<T>> pool;
  int it;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  node<T> *new_node() {
    pool[it].init(id);
    return &pool[it++];
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return id;
    if (l <= lb && ub <= r)
      return n->val + n->val2 * (ub - lb);
    return n->val2 * (min(r, ub) - max(l, lb)) +
           merge(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : id,
                 n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : id);
  }
  void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return;
    if (l <= lb && ub <= r) {
      n->val2 += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new_node();
    }
    if (n->r == nullptr) {
      n->r = new_node();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(
      ll n_, T id_ = 0, func_t merge_ = [](ll a, ll b) { return a + b; },
      int max_ = 10000000)
      : n(size(n_)), id(id_), merge(merge_), pool(max_) {
    it = 0;
    root = new_node();
  }
  void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
  T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};

int main() {
  cin.sync_with_stdio(false);
  ll M, N, Q;
  cin >> N >> M >> Q;
  vector<ll> a(M), b(M), v(M), vs;
  vector<tuple<ll, ll, ll>> vab(M);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i] >> v[i];
    a[i]--;
    b[i]--;
    vs.push_back(v[i]);
    vab[i] = make_tuple(v[i], a[i], b[i]);
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  sort(vab.begin(), vab.end());
  for (int i = 0; i < M; i++) {
    a[i] = get<1>(vab[i]);
    b[i] = get<2>(vab[i]);
    v[i] = lower_bound(vs.begin(), vs.end(), get<0>(vab[i])) - vs.begin();
  }
  ll vss = vs.size();
  vector<ll> x(Q), y(Q), j(Q);
  vector<ll> l(Q, -1), r(Q, vss - 1);
  for (int i = 0; i < Q; i++) {
    cin >> x[i] >> y[i] >> j[i];
    x[i]--;
    y[i]--;
  }
  for (int cnt = 0; cnt < 18; cnt++) {
    vector<vector<int>> vv(vss);
    for (int i = 0; i < Q; i++) {
      vv[(l[i] + r[i]) / 2].push_back(i);
    }
    DynamicLazySegmentTree<ll> dlst(N);
    ll tmp = 0;
    for (ll i = 0; i < vss; i++) {
      for (; tmp < M && v[tmp] <= i; tmp++) {
        dlst.add(a[tmp], b[tmp], 1);
      }
      for (auto q : vv[i]) {
        if (dlst.getSum(x[q], y[q]) < j[q]) {
          l[q] = i;
        } else {
          r[q] = i;
        }
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    printf("%d\\n", (int)vs[r[i]]);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;

template <typename T> class node {
public:
  node<T> *l, *r;
  T val, val2;
  node() : val2(0) {}
  node(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), val2(0) {}
  void init(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr) {
    val = val_;
    l = l_;
    r = r_;
  }
};

template <typename T> class DynamicLazySegmentTree {
  using func_t = function<T(T, T)>;
  const ll n;
  const T id;
  func_t merge;
  node<T> *root;
  vector<node<T>> pool;
  int it;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  node<T> *new_node() {
    pool[it].init(id);
    return &pool[it++];
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return id;
    if (l <= lb && ub <= r)
      return n->val + n->val2 * (ub - lb);
    return n->val2 * (min(r, ub) - max(l, lb)) +
           merge(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : id,
                 n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : id);
  }
  void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return;
    if (l <= lb && ub <= r) {
      n->val2 += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new_node();
    }
    if (n->r == nullptr) {
      n->r = new_node();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(
      ll n_, T id_ = 0, func_t merge_ = [](ll a, ll b) { return a + b; },
      int max_ = 10000000)
      : n(size(n_)), id(id_), merge(merge_), pool(max_) {
    it = 0;
    root = new_node();
  }
  void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
  T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};

int main() {
  cin.sync_with_stdio(false);
  ll M, N, Q;
  cin >> N >> M >> Q;
  vector<ll> a(M), b(M), v(M), vs;
  vector<tuple<ll, ll, ll>> vab(M);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i] >> v[i];
    a[i]--;
    b[i]--;
    vs.push_back(v[i]);
    vab[i] = make_tuple(v[i], a[i], b[i]);
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());
  sort(vab.begin(), vab.end());
  for (int i = 0; i < M; i++) {
    a[i] = get<1>(vab[i]);
    b[i] = get<2>(vab[i]);
    v[i] = lower_bound(vs.begin(), vs.end(), get<0>(vab[i])) - vs.begin();
  }
  ll vss = vs.size();
  vector<ll> x(Q), y(Q), j(Q);
  vector<ll> l(Q, -1), r(Q, vss - 1);
  for (int i = 0; i < Q; i++) {
    cin >> x[i] >> y[i] >> j[i];
    x[i]--;
    y[i]--;
  }
  for (int cnt = 0; cnt < 30; cnt++) {
    vector<vector<int>> vv(vss);
    for (int i = 0; i < Q; i++) {
      vv[(l[i] + r[i]) / 2].push_back(i);
    }
    DynamicLazySegmentTree<ll> dlst(N);
    ll tmp = 0;
    for (ll i = 0; i < vss; i++) {
      for (; tmp < M && v[tmp] <= i; tmp++) {
        dlst.add(a[tmp], b[tmp], 1);
      }
      for (auto q : vv[i]) {
        if (dlst.getSum(x[q], y[q]) < j[q]) {
          l[q] = i;
        } else {
          r[q] = i;
        }
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    cout << vs[r[i]] << endl;
  }
  return 0;
}