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: Runtime Error)

#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_ = 1000000) :
		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);
	int M, N, Q;
	cin >> N >> M >> Q;
	vector<int> a(M), b(M), v(M), vs;
	vector<tuple<int, int, int>> 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();
	}
	int vss = vs.size();
	vector<int> x(Q), y(Q), j(Q);
	vector<int> l(Q), r(Q, vss);
	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<int> dlst(N);
		int tmp = 0;
		for (int 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;
}