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"
#include<random>
using namespace std;

using Value = long long int;
const Value INI = 0;


namespace perseg {

	struct permanent_lazy_segtree {
		Value val;
		Value lazy;
		int l, r;
		vector<shared_ptr<permanent_lazy_segtree>>chs;
	public:
		permanent_lazy_segtree(const int size_) :val(INI), lazy(INI), l(0), r(size_), chs(2, nullptr) {

		}
		permanent_lazy_segtree(const Value& val_, const int l_, const int r_) :val(val_), lazy(INI), l(l_), r(r_), chs(2, nullptr) {

		}
		permanent_lazy_segtree(const permanent_lazy_segtree&tree_) :val(tree_.val), lazy(tree_.lazy), l(tree_.l), r(tree_.r), chs(tree_.chs) {

		}
		void lazy_func() {
			val += lazy*(r - l);
		}
		Value update_lazy(int lazy_from, int lazy_to) {
			return lazy_from + lazy_to;
		}
		void lazy_evaluate_node() {
			lazy_func();
			if (l + 1 != r) {
				for (int i = 0; i < 2; ++i) {
					getch(i);
					chs[i] = make_shared<permanent_lazy_segtree>(*chs[i]);
					getch(i)->lazy = update_lazy(getch(i)->lazy, lazy);
				}
			}
			lazy = INI;
		}
		shared_ptr<permanent_lazy_segtree>getch(const int num_) {
			const int amid = (l + r) / 2;

			if (chs[num_] == nullptr) {
				if (num_ == 0) {
					chs[0] = make_shared<permanent_lazy_segtree>(INI, l, amid);
				}
				else {
					chs[1] = make_shared<permanent_lazy_segtree>(INI, amid, r);
				}
			}
			return chs[num_];

		}
		Value eval_value(const Value&value_l, const Value&value_r)const {
			return value_l + value_r;
		}
		// min [a, b)
		Value  query(int a, int b, int k) {
			lazy_evaluate_node();
			if (r <= a or b <= l) return INI;
			if (a <= l and r <= b) return val;
			const int m = (l + r) / 2;
			const Value al(getch(0)->query(a, b, k));
			const Value ar(getch(1)->query(a, b, k));
			return eval_value(al, ar);
		}
	};
	Value immutable_query(const shared_ptr<permanent_lazy_segtree>&tree, const int a, const int b) {
		shared_ptr<permanent_lazy_segtree>copy_tree(make_shared<permanent_lazy_segtree>(*tree));
		copy_tree->lazy_evaluate_node();
		if (copy_tree->r <= a or b <= copy_tree->l) return INI;
		if (a <= copy_tree->l and copy_tree->r <= b) return copy_tree->val;
		const int m = (copy_tree->l + copy_tree->r) / 2;
		const Value al(copy_tree->getch(0)->query(a, b, 22));
		const Value ar(copy_tree->getch(1)->query(a, b, 22));
		return copy_tree->eval_value(al, ar);
	}

	shared_ptr<permanent_lazy_segtree>immutable_update(const shared_ptr<permanent_lazy_segtree>&tree, const int l, const int r, const Value a) {
		
		if (tree->r <= l || r <= tree->l) {
			tree->lazy_evaluate_node();
			return tree;
		}
		else if (l <=tree->l&&tree->r <= r) {
			shared_ptr<permanent_lazy_segtree>copy_tree = (make_shared<permanent_lazy_segtree>(*tree));
			copy_tree->lazy = copy_tree->update_lazy(copy_tree->lazy, a);
			copy_tree->lazy_evaluate_node();
			return copy_tree;
		}
		else {
			assert(tree->l + 1 < tree->r); 
			shared_ptr<permanent_lazy_segtree>copy_tree = (make_shared<permanent_lazy_segtree>(*tree));
			copy_tree->lazy_evaluate_node();
			copy_tree->chs[0] = immutable_update(copy_tree->getch(0), l, r, a);
			copy_tree->chs[1] = immutable_update(copy_tree->getch(1), l, r, a);
			copy_tree->val = copy_tree->eval_value(copy_tree->getch(0)->val, copy_tree->getch(1)->val);

			return copy_tree;
		}
	}
}

struct query {
	int l;
	int r;
	int v;
};
using namespace perseg;
vector<shared_ptr<permanent_lazy_segtree>>trees;
bool check(permanent_lazy_segtree&tree, int l, int r, long long int k) {
	long long int sum = tree.query(l, r, k);
	if (sum < k)return true;
	else return false;
}
int main() {
	int N, M, Q; cin >> N >> M >> Q;
	vector<query>qs;
	for (int i = 0; i < M; ++i) {
		int a, b, v; cin >> a >> b >> v;
		a--;
		qs.push_back(query{ a,b,v });
	}
	sort(qs.begin(), qs.end(), [](const query&l, const query&r) {
		return l.v < r.v;
	});
	trees.push_back(make_shared<permanent_lazy_segtree>(1 << 30));
	for (int i = 0; i < qs.size(); ++i) {
		trees.push_back(immutable_update(trees.back(), qs[i].l, qs[i].r, 1));
	}
	while (Q--) {
		int x, y;long long int  j; cin >> x >> y >> j;
		x--;
		int amin = 0;
		int amax = trees.size();
		while (amin + 1 != amax) {
			int amid = (amin + amax) / 2;
			if (check(*trees[amid], x, y, j)) {
				amin = amid;
			}
			else {
				amax = amid;
			}
		}
		cout << qs[amin].v << endl;
	}

	return 0;
}