Problem:
I - The J-th Number

Problem Statement

You are given N empty arrays, t_1, ..., t_n. At first, you execute M queries as follows.

add a value v to array t_i (a \leq i \leq b)

Next, you process Q following output queries.

output the j-th number of the sequence sorted all values in t_i (x \leq i \leq y)

Input

The dataset is formatted as follows.

N M Q
a_1 b_1 v_1
...
a_M b_M v_M
x_1 y_1 j_1
...
x_Q y_Q j_Q

The first line contains three integers N (1 \leq N \leq 10^9), M (1 \leq M \leq 10^5) and Q (1 \leq Q \leq 10^5). Each of the following M lines consists of three integers a_i, b_i and v_i (1 \leq a_i \leq b_i \leq N, 1 \leq v_i \leq 10^9). Finally the following Q lines give the list of output queries, each of these lines consists of three integers x_i, y_i and j_i (1 \leq x_i \leq y_i \leq N, 1 \leq j_i \leq Σ_{x_i \leq k \leq y_i} |t_k|).

Output

For each output query, print in a line the j-th number.

Sample Input 1

5 4 1
1 5 1
1 1 3
4 5 1
3 4 2
1 3 4

Output for the Sample Input 1

2

After the M-th query is executed, each t_i is as follows:

[1,3], [1], [1,2], [1,1,2], [1,1]

The sequence sorted values in t_1, t_2 and t_3 is [1,1,1,2,3]. In the sequence, the 4-th number is 2.

Sample Input 2

10 4 4
1 4 11
2 3 22
6 9 33
8 9 44
1 1 1
4 5 1
4 6 2
1 10 12

Output for the Sample Input 2

11
11
33
44

Solution in C++14 (judge verdict: Memory Limit Exceeded)

#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, int >;

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

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

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


int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector< int > A(M), B(M), C(M);
  vector< int > ys;
  for(int i = 0; i < M; i++) {
    cin >> A[i] >> B[i] >> C[i];
    --A[i];
    ys.emplace_back(A[i]);
    ys.emplace_back(B[i]);
  }
  vector< int > X(Q), Y(Q), Z(Q);
  for(int i = 0; i < Q; i++) {
    cin >> X[i] >> Y[i] >> Z[i];
    --X[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));
  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++) {
    A[i] = lower_bound(begin(ys), end(ys), A[i]) - begin(ys);
    B[i] = lower_bound(begin(ys), end(ys), B[i]) - begin(ys);
    C[i] = lower_bound(begin(xs), end(xs), C[i]) - begin(xs);
    range[C[i]].emplace_back(A[i], B[i]);
  }
  for(int i = 0; i < Q; 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);
  }
  using PRBT= PersistentRedBlackTree< pi, int, F, G, H >;
  PRBT beet((int) (7.5e6), 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]) beet.set_propagate(root, t.first, t.second, 1);
    nodes.emplace_back(root);
  }
  for(int i = 0; i < Q; i++) {
    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;
  }
}