#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ff	first
#define ss	second
#define pii	pair<int, int>
int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int n, K, q;
	cin >> n >> K >> q;
	map<int, vector<pii>> m;
	for (int i = 1; i <= n; i++) {
		int x, t, a, b;
		cin >> x >> t >> a >> b;
		m[a].push_back({x, t});
		m[b + 1].push_back({-x, t});
	}
	for (int i = 1; i <= q; i++) {
		int l, y;
		cin >> l >> y;
		m[y].push_back({l, -i});
	}
	vector<int> ans(q + 1);
	vector<multiset<int>> s(K + 1);
	for (auto i : m) {
		for (auto [x, t] : i.ss) {
			if (x < 0) s[t].erase(s[t].find(-x));
			else if (0 < t) s[t].insert(x);
		}
		for (auto [x, t] : i.ss) {
			if (0 < t) continue;
		
			t = -t;
			for (int j = 1; j <= K; j++) {
				if (s[j].empty()) {
					ans[t] = -1;
					break;
				}
				auto z = s[j].upper_bound(x);
				int mn = INT_MAX;
				if (z != s[j].end()) mn = *z - x;
				if (z != s[j].begin()) mn = *--z - x; 
			
				ans[t] = max(ans[t], mn);
			}
		}
	}
	for (int i = 1; i <= q; i++)
		cout << ans[i] << '\n';
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |