Submission #727641

# Submission time Handle Problem Language Result Execution time Memory
727641 2023-04-21T04:33:18 Z SanguineChameleon New Home (APIO18_new_home) C++17
100 / 100
3763 ms 113628 KB
#include <bits/stdc++.h>
using namespace std;

void just_do_it();

int main() {
	#ifdef KAMIRULEZ
		freopen("kamirulez.inp", "r", stdin);
		freopen("kamirulez.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	just_do_it();
	return 0;
}

struct store {
	int pos, type, left_T, right_T;
};

struct query {
	int pos, T;
};

const int inf = 1e8 + 20;
const int maxn = 3e5 + 20;
int tr[maxn * 4];
store S[maxn];
query Q[maxn];
set<int> bad[maxn];
set<int> val_pos[maxn];
int res[maxn];
int n, k, q;

void update(int id, int lt, int rt, int pos, int val) {
	if (lt == rt) {
		tr[id] = val;
		return;
	}
	int mt = (lt + rt) / 2;
	if (pos <= mt) {
		update(id * 2, lt, mt, pos, val);
	}
	else {
		update(id * 2 + 1, mt + 1, rt, pos, val);
	}
	tr[id] = max(tr[id * 2], tr[id * 2 + 1]);
}

int get(int id, int lt, int rt, int pos) {
	if (lt == rt) {
		return tr[id];
	}
	int mt = (lt + rt) / 2;
	if (pos <= mt) {
		return get(id * 2, lt, mt, pos);
	}
	else {
		return max(tr[id * 2], get(id * 2 + 1, mt + 1, rt, pos));
	}
}

void add_bad(int lt, int rt, int val) {
	if (lt > rt) {
		return;
	}
	if (rt == n) {
		rt += val - 1;
	}
	bad[lt].insert(rt);
	update(1, 1, n, lt, (bad[lt].empty() ? -inf : *bad[lt].rbegin()));
}

void rem_bad(int lt, int rt, int val) {
	if (lt > rt) {
		return;
	}
	if (rt == n) {
		rt += val - 1;
	}
	bad[lt].erase(rt);
	update(1, 1, n, lt, (bad[lt].empty() ? -inf : *bad[lt].rbegin()));
}

void add_val(int pos, int val) {
	auto it = val_pos[val].insert(pos).first;
	int prv = (it == val_pos[val].begin() ? 0 : *prev(it));
	int nxt = (next(it) == val_pos[val].end() ? n + 1 : *next(it));
	rem_bad(prv + 1, nxt - 1, val);
	add_bad(prv + 1, pos - 1, val);
	add_bad(pos + 1, nxt - 1, val);
}

void rem_val(int pos, int val) {
	auto it = val_pos[val].lower_bound(pos);
	int prv = (it == val_pos[val].begin() ? 0 : *prev(it));
	int nxt = (next(it) == val_pos[val].end() ? n + 1 : *next(it));
	rem_bad(prv + 1, pos - 1, val);
	rem_bad(pos + 1, nxt - 1, val);
	add_bad(prv + 1, nxt - 1, val);
	val_pos[val].erase(it);
}

bool check(int lt, int rt) {
	if (lt > rt) {
		return false;
	}
	return get(1, 1, n, lt) < rt;
}

void just_do_it() {
	fill_n(tr, maxn * 4, -inf);
	cin >> n >> k >> q;
	for (int i = 1; i <= k; i++) {
		add_bad(1, n, i);
	}
	vector<pair<int, int>> sorted_pos;
	vector<pair<int, pair<int, int>>> events;
	for (int i = 1; i <= n; i++) {
		cin >> S[i].pos >> S[i].type >> S[i].left_T >> S[i].right_T;
		sorted_pos.push_back({S[i].pos, i});
		events.push_back({S[i].left_T, {1, i}});
		events.push_back({S[i].right_T, {3, i}});
	}
	sort(sorted_pos.begin(), sorted_pos.end());
	for (int i = 1; i <= n; i++) {
		S[sorted_pos[i - 1].second].pos = i;
	}
	for (int i = 1; i <= q; i++) {
		cin >> Q[i].pos >> Q[i].T;
		events.push_back({Q[i].T, {2, i}});
	}
	sort(events.begin(), events.end());
	for (auto e: events) {
		int type = e.second.first;
		int id = e.second.second;
		if (type == 1) {
			add_val(S[id].pos, S[id].type);
		}
		if (type == 2) {
			res[id] = -1;
			int lt = 0;
			int rt = inf;
			while (lt <= rt) {
				int mt = (lt + rt) / 2;
				int ql = lower_bound(sorted_pos.begin(), sorted_pos.end(), make_pair(Q[id].pos - mt, -inf)) - sorted_pos.begin() + 1;
				int qr = upper_bound(sorted_pos.begin(), sorted_pos.end(), make_pair(Q[id].pos + mt, inf)) - sorted_pos.begin();
				if (check(ql, qr)) {
					res[id] = mt;
					rt = mt - 1;
				}
				else {
					lt = mt + 1;
				}
			}
		}
		if (type == 3) {
			rem_val(S[id].pos, S[id].type);
		}
	}
	for (int i = 1; i <= q; i++) {
		cout << res[i] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 33224 KB Output is correct
2 Correct 15 ms 33108 KB Output is correct
3 Correct 15 ms 33208 KB Output is correct
4 Correct 16 ms 33176 KB Output is correct
5 Correct 17 ms 33200 KB Output is correct
6 Correct 17 ms 33260 KB Output is correct
7 Correct 17 ms 33236 KB Output is correct
8 Correct 17 ms 33188 KB Output is correct
9 Correct 17 ms 33236 KB Output is correct
10 Correct 17 ms 33236 KB Output is correct
11 Correct 17 ms 33276 KB Output is correct
12 Correct 17 ms 33212 KB Output is correct
13 Correct 16 ms 33260 KB Output is correct
14 Correct 17 ms 33236 KB Output is correct
15 Correct 18 ms 33284 KB Output is correct
16 Correct 18 ms 33236 KB Output is correct
17 Correct 17 ms 33236 KB Output is correct
18 Correct 18 ms 33228 KB Output is correct
19 Correct 18 ms 33244 KB Output is correct
20 Correct 18 ms 33200 KB Output is correct
21 Correct 19 ms 33288 KB Output is correct
22 Correct 19 ms 33192 KB Output is correct
23 Correct 18 ms 33236 KB Output is correct
24 Correct 18 ms 33216 KB Output is correct
25 Correct 19 ms 33256 KB Output is correct
26 Correct 18 ms 33236 KB Output is correct
27 Correct 19 ms 33236 KB Output is correct
28 Correct 18 ms 33268 KB Output is correct
29 Correct 17 ms 33236 KB Output is correct
30 Correct 20 ms 33256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 33224 KB Output is correct
2 Correct 15 ms 33108 KB Output is correct
3 Correct 15 ms 33208 KB Output is correct
4 Correct 16 ms 33176 KB Output is correct
5 Correct 17 ms 33200 KB Output is correct
6 Correct 17 ms 33260 KB Output is correct
7 Correct 17 ms 33236 KB Output is correct
8 Correct 17 ms 33188 KB Output is correct
9 Correct 17 ms 33236 KB Output is correct
10 Correct 17 ms 33236 KB Output is correct
11 Correct 17 ms 33276 KB Output is correct
12 Correct 17 ms 33212 KB Output is correct
13 Correct 16 ms 33260 KB Output is correct
14 Correct 17 ms 33236 KB Output is correct
15 Correct 18 ms 33284 KB Output is correct
16 Correct 18 ms 33236 KB Output is correct
17 Correct 17 ms 33236 KB Output is correct
18 Correct 18 ms 33228 KB Output is correct
19 Correct 18 ms 33244 KB Output is correct
20 Correct 18 ms 33200 KB Output is correct
21 Correct 19 ms 33288 KB Output is correct
22 Correct 19 ms 33192 KB Output is correct
23 Correct 18 ms 33236 KB Output is correct
24 Correct 18 ms 33216 KB Output is correct
25 Correct 19 ms 33256 KB Output is correct
26 Correct 18 ms 33236 KB Output is correct
27 Correct 19 ms 33236 KB Output is correct
28 Correct 18 ms 33268 KB Output is correct
29 Correct 17 ms 33236 KB Output is correct
30 Correct 20 ms 33256 KB Output is correct
31 Correct 529 ms 43524 KB Output is correct
32 Correct 259 ms 40764 KB Output is correct
33 Correct 528 ms 42772 KB Output is correct
34 Correct 470 ms 42924 KB Output is correct
35 Correct 533 ms 46420 KB Output is correct
36 Correct 542 ms 46120 KB Output is correct
37 Correct 459 ms 41108 KB Output is correct
38 Correct 464 ms 40980 KB Output is correct
39 Correct 413 ms 40808 KB Output is correct
40 Correct 411 ms 40868 KB Output is correct
41 Correct 324 ms 41168 KB Output is correct
42 Correct 280 ms 40968 KB Output is correct
43 Correct 181 ms 45900 KB Output is correct
44 Correct 326 ms 41104 KB Output is correct
45 Correct 328 ms 41104 KB Output is correct
46 Correct 360 ms 41064 KB Output is correct
47 Correct 288 ms 40496 KB Output is correct
48 Correct 316 ms 40528 KB Output is correct
49 Correct 336 ms 40716 KB Output is correct
50 Correct 305 ms 40812 KB Output is correct
51 Correct 363 ms 40652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2128 ms 88084 KB Output is correct
2 Correct 3300 ms 82596 KB Output is correct
3 Correct 2567 ms 113160 KB Output is correct
4 Correct 2238 ms 103720 KB Output is correct
5 Correct 3103 ms 81960 KB Output is correct
6 Correct 3341 ms 93664 KB Output is correct
7 Correct 2056 ms 113172 KB Output is correct
8 Correct 1915 ms 103588 KB Output is correct
9 Correct 2085 ms 100088 KB Output is correct
10 Correct 2954 ms 97548 KB Output is correct
11 Correct 2234 ms 95108 KB Output is correct
12 Correct 2300 ms 97176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2949 ms 85108 KB Output is correct
2 Correct 1356 ms 85984 KB Output is correct
3 Correct 3599 ms 95016 KB Output is correct
4 Correct 2681 ms 111072 KB Output is correct
5 Correct 2206 ms 99556 KB Output is correct
6 Correct 2192 ms 101464 KB Output is correct
7 Correct 3602 ms 81788 KB Output is correct
8 Correct 3718 ms 93440 KB Output is correct
9 Correct 2409 ms 112252 KB Output is correct
10 Correct 2244 ms 101584 KB Output is correct
11 Correct 2543 ms 98976 KB Output is correct
12 Correct 3313 ms 97188 KB Output is correct
13 Correct 2065 ms 93064 KB Output is correct
14 Correct 2085 ms 90976 KB Output is correct
15 Correct 2291 ms 94712 KB Output is correct
16 Correct 2631 ms 96784 KB Output is correct
17 Correct 2499 ms 93748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 33224 KB Output is correct
2 Correct 15 ms 33108 KB Output is correct
3 Correct 15 ms 33208 KB Output is correct
4 Correct 16 ms 33176 KB Output is correct
5 Correct 17 ms 33200 KB Output is correct
6 Correct 17 ms 33260 KB Output is correct
7 Correct 17 ms 33236 KB Output is correct
8 Correct 17 ms 33188 KB Output is correct
9 Correct 17 ms 33236 KB Output is correct
10 Correct 17 ms 33236 KB Output is correct
11 Correct 17 ms 33276 KB Output is correct
12 Correct 17 ms 33212 KB Output is correct
13 Correct 16 ms 33260 KB Output is correct
14 Correct 17 ms 33236 KB Output is correct
15 Correct 18 ms 33284 KB Output is correct
16 Correct 18 ms 33236 KB Output is correct
17 Correct 17 ms 33236 KB Output is correct
18 Correct 18 ms 33228 KB Output is correct
19 Correct 18 ms 33244 KB Output is correct
20 Correct 18 ms 33200 KB Output is correct
21 Correct 19 ms 33288 KB Output is correct
22 Correct 19 ms 33192 KB Output is correct
23 Correct 18 ms 33236 KB Output is correct
24 Correct 18 ms 33216 KB Output is correct
25 Correct 19 ms 33256 KB Output is correct
26 Correct 18 ms 33236 KB Output is correct
27 Correct 19 ms 33236 KB Output is correct
28 Correct 18 ms 33268 KB Output is correct
29 Correct 17 ms 33236 KB Output is correct
30 Correct 20 ms 33256 KB Output is correct
31 Correct 529 ms 43524 KB Output is correct
32 Correct 259 ms 40764 KB Output is correct
33 Correct 528 ms 42772 KB Output is correct
34 Correct 470 ms 42924 KB Output is correct
35 Correct 533 ms 46420 KB Output is correct
36 Correct 542 ms 46120 KB Output is correct
37 Correct 459 ms 41108 KB Output is correct
38 Correct 464 ms 40980 KB Output is correct
39 Correct 413 ms 40808 KB Output is correct
40 Correct 411 ms 40868 KB Output is correct
41 Correct 324 ms 41168 KB Output is correct
42 Correct 280 ms 40968 KB Output is correct
43 Correct 181 ms 45900 KB Output is correct
44 Correct 326 ms 41104 KB Output is correct
45 Correct 328 ms 41104 KB Output is correct
46 Correct 360 ms 41064 KB Output is correct
47 Correct 288 ms 40496 KB Output is correct
48 Correct 316 ms 40528 KB Output is correct
49 Correct 336 ms 40716 KB Output is correct
50 Correct 305 ms 40812 KB Output is correct
51 Correct 363 ms 40652 KB Output is correct
52 Correct 397 ms 49104 KB Output is correct
53 Correct 383 ms 45520 KB Output is correct
54 Correct 344 ms 47320 KB Output is correct
55 Correct 366 ms 43940 KB Output is correct
56 Correct 373 ms 45324 KB Output is correct
57 Correct 344 ms 41864 KB Output is correct
58 Correct 328 ms 43756 KB Output is correct
59 Correct 343 ms 45184 KB Output is correct
60 Correct 297 ms 41676 KB Output is correct
61 Correct 216 ms 48848 KB Output is correct
62 Correct 378 ms 49224 KB Output is correct
63 Correct 346 ms 46932 KB Output is correct
64 Correct 356 ms 45448 KB Output is correct
65 Correct 326 ms 42448 KB Output is correct
66 Correct 325 ms 41140 KB Output is correct
67 Correct 284 ms 40468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 33224 KB Output is correct
2 Correct 15 ms 33108 KB Output is correct
3 Correct 15 ms 33208 KB Output is correct
4 Correct 16 ms 33176 KB Output is correct
5 Correct 17 ms 33200 KB Output is correct
6 Correct 17 ms 33260 KB Output is correct
7 Correct 17 ms 33236 KB Output is correct
8 Correct 17 ms 33188 KB Output is correct
9 Correct 17 ms 33236 KB Output is correct
10 Correct 17 ms 33236 KB Output is correct
11 Correct 17 ms 33276 KB Output is correct
12 Correct 17 ms 33212 KB Output is correct
13 Correct 16 ms 33260 KB Output is correct
14 Correct 17 ms 33236 KB Output is correct
15 Correct 18 ms 33284 KB Output is correct
16 Correct 18 ms 33236 KB Output is correct
17 Correct 17 ms 33236 KB Output is correct
18 Correct 18 ms 33228 KB Output is correct
19 Correct 18 ms 33244 KB Output is correct
20 Correct 18 ms 33200 KB Output is correct
21 Correct 19 ms 33288 KB Output is correct
22 Correct 19 ms 33192 KB Output is correct
23 Correct 18 ms 33236 KB Output is correct
24 Correct 18 ms 33216 KB Output is correct
25 Correct 19 ms 33256 KB Output is correct
26 Correct 18 ms 33236 KB Output is correct
27 Correct 19 ms 33236 KB Output is correct
28 Correct 18 ms 33268 KB Output is correct
29 Correct 17 ms 33236 KB Output is correct
30 Correct 20 ms 33256 KB Output is correct
31 Correct 529 ms 43524 KB Output is correct
32 Correct 259 ms 40764 KB Output is correct
33 Correct 528 ms 42772 KB Output is correct
34 Correct 470 ms 42924 KB Output is correct
35 Correct 533 ms 46420 KB Output is correct
36 Correct 542 ms 46120 KB Output is correct
37 Correct 459 ms 41108 KB Output is correct
38 Correct 464 ms 40980 KB Output is correct
39 Correct 413 ms 40808 KB Output is correct
40 Correct 411 ms 40868 KB Output is correct
41 Correct 324 ms 41168 KB Output is correct
42 Correct 280 ms 40968 KB Output is correct
43 Correct 181 ms 45900 KB Output is correct
44 Correct 326 ms 41104 KB Output is correct
45 Correct 328 ms 41104 KB Output is correct
46 Correct 360 ms 41064 KB Output is correct
47 Correct 288 ms 40496 KB Output is correct
48 Correct 316 ms 40528 KB Output is correct
49 Correct 336 ms 40716 KB Output is correct
50 Correct 305 ms 40812 KB Output is correct
51 Correct 363 ms 40652 KB Output is correct
52 Correct 2128 ms 88084 KB Output is correct
53 Correct 3300 ms 82596 KB Output is correct
54 Correct 2567 ms 113160 KB Output is correct
55 Correct 2238 ms 103720 KB Output is correct
56 Correct 3103 ms 81960 KB Output is correct
57 Correct 3341 ms 93664 KB Output is correct
58 Correct 2056 ms 113172 KB Output is correct
59 Correct 1915 ms 103588 KB Output is correct
60 Correct 2085 ms 100088 KB Output is correct
61 Correct 2954 ms 97548 KB Output is correct
62 Correct 2234 ms 95108 KB Output is correct
63 Correct 2300 ms 97176 KB Output is correct
64 Correct 2949 ms 85108 KB Output is correct
65 Correct 1356 ms 85984 KB Output is correct
66 Correct 3599 ms 95016 KB Output is correct
67 Correct 2681 ms 111072 KB Output is correct
68 Correct 2206 ms 99556 KB Output is correct
69 Correct 2192 ms 101464 KB Output is correct
70 Correct 3602 ms 81788 KB Output is correct
71 Correct 3718 ms 93440 KB Output is correct
72 Correct 2409 ms 112252 KB Output is correct
73 Correct 2244 ms 101584 KB Output is correct
74 Correct 2543 ms 98976 KB Output is correct
75 Correct 3313 ms 97188 KB Output is correct
76 Correct 2065 ms 93064 KB Output is correct
77 Correct 2085 ms 90976 KB Output is correct
78 Correct 2291 ms 94712 KB Output is correct
79 Correct 2631 ms 96784 KB Output is correct
80 Correct 2499 ms 93748 KB Output is correct
81 Correct 397 ms 49104 KB Output is correct
82 Correct 383 ms 45520 KB Output is correct
83 Correct 344 ms 47320 KB Output is correct
84 Correct 366 ms 43940 KB Output is correct
85 Correct 373 ms 45324 KB Output is correct
86 Correct 344 ms 41864 KB Output is correct
87 Correct 328 ms 43756 KB Output is correct
88 Correct 343 ms 45184 KB Output is correct
89 Correct 297 ms 41676 KB Output is correct
90 Correct 216 ms 48848 KB Output is correct
91 Correct 378 ms 49224 KB Output is correct
92 Correct 346 ms 46932 KB Output is correct
93 Correct 356 ms 45448 KB Output is correct
94 Correct 326 ms 42448 KB Output is correct
95 Correct 325 ms 41140 KB Output is correct
96 Correct 284 ms 40468 KB Output is correct
97 Correct 2937 ms 113096 KB Output is correct
98 Correct 1478 ms 70976 KB Output is correct
99 Correct 3760 ms 80560 KB Output is correct
100 Correct 2792 ms 95520 KB Output is correct
101 Correct 2371 ms 103580 KB Output is correct
102 Correct 3763 ms 97296 KB Output is correct
103 Correct 3028 ms 72652 KB Output is correct
104 Correct 2989 ms 72108 KB Output is correct
105 Correct 2428 ms 70340 KB Output is correct
106 Correct 2508 ms 70624 KB Output is correct
107 Correct 2248 ms 86596 KB Output is correct
108 Correct 2364 ms 93632 KB Output is correct
109 Correct 2092 ms 76712 KB Output is correct
110 Correct 2243 ms 86052 KB Output is correct
111 Correct 2463 ms 92940 KB Output is correct
112 Correct 2120 ms 76140 KB Output is correct
113 Correct 1294 ms 111756 KB Output is correct
114 Correct 2914 ms 113628 KB Output is correct
115 Correct 2596 ms 101832 KB Output is correct
116 Correct 2485 ms 95196 KB Output is correct
117 Correct 2303 ms 79792 KB Output is correct
118 Correct 1998 ms 72624 KB Output is correct
119 Correct 1690 ms 69388 KB Output is correct
120 Correct 1565 ms 67992 KB Output is correct
121 Correct 1876 ms 69416 KB Output is correct
122 Correct 1993 ms 69352 KB Output is correct
123 Correct 1939 ms 70204 KB Output is correct
124 Correct 1876 ms 70856 KB Output is correct
125 Correct 2207 ms 70640 KB Output is correct
126 Correct 1854 ms 70812 KB Output is correct