Submission #545431

# Submission time Handle Problem Language Result Execution time Memory
545431 2022-04-04T13:57:56 Z valerikk New Home (APIO18_new_home) C++17
47 / 100
5000 ms 149760 KB
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e8;
const int N = 3e5 + 7;

struct Store {
	int x, t, a, b;
};

struct Query {
	int l, y;
};

int n, k, q;
Store st[N];
Query qq[N];
vector<int> ys;
vector<int> beg[N], en[N];
int m;
int ans[N];
set<pair<int, int>> s[N];
int mn[2 * N];

void upd(int l, int r, int x) {
	l += m, r += m;
	while (l < r) {
		if (l & 1) {
			mn[l] = min(mn[l], x);
			++l;
		}
		if (r & 1) {
			--r;
			mn[r] = min(mn[r], x);
		}
		l >>= 1, r >>= 1;
	}
}

int get(int i) {
	i += m;
	int res = INF + 1;
	for (; i > 0; i >>= 1) {
		res = min(res, mn[i]);
	}
	return res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> k >> q;
	for (int i = 0; i < n; ++i) {
		auto &[x, t, a, b] = st[i];
		cin >> x >> t >> a >> b;
	}
	for (int i = 0; i < q; ++i) {
		auto &[l, y] = qq[i];
		cin >> l >> y;
		ys.push_back(y);
	}

	sort(ys.begin(), ys.end());
	ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
	m = ys.size();

	for (int i = 0; i < n; ++i) {
		st[i].a = lower_bound(ys.begin(), ys.end(), st[i].a) - ys.begin();
		st[i].b = lower_bound(ys.begin(), ys.end(), st[i].b + 1) - ys.begin();
		--st[i].t;
	}
	for (int i = 0; i < q; ++i) {
		qq[i].y = lower_bound(ys.begin(), ys.end(), qq[i].y) - ys.begin();
	}

	for (int i = 0; i < n; ++i) {
		beg[st[i].a].push_back(i);
		en[st[i].b].push_back(i);
	}

	for (int _ = 0; _ < 2; ++_) {
		for (int i = 0; i < k; ++i) {
			s[i].clear();
			s[i].insert({0, -1});
			s[i].insert({INF + 1, -1});
		}
		map<pair<int, int>, vector<pair<int, int>>> mp;
		
		auto kek = [&](int x1, int x2, int y, int type) {
			if (x1 == 0) return;
			if (x1 < x2) {
				if (x2 != INF + 1) {
					x2 = (x1 + x2) / 2 + 1;
				}
				mp[{x1, x2}].push_back({y, type});
			}
		};

		for (int i = 0; i <= m; ++i) {
			for (int id : beg[i]) {
				auto [it, fl] = s[st[id].t].insert({st[id].x, id});
				assert(fl);
				kek(prev(it)->first, st[id].x, i, 1);
				kek(st[id].x, next(it)->first, i, 1);
				kek(prev(it)->first, next(it)->first, i, -1);
			}
			for (int id : en[i]) {
				auto it = s[st[id].t].find({st[id].x, id});
				kek(prev(it)->first, st[id].x, i, -1);
				kek(st[id].x, next(it)->first, i, -1);
				kek(prev(it)->first, next(it)->first, i, 1);
				s[st[id].t].erase(it);
			}
			// cout << "Time: " << i << endl;
			// for (auto z : s[0]) {
			// 	cout << z.first << " ";
			// }
			// cout << endl;
		}

		vector<pair<int, int>> ord;
		for (const auto &[p, v] : mp) {
			ord.push_back(p);
		}
		sort(ord.begin(), ord.end(), [&](const auto &p1, const auto &p2) {
			return p1.second > p2.second;
		});

		vector<int> qord(q);
		iota(qord.begin(), qord.end(), 0);
		sort(qord.begin(), qord.end(), [&](const int &i, const int &j) {
			return qq[i].l > qq[j].l;
		});

		for (int i = 0; i < 2 * m; ++i) {
			mn[i] = INF;
		}

		int ptr = 0;
		for (int i = 0; i < q; ++i) {
			while (ptr < (int)ord.size() && ord[ptr].second > qq[qord[i]].l) {
				const auto &v = mp[ord[ptr]];
				int bal = 0;
				for (int j = 0; j < (int)v.size(); ++j) {
					bal += v[j].second;
					if (bal > 0) {
						upd(v[j].first, j == (int)v.size() - 1 ? m : v[j + 1].first, ord[ptr].first);
					}
				}
				++ptr;
			}
			ans[qord[i]] = max(ans[qord[i]], qq[qord[i]].l - get(qq[qord[i]].y));
		}


		for (int i = 0; i < n; ++i) {
			st[i].x = INF - st[i].x + 1;
		}
		for (int i = 0; i < q; ++i) {
			qq[i].l = INF - qq[i].l + 1;
		}
	}

	vector<int> cnt(k, 0);
	vector<bool> good(m + 1);
	int z = k;
	for (int i = 0; i <= m; ++i) {
		for (int id : beg[i]) {
			z -= cnt[st[id].t] == 0;
			++cnt[st[id].t];
			z += cnt[st[id].t] == 0;
		}
		for (int id : en[i]) {
			z -= cnt[st[id].t] == 0;
			--cnt[st[id].t];
			z += cnt[st[id].t] == 0;
		}
		good[i] = z == 0;
	}
	for (int i = 0; i < q; ++i) {
		if (!good[qq[i].y]) {
			cout << "-1\n";
		} else {
			cout << ans[i] << "\n";
		}
	}


	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 15 ms 28520 KB Output is correct
3 Correct 16 ms 28500 KB Output is correct
4 Correct 18 ms 28524 KB Output is correct
5 Correct 25 ms 28540 KB Output is correct
6 Correct 18 ms 28632 KB Output is correct
7 Correct 17 ms 28660 KB Output is correct
8 Correct 16 ms 28676 KB Output is correct
9 Correct 17 ms 28688 KB Output is correct
10 Correct 17 ms 28728 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 20 ms 28648 KB Output is correct
13 Correct 17 ms 28528 KB Output is correct
14 Correct 16 ms 28652 KB Output is correct
15 Correct 17 ms 28688 KB Output is correct
16 Correct 17 ms 28668 KB Output is correct
17 Correct 19 ms 28652 KB Output is correct
18 Correct 17 ms 28592 KB Output is correct
19 Correct 17 ms 28608 KB Output is correct
20 Correct 20 ms 28676 KB Output is correct
21 Correct 17 ms 28536 KB Output is correct
22 Correct 21 ms 28628 KB Output is correct
23 Correct 20 ms 28660 KB Output is correct
24 Correct 16 ms 28628 KB Output is correct
25 Correct 17 ms 28652 KB Output is correct
26 Correct 17 ms 28660 KB Output is correct
27 Correct 16 ms 28628 KB Output is correct
28 Correct 16 ms 28628 KB Output is correct
29 Correct 17 ms 28676 KB Output is correct
30 Correct 17 ms 28532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 15 ms 28520 KB Output is correct
3 Correct 16 ms 28500 KB Output is correct
4 Correct 18 ms 28524 KB Output is correct
5 Correct 25 ms 28540 KB Output is correct
6 Correct 18 ms 28632 KB Output is correct
7 Correct 17 ms 28660 KB Output is correct
8 Correct 16 ms 28676 KB Output is correct
9 Correct 17 ms 28688 KB Output is correct
10 Correct 17 ms 28728 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 20 ms 28648 KB Output is correct
13 Correct 17 ms 28528 KB Output is correct
14 Correct 16 ms 28652 KB Output is correct
15 Correct 17 ms 28688 KB Output is correct
16 Correct 17 ms 28668 KB Output is correct
17 Correct 19 ms 28652 KB Output is correct
18 Correct 17 ms 28592 KB Output is correct
19 Correct 17 ms 28608 KB Output is correct
20 Correct 20 ms 28676 KB Output is correct
21 Correct 17 ms 28536 KB Output is correct
22 Correct 21 ms 28628 KB Output is correct
23 Correct 20 ms 28660 KB Output is correct
24 Correct 16 ms 28628 KB Output is correct
25 Correct 17 ms 28652 KB Output is correct
26 Correct 17 ms 28660 KB Output is correct
27 Correct 16 ms 28628 KB Output is correct
28 Correct 16 ms 28628 KB Output is correct
29 Correct 17 ms 28676 KB Output is correct
30 Correct 17 ms 28532 KB Output is correct
31 Correct 1058 ms 57240 KB Output is correct
32 Correct 108 ms 33976 KB Output is correct
33 Correct 978 ms 58280 KB Output is correct
34 Correct 1006 ms 57552 KB Output is correct
35 Correct 1024 ms 59420 KB Output is correct
36 Correct 1030 ms 59572 KB Output is correct
37 Correct 624 ms 57760 KB Output is correct
38 Correct 549 ms 58000 KB Output is correct
39 Correct 495 ms 57276 KB Output is correct
40 Correct 453 ms 57560 KB Output is correct
41 Correct 609 ms 52096 KB Output is correct
42 Correct 650 ms 52168 KB Output is correct
43 Correct 73 ms 37020 KB Output is correct
44 Correct 590 ms 52016 KB Output is correct
45 Correct 655 ms 52076 KB Output is correct
46 Correct 580 ms 53332 KB Output is correct
47 Correct 257 ms 49620 KB Output is correct
48 Correct 266 ms 50448 KB Output is correct
49 Correct 306 ms 51084 KB Output is correct
50 Correct 352 ms 50956 KB Output is correct
51 Correct 333 ms 51448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5055 ms 145868 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5042 ms 149760 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 15 ms 28520 KB Output is correct
3 Correct 16 ms 28500 KB Output is correct
4 Correct 18 ms 28524 KB Output is correct
5 Correct 25 ms 28540 KB Output is correct
6 Correct 18 ms 28632 KB Output is correct
7 Correct 17 ms 28660 KB Output is correct
8 Correct 16 ms 28676 KB Output is correct
9 Correct 17 ms 28688 KB Output is correct
10 Correct 17 ms 28728 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 20 ms 28648 KB Output is correct
13 Correct 17 ms 28528 KB Output is correct
14 Correct 16 ms 28652 KB Output is correct
15 Correct 17 ms 28688 KB Output is correct
16 Correct 17 ms 28668 KB Output is correct
17 Correct 19 ms 28652 KB Output is correct
18 Correct 17 ms 28592 KB Output is correct
19 Correct 17 ms 28608 KB Output is correct
20 Correct 20 ms 28676 KB Output is correct
21 Correct 17 ms 28536 KB Output is correct
22 Correct 21 ms 28628 KB Output is correct
23 Correct 20 ms 28660 KB Output is correct
24 Correct 16 ms 28628 KB Output is correct
25 Correct 17 ms 28652 KB Output is correct
26 Correct 17 ms 28660 KB Output is correct
27 Correct 16 ms 28628 KB Output is correct
28 Correct 16 ms 28628 KB Output is correct
29 Correct 17 ms 28676 KB Output is correct
30 Correct 17 ms 28532 KB Output is correct
31 Correct 1058 ms 57240 KB Output is correct
32 Correct 108 ms 33976 KB Output is correct
33 Correct 978 ms 58280 KB Output is correct
34 Correct 1006 ms 57552 KB Output is correct
35 Correct 1024 ms 59420 KB Output is correct
36 Correct 1030 ms 59572 KB Output is correct
37 Correct 624 ms 57760 KB Output is correct
38 Correct 549 ms 58000 KB Output is correct
39 Correct 495 ms 57276 KB Output is correct
40 Correct 453 ms 57560 KB Output is correct
41 Correct 609 ms 52096 KB Output is correct
42 Correct 650 ms 52168 KB Output is correct
43 Correct 73 ms 37020 KB Output is correct
44 Correct 590 ms 52016 KB Output is correct
45 Correct 655 ms 52076 KB Output is correct
46 Correct 580 ms 53332 KB Output is correct
47 Correct 257 ms 49620 KB Output is correct
48 Correct 266 ms 50448 KB Output is correct
49 Correct 306 ms 51084 KB Output is correct
50 Correct 352 ms 50956 KB Output is correct
51 Correct 333 ms 51448 KB Output is correct
52 Correct 408 ms 51384 KB Output is correct
53 Correct 363 ms 49976 KB Output is correct
54 Correct 672 ms 52692 KB Output is correct
55 Correct 534 ms 51984 KB Output is correct
56 Correct 444 ms 51900 KB Output is correct
57 Correct 606 ms 51960 KB Output is correct
58 Correct 567 ms 52056 KB Output is correct
59 Correct 485 ms 52072 KB Output is correct
60 Correct 594 ms 52184 KB Output is correct
61 Correct 88 ms 43164 KB Output is correct
62 Correct 425 ms 51040 KB Output is correct
63 Correct 641 ms 52500 KB Output is correct
64 Correct 650 ms 52388 KB Output is correct
65 Correct 697 ms 52148 KB Output is correct
66 Correct 684 ms 52112 KB Output is correct
67 Correct 151 ms 37232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 15 ms 28520 KB Output is correct
3 Correct 16 ms 28500 KB Output is correct
4 Correct 18 ms 28524 KB Output is correct
5 Correct 25 ms 28540 KB Output is correct
6 Correct 18 ms 28632 KB Output is correct
7 Correct 17 ms 28660 KB Output is correct
8 Correct 16 ms 28676 KB Output is correct
9 Correct 17 ms 28688 KB Output is correct
10 Correct 17 ms 28728 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 20 ms 28648 KB Output is correct
13 Correct 17 ms 28528 KB Output is correct
14 Correct 16 ms 28652 KB Output is correct
15 Correct 17 ms 28688 KB Output is correct
16 Correct 17 ms 28668 KB Output is correct
17 Correct 19 ms 28652 KB Output is correct
18 Correct 17 ms 28592 KB Output is correct
19 Correct 17 ms 28608 KB Output is correct
20 Correct 20 ms 28676 KB Output is correct
21 Correct 17 ms 28536 KB Output is correct
22 Correct 21 ms 28628 KB Output is correct
23 Correct 20 ms 28660 KB Output is correct
24 Correct 16 ms 28628 KB Output is correct
25 Correct 17 ms 28652 KB Output is correct
26 Correct 17 ms 28660 KB Output is correct
27 Correct 16 ms 28628 KB Output is correct
28 Correct 16 ms 28628 KB Output is correct
29 Correct 17 ms 28676 KB Output is correct
30 Correct 17 ms 28532 KB Output is correct
31 Correct 1058 ms 57240 KB Output is correct
32 Correct 108 ms 33976 KB Output is correct
33 Correct 978 ms 58280 KB Output is correct
34 Correct 1006 ms 57552 KB Output is correct
35 Correct 1024 ms 59420 KB Output is correct
36 Correct 1030 ms 59572 KB Output is correct
37 Correct 624 ms 57760 KB Output is correct
38 Correct 549 ms 58000 KB Output is correct
39 Correct 495 ms 57276 KB Output is correct
40 Correct 453 ms 57560 KB Output is correct
41 Correct 609 ms 52096 KB Output is correct
42 Correct 650 ms 52168 KB Output is correct
43 Correct 73 ms 37020 KB Output is correct
44 Correct 590 ms 52016 KB Output is correct
45 Correct 655 ms 52076 KB Output is correct
46 Correct 580 ms 53332 KB Output is correct
47 Correct 257 ms 49620 KB Output is correct
48 Correct 266 ms 50448 KB Output is correct
49 Correct 306 ms 51084 KB Output is correct
50 Correct 352 ms 50956 KB Output is correct
51 Correct 333 ms 51448 KB Output is correct
52 Execution timed out 5055 ms 145868 KB Time limit exceeded
53 Halted 0 ms 0 KB -