Submission #545439

# Submission time Handle Problem Language Result Execution time Memory
545439 2022-04-04T14:12:14 Z valerikk New Home (APIO18_new_home) C++17
100 / 100
2300 ms 136004 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);
	}

	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 _ = 0; _ < 2; ++_) {
		for (int i = 0; i < k; ++i) {
			s[i].clear();
			s[i].insert({0, -1});
			s[i].insert({INF + 1, -1});
		}
		vector<pair<pair<int, int>, pair<int, int>>> v;
		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;
				}
				v.push_back({{x2, x1}, {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;
		}
 	
 		sort(v.begin(), v.end(), [&](const auto &p1, const auto &p2) {
 			return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second);
 		});
 
		for (int i = 0; i < 2 * m; ++i) {
			mn[i] = INF;
		}
 
		int ptr = 0;
		for (int i = 0; i < q; ++i) {
			while (ptr < (int)v.size() && v[ptr].first.first > qq[qord[i]].l) {
				int pp = ptr;
				vector<pair<int, int>> cur;
				while (ptr < (int)v.size() && v[ptr].first == v[pp].first) {
					cur.push_back(v[ptr].second);
					++ptr;
				}
				int bal = 0;
				for (int j = 0; j < (int)cur.size(); ++j) {
					bal += cur[j].second;
					if (bal > 0) {
						upd(cur[j].first, j == (int)cur.size() - 1 ? m : cur[j + 1].first, v[pp].first.second);
					}
				}
			}
			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;
		}
		reverse(qord.begin(), qord.end());
	}
 
	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 13 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 12 ms 28500 KB Output is correct
5 Correct 18 ms 28500 KB Output is correct
6 Correct 15 ms 28576 KB Output is correct
7 Correct 14 ms 28628 KB Output is correct
8 Correct 14 ms 28552 KB Output is correct
9 Correct 15 ms 28648 KB Output is correct
10 Correct 16 ms 28628 KB Output is correct
11 Correct 14 ms 28620 KB Output is correct
12 Correct 15 ms 28628 KB Output is correct
13 Correct 15 ms 28588 KB Output is correct
14 Correct 15 ms 28500 KB Output is correct
15 Correct 15 ms 28632 KB Output is correct
16 Correct 16 ms 28628 KB Output is correct
17 Correct 14 ms 28628 KB Output is correct
18 Correct 14 ms 28644 KB Output is correct
19 Correct 15 ms 28628 KB Output is correct
20 Correct 15 ms 28616 KB Output is correct
21 Correct 15 ms 28628 KB Output is correct
22 Correct 14 ms 28648 KB Output is correct
23 Correct 16 ms 28592 KB Output is correct
24 Correct 15 ms 28648 KB Output is correct
25 Correct 15 ms 28592 KB Output is correct
26 Correct 16 ms 28644 KB Output is correct
27 Correct 15 ms 28628 KB Output is correct
28 Correct 15 ms 28504 KB Output is correct
29 Correct 14 ms 28500 KB Output is correct
30 Correct 15 ms 28612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 12 ms 28500 KB Output is correct
5 Correct 18 ms 28500 KB Output is correct
6 Correct 15 ms 28576 KB Output is correct
7 Correct 14 ms 28628 KB Output is correct
8 Correct 14 ms 28552 KB Output is correct
9 Correct 15 ms 28648 KB Output is correct
10 Correct 16 ms 28628 KB Output is correct
11 Correct 14 ms 28620 KB Output is correct
12 Correct 15 ms 28628 KB Output is correct
13 Correct 15 ms 28588 KB Output is correct
14 Correct 15 ms 28500 KB Output is correct
15 Correct 15 ms 28632 KB Output is correct
16 Correct 16 ms 28628 KB Output is correct
17 Correct 14 ms 28628 KB Output is correct
18 Correct 14 ms 28644 KB Output is correct
19 Correct 15 ms 28628 KB Output is correct
20 Correct 15 ms 28616 KB Output is correct
21 Correct 15 ms 28628 KB Output is correct
22 Correct 14 ms 28648 KB Output is correct
23 Correct 16 ms 28592 KB Output is correct
24 Correct 15 ms 28648 KB Output is correct
25 Correct 15 ms 28592 KB Output is correct
26 Correct 16 ms 28644 KB Output is correct
27 Correct 15 ms 28628 KB Output is correct
28 Correct 15 ms 28504 KB Output is correct
29 Correct 14 ms 28500 KB Output is correct
30 Correct 15 ms 28612 KB Output is correct
31 Correct 301 ms 48764 KB Output is correct
32 Correct 108 ms 33976 KB Output is correct
33 Correct 292 ms 48128 KB Output is correct
34 Correct 292 ms 47636 KB Output is correct
35 Correct 297 ms 49384 KB Output is correct
36 Correct 303 ms 49216 KB Output is correct
37 Correct 259 ms 46992 KB Output is correct
38 Correct 260 ms 46900 KB Output is correct
39 Correct 228 ms 46748 KB Output is correct
40 Correct 240 ms 46800 KB Output is correct
41 Correct 204 ms 41356 KB Output is correct
42 Correct 207 ms 41424 KB Output is correct
43 Correct 79 ms 35980 KB Output is correct
44 Correct 204 ms 41380 KB Output is correct
45 Correct 194 ms 41308 KB Output is correct
46 Correct 186 ms 45528 KB Output is correct
47 Correct 144 ms 40508 KB Output is correct
48 Correct 147 ms 40900 KB Output is correct
49 Correct 172 ms 41072 KB Output is correct
50 Correct 188 ms 41044 KB Output is correct
51 Correct 164 ms 41212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1162 ms 109048 KB Output is correct
2 Correct 1490 ms 124264 KB Output is correct
3 Correct 942 ms 123404 KB Output is correct
4 Correct 1160 ms 123772 KB Output is correct
5 Correct 1402 ms 124112 KB Output is correct
6 Correct 1402 ms 124084 KB Output is correct
7 Correct 721 ms 123344 KB Output is correct
8 Correct 891 ms 123236 KB Output is correct
9 Correct 1068 ms 122544 KB Output is correct
10 Correct 1174 ms 124448 KB Output is correct
11 Correct 1045 ms 122612 KB Output is correct
12 Correct 1103 ms 123160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2118 ms 120364 KB Output is correct
2 Correct 816 ms 76472 KB Output is correct
3 Correct 2071 ms 131092 KB Output is correct
4 Correct 1275 ms 128760 KB Output is correct
5 Correct 1578 ms 132200 KB Output is correct
6 Correct 1529 ms 133312 KB Output is correct
7 Correct 1907 ms 132604 KB Output is correct
8 Correct 1915 ms 131332 KB Output is correct
9 Correct 1109 ms 128352 KB Output is correct
10 Correct 1421 ms 130472 KB Output is correct
11 Correct 1643 ms 130152 KB Output is correct
12 Correct 1706 ms 133008 KB Output is correct
13 Correct 1046 ms 123184 KB Output is correct
14 Correct 1099 ms 122964 KB Output is correct
15 Correct 1205 ms 124896 KB Output is correct
16 Correct 1299 ms 125420 KB Output is correct
17 Correct 1206 ms 124824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 12 ms 28500 KB Output is correct
5 Correct 18 ms 28500 KB Output is correct
6 Correct 15 ms 28576 KB Output is correct
7 Correct 14 ms 28628 KB Output is correct
8 Correct 14 ms 28552 KB Output is correct
9 Correct 15 ms 28648 KB Output is correct
10 Correct 16 ms 28628 KB Output is correct
11 Correct 14 ms 28620 KB Output is correct
12 Correct 15 ms 28628 KB Output is correct
13 Correct 15 ms 28588 KB Output is correct
14 Correct 15 ms 28500 KB Output is correct
15 Correct 15 ms 28632 KB Output is correct
16 Correct 16 ms 28628 KB Output is correct
17 Correct 14 ms 28628 KB Output is correct
18 Correct 14 ms 28644 KB Output is correct
19 Correct 15 ms 28628 KB Output is correct
20 Correct 15 ms 28616 KB Output is correct
21 Correct 15 ms 28628 KB Output is correct
22 Correct 14 ms 28648 KB Output is correct
23 Correct 16 ms 28592 KB Output is correct
24 Correct 15 ms 28648 KB Output is correct
25 Correct 15 ms 28592 KB Output is correct
26 Correct 16 ms 28644 KB Output is correct
27 Correct 15 ms 28628 KB Output is correct
28 Correct 15 ms 28504 KB Output is correct
29 Correct 14 ms 28500 KB Output is correct
30 Correct 15 ms 28612 KB Output is correct
31 Correct 301 ms 48764 KB Output is correct
32 Correct 108 ms 33976 KB Output is correct
33 Correct 292 ms 48128 KB Output is correct
34 Correct 292 ms 47636 KB Output is correct
35 Correct 297 ms 49384 KB Output is correct
36 Correct 303 ms 49216 KB Output is correct
37 Correct 259 ms 46992 KB Output is correct
38 Correct 260 ms 46900 KB Output is correct
39 Correct 228 ms 46748 KB Output is correct
40 Correct 240 ms 46800 KB Output is correct
41 Correct 204 ms 41356 KB Output is correct
42 Correct 207 ms 41424 KB Output is correct
43 Correct 79 ms 35980 KB Output is correct
44 Correct 204 ms 41380 KB Output is correct
45 Correct 194 ms 41308 KB Output is correct
46 Correct 186 ms 45528 KB Output is correct
47 Correct 144 ms 40508 KB Output is correct
48 Correct 147 ms 40900 KB Output is correct
49 Correct 172 ms 41072 KB Output is correct
50 Correct 188 ms 41044 KB Output is correct
51 Correct 164 ms 41212 KB Output is correct
52 Correct 249 ms 45184 KB Output is correct
53 Correct 222 ms 43900 KB Output is correct
54 Correct 270 ms 45408 KB Output is correct
55 Correct 204 ms 43748 KB Output is correct
56 Correct 202 ms 45044 KB Output is correct
57 Correct 206 ms 42476 KB Output is correct
58 Correct 217 ms 43752 KB Output is correct
59 Correct 230 ms 44576 KB Output is correct
60 Correct 214 ms 42424 KB Output is correct
61 Correct 108 ms 43116 KB Output is correct
62 Correct 246 ms 45348 KB Output is correct
63 Correct 263 ms 45376 KB Output is correct
64 Correct 255 ms 44176 KB Output is correct
65 Correct 225 ms 42668 KB Output is correct
66 Correct 218 ms 41960 KB Output is correct
67 Correct 160 ms 41088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 12 ms 28500 KB Output is correct
5 Correct 18 ms 28500 KB Output is correct
6 Correct 15 ms 28576 KB Output is correct
7 Correct 14 ms 28628 KB Output is correct
8 Correct 14 ms 28552 KB Output is correct
9 Correct 15 ms 28648 KB Output is correct
10 Correct 16 ms 28628 KB Output is correct
11 Correct 14 ms 28620 KB Output is correct
12 Correct 15 ms 28628 KB Output is correct
13 Correct 15 ms 28588 KB Output is correct
14 Correct 15 ms 28500 KB Output is correct
15 Correct 15 ms 28632 KB Output is correct
16 Correct 16 ms 28628 KB Output is correct
17 Correct 14 ms 28628 KB Output is correct
18 Correct 14 ms 28644 KB Output is correct
19 Correct 15 ms 28628 KB Output is correct
20 Correct 15 ms 28616 KB Output is correct
21 Correct 15 ms 28628 KB Output is correct
22 Correct 14 ms 28648 KB Output is correct
23 Correct 16 ms 28592 KB Output is correct
24 Correct 15 ms 28648 KB Output is correct
25 Correct 15 ms 28592 KB Output is correct
26 Correct 16 ms 28644 KB Output is correct
27 Correct 15 ms 28628 KB Output is correct
28 Correct 15 ms 28504 KB Output is correct
29 Correct 14 ms 28500 KB Output is correct
30 Correct 15 ms 28612 KB Output is correct
31 Correct 301 ms 48764 KB Output is correct
32 Correct 108 ms 33976 KB Output is correct
33 Correct 292 ms 48128 KB Output is correct
34 Correct 292 ms 47636 KB Output is correct
35 Correct 297 ms 49384 KB Output is correct
36 Correct 303 ms 49216 KB Output is correct
37 Correct 259 ms 46992 KB Output is correct
38 Correct 260 ms 46900 KB Output is correct
39 Correct 228 ms 46748 KB Output is correct
40 Correct 240 ms 46800 KB Output is correct
41 Correct 204 ms 41356 KB Output is correct
42 Correct 207 ms 41424 KB Output is correct
43 Correct 79 ms 35980 KB Output is correct
44 Correct 204 ms 41380 KB Output is correct
45 Correct 194 ms 41308 KB Output is correct
46 Correct 186 ms 45528 KB Output is correct
47 Correct 144 ms 40508 KB Output is correct
48 Correct 147 ms 40900 KB Output is correct
49 Correct 172 ms 41072 KB Output is correct
50 Correct 188 ms 41044 KB Output is correct
51 Correct 164 ms 41212 KB Output is correct
52 Correct 1162 ms 109048 KB Output is correct
53 Correct 1490 ms 124264 KB Output is correct
54 Correct 942 ms 123404 KB Output is correct
55 Correct 1160 ms 123772 KB Output is correct
56 Correct 1402 ms 124112 KB Output is correct
57 Correct 1402 ms 124084 KB Output is correct
58 Correct 721 ms 123344 KB Output is correct
59 Correct 891 ms 123236 KB Output is correct
60 Correct 1068 ms 122544 KB Output is correct
61 Correct 1174 ms 124448 KB Output is correct
62 Correct 1045 ms 122612 KB Output is correct
63 Correct 1103 ms 123160 KB Output is correct
64 Correct 2118 ms 120364 KB Output is correct
65 Correct 816 ms 76472 KB Output is correct
66 Correct 2071 ms 131092 KB Output is correct
67 Correct 1275 ms 128760 KB Output is correct
68 Correct 1578 ms 132200 KB Output is correct
69 Correct 1529 ms 133312 KB Output is correct
70 Correct 1907 ms 132604 KB Output is correct
71 Correct 1915 ms 131332 KB Output is correct
72 Correct 1109 ms 128352 KB Output is correct
73 Correct 1421 ms 130472 KB Output is correct
74 Correct 1643 ms 130152 KB Output is correct
75 Correct 1706 ms 133008 KB Output is correct
76 Correct 1046 ms 123184 KB Output is correct
77 Correct 1099 ms 122964 KB Output is correct
78 Correct 1205 ms 124896 KB Output is correct
79 Correct 1299 ms 125420 KB Output is correct
80 Correct 1206 ms 124824 KB Output is correct
81 Correct 249 ms 45184 KB Output is correct
82 Correct 222 ms 43900 KB Output is correct
83 Correct 270 ms 45408 KB Output is correct
84 Correct 204 ms 43748 KB Output is correct
85 Correct 202 ms 45044 KB Output is correct
86 Correct 206 ms 42476 KB Output is correct
87 Correct 217 ms 43752 KB Output is correct
88 Correct 230 ms 44576 KB Output is correct
89 Correct 214 ms 42424 KB Output is correct
90 Correct 108 ms 43116 KB Output is correct
91 Correct 246 ms 45348 KB Output is correct
92 Correct 263 ms 45376 KB Output is correct
93 Correct 255 ms 44176 KB Output is correct
94 Correct 225 ms 42668 KB Output is correct
95 Correct 218 ms 41960 KB Output is correct
96 Correct 160 ms 41088 KB Output is correct
97 Correct 1513 ms 130092 KB Output is correct
98 Correct 647 ms 59916 KB Output is correct
99 Correct 1979 ms 129532 KB Output is correct
100 Correct 1478 ms 124864 KB Output is correct
101 Correct 1781 ms 136004 KB Output is correct
102 Correct 2300 ms 135504 KB Output is correct
103 Correct 1497 ms 126836 KB Output is correct
104 Correct 1625 ms 126560 KB Output is correct
105 Correct 1522 ms 114012 KB Output is correct
106 Correct 1542 ms 114036 KB Output is correct
107 Correct 1391 ms 128964 KB Output is correct
108 Correct 1348 ms 119280 KB Output is correct
109 Correct 1362 ms 122584 KB Output is correct
110 Correct 1354 ms 129368 KB Output is correct
111 Correct 1357 ms 118804 KB Output is correct
112 Correct 1235 ms 122672 KB Output is correct
113 Correct 428 ms 117120 KB Output is correct
114 Correct 1459 ms 132012 KB Output is correct
115 Correct 1698 ms 119880 KB Output is correct
116 Correct 1710 ms 115272 KB Output is correct
117 Correct 1619 ms 123936 KB Output is correct
118 Correct 1265 ms 115896 KB Output is correct
119 Correct 891 ms 105932 KB Output is correct
120 Correct 626 ms 95192 KB Output is correct
121 Correct 781 ms 113968 KB Output is correct
122 Correct 743 ms 114116 KB Output is correct
123 Correct 833 ms 114488 KB Output is correct
124 Correct 925 ms 114776 KB Output is correct
125 Correct 841 ms 114268 KB Output is correct
126 Correct 958 ms 114812 KB Output is correct