Language: C++

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< class T >
struct ArrayPool {
  vector< T > pool;
  vector< T * > stock;
  int ptr;

  ArrayPool(int sz) : pool(sz), stock(sz) {}

  inline T *alloc() { return stock[--ptr]; }

  inline void free(T *t) { stock[ptr++] = t; }

  void clear() {
    ptr = (int) pool.size();
    for(int i = 0; i < pool.size(); i++) stock[i] = &pool[i];
  }
};

template< class D, class L, D (*f)(D, D), D (*g)(D, L), L (*h)(L, L) >
struct RedBlackTree {
  enum COLOR {
    BLACK, RED
  };

  struct Node {
    Node *l, *r;
    bool color;
    unsigned char level;
    int cnt;
    D key, sum;
    L lazy;

    Node() {}

    Node(const D &k, const L &pp) :
        key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0), cnt(1), lazy(pp) {}

    Node(Node *l, Node *r, const D &k, const L &pp) :
        key(k), color(RED), l(l), r(r), lazy(pp) {}
  };

  ArrayPool< Node > pool;

  const D M1;
  const L OM0;

  RedBlackTree(int sz, const D &M1, const L &OM0) :
      pool(sz), M1(M1), OM0(OM0) {}


  inline Node *alloc(const D &key) {
    return &(*pool.alloc() = Node(key, OM0));
  }

  inline Node *alloc(Node *l, Node *r) {
    auto t = &(*pool.alloc() = Node(l, r, M1, OM0));
    return update(t);
  }

  virtual Node *clone(Node *t) { return t; }

  inline int count(const Node *t) { return t ? t->cnt : 0; }

  inline D sum(const Node *t) { return t ? t->sum : M1; }

  Node *update(Node *t) {
    t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r);
    t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0;
    t->sum = f(f(sum(t->l), t->key), sum(t->r));
    return t;
  }

  Node *propagate(Node *t) {
    t = clone(t);
    if(t->lazy != OM0) {
      if(!t->l) {
        t->key = g(t->key, t->lazy);
      } else {
        if(t->l) {
          t->l = clone(t->l);
          t->l->lazy = h(t->l->lazy, t->lazy);
          t->l->sum = g(t->l->sum, t->lazy);
        }
        if(t->r) {
          t->r = clone(t->r);
          t->r->lazy = h(t->r->lazy, t->lazy);
          t->r->sum = g(t->r->sum, t->lazy);
        }
      }
      t->lazy = OM0;
    }
    return update(t);
  }

  Node *rotate(Node *t, bool b) {
    t = propagate(t);
    Node *s;
    if(b) {
      s = propagate(t->l);
      t->l = s->r;
      s->r = t;
    } else {
      s = propagate(t->r);
      t->r = s->l;
      s->l = t;
    }
    update(t);
    return update(s);
  }

  Node *submerge(Node *l, Node *r) {
    if(l->level < r->level) {
      r = propagate(r);
      Node *c = (r->l = submerge(l, r->l));
      if(r->color == BLACK && c->color == RED && c->l && c->l->color == RED) {
        r->color = RED;
        c->color = BLACK;
        if(r->r->color == BLACK) return rotate(r, true);
        r->r->color = BLACK;
      }
      return update(r);
    }
    if(l->level > r->level) {
      l = propagate(l);
      Node *c = (l->r = submerge(l->r, r));
      if(l->color == BLACK && c->color == RED && c->r && c->r->color == RED) {
        l->color = RED;
        c->color = BLACK;
        if(l->l->color == BLACK) return rotate(l, false);
        l->l->color = BLACK;
      }
      return update(l);
    }
    return alloc(l, r);
  }

  Node *merge(Node *l, Node *r) {
    if(!l || !r) return l ? l : r;
    Node *c = submerge(l, r);
    c->color = BLACK;
    return c;
  }

  pair< Node *, Node * > split(Node *t, int k) {
    if(!t) return {nullptr, nullptr};
    t = propagate(t);
    if(k == 0) return {nullptr, t};
    if(k >= count(t)) return {t, nullptr};
    Node *l = t->l, *r = t->r;
    pool.free(t);
    if(k < count(l)) {
      auto pp = split(l, k);
      return {pp.first, merge(pp.second, r)};
    }
    if(k > count(l)) {
      auto pp = split(r, k - count(l));
      return {merge(l, pp.first), pp.second};
    }
    return {l, r};
  }

  Node *build(int l, int r, const vector< D > &v) {
    if(l + 1 >= r) return alloc(v[l]);
    return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
  }

  Node *build(const vector< D > &v) {
    pool.clear();
    return build(0, (int) v.size(), v);
  }

  void dump(Node *r, typename vector< D >::iterator &it, L lazy) {
    if(r->lazy != OM0) lazy = h(lazy, r->lazy);
    if(!r->l || !r->r) {
      *it++ = g(r->key, lazy);
      return;
    }
    dump(r->l, it, lazy);
    dump(r->r, it, lazy);
  }

  vector< D > dump(Node *r) {
    vector< D > v((size_t) count(r));
    auto it = begin(v);
    dump(r, it, OM0);
    return v;
  }

  string to_string(Node *r) {
    auto s = dump(r);
    string ret;
    for(int i = 0; i < s.size(); i++) {
      ret += std::to_string(s[i]);
      ret += ", ";
    }
    return (ret);
  }

  void insert(Node *&t, int k, const D &v) {
    auto x = split(t, k);
    t = merge(merge(x.first, alloc(v)), x.second);
  }

  D erase(Node *&t, int k) {
    auto x = split(t, k);
    auto y = split(x.second, 1);
    auto v = y.first->key;
    pool.free(y.first);
    t = merge(x.first, y.second);
    return v;
  }

  D query(Node *&t, int a, int b) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    auto ret = sum(y.first);
    t = merge(x.first, merge(y.first, y.second));
    return ret;
  }

  void set_propagate(Node *&t, int a, int b, const L &pp) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    y.first = clone(y.first);
    y.first->lazy = h(y.first->lazy, pp);
    t = merge(x.first, merge(propagate(y.first), y.second));
  }

  void set_element(Node *&t, int k, const D &x) {
    if(!t->l) {
      t->key = t->sum = x;
      return;
    }
    t = propagate(t);
    if(k < count(t->l)) set_element(t->l, k, x);
    else set_element(t->r, k - count(t->l), x);
    t = update(t);
  }

  int size(Node *t) {
    return count(t);
  }

  bool empty(Node *t) {
    return !t;
  }

  Node *makeset() {
    return nullptr;
  }
};

template< class D, class L, D (*f)(D, D), D (*g)(D, L), L (*h)(L, L) >
struct PersistentRedBlackTree : RedBlackTree< D, L, f, g, h > {
  using RBT = RedBlackTree< D, L, f, g, h >;
  using Node = typename RBT::Node;

  PersistentRedBlackTree(int sz, const D &M1, const L &OM0) :
      RBT(sz, M1, OM0) {}

  Node *clone(Node *t) override { return &(*RBT::pool.alloc() = *t); }

  Node *rebuild(Node *r) { return RBT::build(RBT::dump(r)); }
};


using pi = pair< int64, int64 >;

pi F(pi x, pi y) { return {x.first + y.first, x.second + y.second}; }

pi G(pi x, int64 y) { return {x.first + 1LL * y * x.second, x.second}; }

int H(int64 x, int64 y) { return x + y; }


int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector< int > A(M), B(M), C(M);
  for(int i = 0; i < M; i++) {
    cin >> A[i] >> B[i] >> C[i];
    --A[i];
  }
  vector< int > X(Q), Y(Q);
  vector< int64 > Z(Q);
  for(int i = 0; i < Q; i++) {
    cin >> X[i] >> Y[i] >> Z[i];
    --X[i];
  }
  auto xs(C);
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< vector< pair< int, int > > > range(xs.size());
  for(int i = 0; i < M; i++) {
    C[i] = lower_bound(begin(xs), end(xs), C[i]) - begin(xs);
    range[C[i]].emplace_back(A[i], B[i]);
  }

  const int BACKET = 200;

  for(int z = 0; z < Q; z += BACKET) {
    int l = z * BACKET;
    int r = min(z + BACKET, Q);
    vector< int > ys;
    for(int i = 0; i < M; i++) {
      ys.emplace_back(A[i]);
      ys.emplace_back(B[i]);
    }
    for(int i = l; i < r; i++) {
      ys.emplace_back(X[i]);
      ys.emplace_back(Y[i]);
    }

    sort(begin(ys), end(ys));
    ys.erase(unique(begin(ys), end(ys)), end(ys));


    using PRBT= PersistentRedBlackTree< pi, int64, F, G, H >;
    PRBT beet((int) (6e6), pi(), 0);
    vector< pi > uku;
    for(int i = 0; i + 1 < ys.size(); i++) uku.emplace_back(0, ys[i + 1] - ys[i]);
    auto root = beet.build(uku);
    vector< PRBT::Node * > nodes;
    for(int i = 0; i < range.size(); i++) {
      for(auto t : range[i]) {
        t.first = lower_bound(begin(ys), end(ys), t.first) - begin(ys);
        t.second = lower_bound(begin(ys), end(ys), t.second) - begin(ys);
        beet.set_propagate(root, t.first, t.second, 1);
      }
      nodes.emplace_back(root);
    }
    for(int i = l; i < r; i++) {
      X[i] = lower_bound(begin(ys), end(ys), X[i]) - begin(ys);
      Y[i] = lower_bound(begin(ys), end(ys), Y[i]) - begin(ys);
      int ok = xs.size(), ng = -1;
      auto check = [&](int v) {
        return beet.query(nodes[v], X[i], Y[i]).first >= Z[i];
      };
      while(ok - ng > 1) {
        int mid = (ok + ng) / 2;
        if(check(mid)) ok = mid;
        else ng = mid;
      }
      cout << xs[ok] << endl;
    }

  }

}


Compiler output:
a.cc: In function 'int main()':
a.cc:390:60: error: conversion from 'int (*)(int64, int64)' {aka 'int (*)(long long int, long long int)'} to 'long long int (*)(long long int, long long int)' in a converted constant expression
  390 |     using PRBT= PersistentRedBlackTree< pi, int64, F, G, H >;
      |                                                            ^
a.cc:390:60: error: could not convert 'H' from 'int(int64, int64)' {aka 'int(long long int, long long int)'} to 'long long int (*)(long long int, long long int)'
  390 |     using PRBT= PersistentRedBlackTree< pi, int64, F, G, H >;
      |                                                            ^
      |                                                            |
      |                                                            int(int64, int64) {aka int(long long int, long long int)}
a.cc:391:5: error: 'PRBT' was not declared in this scope
  391 |     PRBT beet((int) (6e6), pi(), 0);
      |     ^~~~
a.cc:394:17: error: 'beet' was not declared in this scope
  394 |     auto root = beet.build(uku);
      |                 ^~~~
a.cc:395:26: error: template argument 2 is invalid
  395 |     vector< PRBT::Node * > nodes;
      |                          ^
a.cc:402:13: error: request for member 'emplace_back' in 'nodes', which is of non-class type 'int'
  402 |       nodes.emplace_back(root);
      |             ^~~~~~~~~~~~
a.cc: In lambda function:
a.cc:409:32: error: invalid types 'int[int]' for array subscript
  409 |         return beet.query(nodes[v], X[i], Y[i]).first >= Z[i];
      |                                ^