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 persistent_lazy_segtree {
		Value val;
		Value lazy;
		int l, r;
		vector < persistent_lazy_segtree*>chs;
		persistent_lazy_segtree(const int size_) :val(INI), lazy(INI), l(0), r(size_), chs(2, nullptr) {

		}
		persistent_lazy_segtree(const int l_, const int r_) :val(INI), lazy(INI), l(l_), r(r_), chs(2, nullptr) {

		}
		persistent_lazy_segtree(const persistent_lazy_segtree*&tree_) :val(tree_->val), lazy(tree_->lazy), l(tree_->l), r(tree_->r), chs(tree_->chs) {
		}
		Value add_val(const Value&l, const Value&r) {
			return l + r;
		}
		Value add_lazy(const Value&l, const Value&r) {
			return l + r;
		}
		Value ltov(const Value&la) {
			return la*(r - l);
		}
		persistent_lazy_segtree*getch(const int num) {
			if (chs[num] == nullptr) {
				const int m = (l + r) / 2;
				if (!num) {
					chs[0] = new persistent_lazy_segtree(l, m);
				}
				else {
					chs[1] = new persistent_lazy_segtree(m, r);
				}
			}
			return chs[num];
		}
		void push() {
			if (lazy != INI) {
				val += ltov(lazy);
				if (l + 1 != r) {
					const int m = (l + r);
					chs[0] = new persistent_lazy_segtree(*getch(0));
					chs[0]->lazy = chs[0]->add_lazy(chs[0]->lazy, lazy);
					chs[1] = new persistent_lazy_segtree(*getch(1));
					chs[1]->lazy = chs[1]->add_lazy(chs[1]->lazy, lazy);
				}
				lazy = INI;
			}
		}
		Value query(int a, int b) {
			if (r <= a or b <= l)return 0;
			else if (a <= l&&r <= b) {
				return val + ltov(lazy);
			}
			else {
				push();
				const int m = (l + r) / 2;
				const Value lval = getch(0)->query(a, b);
				const Value rval = getch(1)->query(a, b);
				return add_val(lval, rval);
			}
		}
	};
	persistent_lazy_segtree* immutable_update(persistent_lazy_segtree* tree, int a, int b, int k) {
		if (tree->r <= a or b <= tree->l)return tree;
		else if (a <= tree->l && tree->r <= b) {
			auto copy_tree(new persistent_lazy_segtree(*tree));
			copy_tree->lazy += k;
			return copy_tree;
		}
		else {
			auto copy_tree(new persistent_lazy_segtree(*tree));
			copy_tree->push();
			const int m = (copy_tree->l + copy_tree->r) / 2;
			copy_tree->chs[0] = immutable_update(copy_tree->getch(0), a, b, k);
			copy_tree->chs[1] = immutable_update(copy_tree->getch(1), a, b, k);
			copy_tree->val = copy_tree->chs[0]->val + copy_tree->chs[0]->ltov(copy_tree->chs[0]->lazy) + copy_tree->chs[1]->val + copy_tree->chs[1]->ltov(copy_tree->chs[1]->lazy);
			return copy_tree;
		}
	}
}
struct query {
	int a, b, v;
};
using namespace perseg;

int main() {
	int N,M, Q; cin >> N>>M >> Q;
	persistent_lazy_segtree*seg(new persistent_lazy_segtree(1 << 30));
	vector<persistent_lazy_segtree*>trees(1,seg);
	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;
	});
	for (int i = 0; i < M; ++i) {
		trees.push_back(immutable_update(trees.back(),qs[i].a, qs[i].b, 1));
	}
	while (Q--) {
		int x, y;Value j; cin >> x >> y >> j;
		x--;
		int amin = 0;
		int amax =M;
		while (amin + 1 != amax) { 
			int amid = (amin + amax) / 2;
			Value aa = trees[amid]->query(x, y);
			if (aa < j) {
				amin = amid;
			}
			else {
				amax = amid;
			}
		}
		cout << qs[amin].v << endl;
	}
	/*vector<tuple<int, int>>qs;
	int now = 0;
	bool flag = false;
	while (Q--) {
		int a; cin >> a;
		if (a) {
			int i; cin >> i;
			i--;
			qs.emplace_back(now, i);
			flag = true;
		}
		else {
			int s, t, x; cin >> s >> t >> x;
			s--;
			if (flag) {

				trees.push_back(immutable_update(trees.back(), s, t, x));
				now++;
				flag = false;
			}
			else {
				trees.back() = immutable_update(trees.back(), s, t, x);
			}
		}
	}
	for (int i = 0; i < qs.size(); ++i) {
		int now, f;
		tie(now, f) = qs[i];
		Value ans = trees[now]->query(f, f + 1);
		cout << ans << endl;
	}*/

	return 0;
}