Submission #934924

# Submission time Handle Problem Language Result Execution time Memory
934924 2024-02-28T07:54:31 Z IBory New Home (APIO18_new_home) C++17
100 / 100
4024 ms 550496 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;

const int SZ = 1 << 21;
int P[SZ], S[SZ], X[SZ], ans[SZ];
multiset<int> CX[SZ];

struct Seg {
	priority_queue<int> P[SZ], E[SZ];
	int T[SZ << 1];
	Seg() {
		fill(T, T + SZ * 2, -1E9);
	}
	void Update(int i, int v, bool e, int sL = 1, int sR = SZ, int n = 1) {
		(e ? E[i] : P[i]).push(v);
		while (!E[i].empty() && !P[i].empty() && E[i].top() == P[i].top()) E[i].pop(), P[i].pop();
		T[i += SZ - 1] = (P[i].empty() ? -1e9 : P[i].top());
		while (i >>= 1) T[i] = max(T[i * 2], T[i * 2 + 1]);
	}
	int Query(int L, int R, int sL = 1, int sR = SZ, int n = 1) {
		if (R < sL || sR < L) return -1e9;
		if (L <= sL && sR <= R) return T[n];
		int mid = (sL + sR) >> 1;
		return max(Query(L, R, sL, mid, n * 2), Query(L, R, mid + 1, sR, n * 2 + 1));
	}
} T1, T2;

vector<int> UX;
int Find(int x) {
	return lower_bound(UX.begin(), UX.end(), x) - UX.begin();
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int N, K, Q;
	cin >> N >> K >> Q;
	vector<pii> ord;
	for (int i = 1; i <= N; ++i) {
		int a, b;
		cin >> P[i] >> S[i] >> a >> b;
		P[i] <<= 1;
		ord.emplace_back(a, i);
		ord.emplace_back(b + 1, -i);
	}
	for (int i = 1; i <= Q; ++i) {
		int t;
		cin >> X[i] >> t;
		X[i] <<= 1;
		ord.emplace_back(t, SZ + i);
	}
	sort(ord.begin(), ord.end());

	UX.push_back(-1); UX.push_back(1); UX.push_back(1e8);
	for (auto [_, id] : ord) {
		// Line Add
		if (0 < id && id < SZ) {
			int pos = P[abs(id)], type = S[abs(id)];
			UX.push_back(pos);
			auto it = CX[type].insert(pos);
			if (it != CX[type].begin()) {
				int h = abs(pos - *prev(it)) / 2;
				UX.push_back(*prev(it) + h);
			}
			if (next(it) != CX[type].end()) {
				int h = abs(pos - *next(it)) / 2;
				UX.push_back(pos + h);
			}
		}
		// Line Erase
		else if (id < 0) {
			int pos = P[abs(id)], type = S[abs(id)];
			auto it = CX[type].find(pos);
			if (it != CX[type].begin() && next(it) != CX[type].end()) {
				int h = abs(*next(it) - *prev(it)) / 2;
				UX.push_back(*prev(it) + h);
			}
			CX[type].erase(it);
		}
		else UX.push_back(X[id - SZ]);
	}
	sort(UX.begin(), UX.end());
	UX.erase(unique(UX.begin(), UX.end()), UX.end());

	memset(ans, 0xf3, sizeof(ans));
	int curOpen = 0, L_LIM = 1, R_LIM = UX.size() - 1;
	for (auto [_, id] : ord) {
		// Line Add/Erase
		if (id < SZ) {
			int pos = P[abs(id)], type = S[abs(id)], x = Find(pos);
			bool out = id < 0;

			if (CX[type].empty()) curOpen++;
			auto it = (out ? CX[type].find(pos) : CX[type].insert(pos));
			bool isPrev = it != CX[type].begin();
			bool isNext = next(it) != CX[type].end();
			int ph = (isPrev ? Find(pos - (pos - *prev(it)) / 2) : L_LIM);
			int nh = (isNext ? Find(pos + (*next(it) - pos) / 2) : R_LIM);
			int all = (isPrev && isNext ? Find(*next(it) - (*next(it) - *prev(it)) / 2) : 0);

			// Add
			T2.Update(ph, pos, out);
			T1.Update(nh, -pos, out);

			// Modify
			if (isNext) {
				T2.Update(isPrev ? all : L_LIM, *next(it), !out);
				T2.Update(nh, *next(it), out);
			}
			if (isPrev) {
				T1.Update(isNext ? all : R_LIM, -*prev(it), !out);
				T1.Update(ph, -*prev(it), out);
			}

			if (out) CX[type].erase(it);
			if (CX[type].empty()) curOpen--;
		}
		// Query
		else if (curOpen == K) {
			id -= SZ;
			int x = X[id], p = Find(x);
			int b1 = T1.Query(p, SZ);
			int b2 = T2.Query(1, p);
			ans[id] = max(x + b1, b2 - x);
		}
	}

	for (int i = 1; i <= Q; ++i) cout << (ans[i] < 0 ? -1 : ans[i] / 2) << '\n';
	return 0;
}

Compilation message

new_home.cpp: In member function 'void Seg::Update(int, int, bool, int, int, int)':
new_home.cpp:18:7: warning: operation on 'i' may be undefined [-Wsequence-point]
   18 |   T[i += SZ - 1] = (P[i].empty() ? -1e9 : P[i].top());
      |     ~~^~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:90:45: warning: unused variable 'x' [-Wunused-variable]
   90 |    int pos = P[abs(id)], type = S[abs(id)], x = Find(pos);
      |                                             ^
# Verdict Execution time Memory Grader output
1 Correct 116 ms 402516 KB Output is correct
2 Correct 119 ms 402512 KB Output is correct
3 Correct 119 ms 402436 KB Output is correct
4 Correct 115 ms 402488 KB Output is correct
5 Correct 122 ms 402448 KB Output is correct
6 Correct 118 ms 402780 KB Output is correct
7 Correct 117 ms 402628 KB Output is correct
8 Correct 116 ms 402512 KB Output is correct
9 Correct 118 ms 402656 KB Output is correct
10 Correct 118 ms 402768 KB Output is correct
11 Correct 117 ms 402772 KB Output is correct
12 Correct 117 ms 402772 KB Output is correct
13 Correct 117 ms 402512 KB Output is correct
14 Correct 118 ms 402736 KB Output is correct
15 Correct 117 ms 402732 KB Output is correct
16 Correct 117 ms 402512 KB Output is correct
17 Correct 121 ms 402516 KB Output is correct
18 Correct 120 ms 402736 KB Output is correct
19 Correct 119 ms 402516 KB Output is correct
20 Correct 116 ms 402512 KB Output is correct
21 Correct 123 ms 402692 KB Output is correct
22 Correct 119 ms 402636 KB Output is correct
23 Correct 117 ms 402500 KB Output is correct
24 Correct 118 ms 402692 KB Output is correct
25 Correct 122 ms 402508 KB Output is correct
26 Correct 117 ms 402628 KB Output is correct
27 Correct 118 ms 402508 KB Output is correct
28 Correct 119 ms 402720 KB Output is correct
29 Correct 117 ms 402508 KB Output is correct
30 Correct 121 ms 402792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 402516 KB Output is correct
2 Correct 119 ms 402512 KB Output is correct
3 Correct 119 ms 402436 KB Output is correct
4 Correct 115 ms 402488 KB Output is correct
5 Correct 122 ms 402448 KB Output is correct
6 Correct 118 ms 402780 KB Output is correct
7 Correct 117 ms 402628 KB Output is correct
8 Correct 116 ms 402512 KB Output is correct
9 Correct 118 ms 402656 KB Output is correct
10 Correct 118 ms 402768 KB Output is correct
11 Correct 117 ms 402772 KB Output is correct
12 Correct 117 ms 402772 KB Output is correct
13 Correct 117 ms 402512 KB Output is correct
14 Correct 118 ms 402736 KB Output is correct
15 Correct 117 ms 402732 KB Output is correct
16 Correct 117 ms 402512 KB Output is correct
17 Correct 121 ms 402516 KB Output is correct
18 Correct 120 ms 402736 KB Output is correct
19 Correct 119 ms 402516 KB Output is correct
20 Correct 116 ms 402512 KB Output is correct
21 Correct 123 ms 402692 KB Output is correct
22 Correct 119 ms 402636 KB Output is correct
23 Correct 117 ms 402500 KB Output is correct
24 Correct 118 ms 402692 KB Output is correct
25 Correct 122 ms 402508 KB Output is correct
26 Correct 117 ms 402628 KB Output is correct
27 Correct 118 ms 402508 KB Output is correct
28 Correct 119 ms 402720 KB Output is correct
29 Correct 117 ms 402508 KB Output is correct
30 Correct 121 ms 402792 KB Output is correct
31 Correct 640 ms 427568 KB Output is correct
32 Correct 282 ms 408616 KB Output is correct
33 Correct 593 ms 427720 KB Output is correct
34 Correct 609 ms 426368 KB Output is correct
35 Correct 682 ms 428380 KB Output is correct
36 Correct 624 ms 429000 KB Output is correct
37 Correct 431 ms 427204 KB Output is correct
38 Correct 427 ms 427440 KB Output is correct
39 Correct 381 ms 426528 KB Output is correct
40 Correct 389 ms 427108 KB Output is correct
41 Correct 396 ms 416708 KB Output is correct
42 Correct 350 ms 416456 KB Output is correct
43 Correct 228 ms 409172 KB Output is correct
44 Correct 362 ms 416712 KB Output is correct
45 Correct 383 ms 416800 KB Output is correct
46 Correct 360 ms 416036 KB Output is correct
47 Correct 250 ms 413016 KB Output is correct
48 Correct 261 ms 414152 KB Output is correct
49 Correct 286 ms 414920 KB Output is correct
50 Correct 284 ms 414620 KB Output is correct
51 Correct 292 ms 415432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2074 ms 472408 KB Output is correct
2 Correct 3113 ms 501568 KB Output is correct
3 Correct 885 ms 450480 KB Output is correct
4 Correct 1840 ms 475980 KB Output is correct
5 Correct 2803 ms 513692 KB Output is correct
6 Correct 3099 ms 514084 KB Output is correct
7 Correct 775 ms 450548 KB Output is correct
8 Correct 1449 ms 469604 KB Output is correct
9 Correct 1987 ms 493144 KB Output is correct
10 Correct 2362 ms 512984 KB Output is correct
11 Correct 1566 ms 509432 KB Output is correct
12 Correct 1710 ms 509608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3669 ms 520880 KB Output is correct
2 Correct 1006 ms 441848 KB Output is correct
3 Correct 3990 ms 545584 KB Output is correct
4 Correct 817 ms 453808 KB Output is correct
5 Correct 2225 ms 491228 KB Output is correct
6 Correct 1851 ms 477496 KB Output is correct
7 Correct 3656 ms 545104 KB Output is correct
8 Correct 3959 ms 545684 KB Output is correct
9 Correct 878 ms 455088 KB Output is correct
10 Correct 2007 ms 488828 KB Output is correct
11 Correct 3090 ms 528876 KB Output is correct
12 Correct 3367 ms 550496 KB Output is correct
13 Correct 1591 ms 534684 KB Output is correct
14 Correct 1535 ms 536744 KB Output is correct
15 Correct 1829 ms 544404 KB Output is correct
16 Correct 2040 ms 543008 KB Output is correct
17 Correct 1800 ms 545204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 402516 KB Output is correct
2 Correct 119 ms 402512 KB Output is correct
3 Correct 119 ms 402436 KB Output is correct
4 Correct 115 ms 402488 KB Output is correct
5 Correct 122 ms 402448 KB Output is correct
6 Correct 118 ms 402780 KB Output is correct
7 Correct 117 ms 402628 KB Output is correct
8 Correct 116 ms 402512 KB Output is correct
9 Correct 118 ms 402656 KB Output is correct
10 Correct 118 ms 402768 KB Output is correct
11 Correct 117 ms 402772 KB Output is correct
12 Correct 117 ms 402772 KB Output is correct
13 Correct 117 ms 402512 KB Output is correct
14 Correct 118 ms 402736 KB Output is correct
15 Correct 117 ms 402732 KB Output is correct
16 Correct 117 ms 402512 KB Output is correct
17 Correct 121 ms 402516 KB Output is correct
18 Correct 120 ms 402736 KB Output is correct
19 Correct 119 ms 402516 KB Output is correct
20 Correct 116 ms 402512 KB Output is correct
21 Correct 123 ms 402692 KB Output is correct
22 Correct 119 ms 402636 KB Output is correct
23 Correct 117 ms 402500 KB Output is correct
24 Correct 118 ms 402692 KB Output is correct
25 Correct 122 ms 402508 KB Output is correct
26 Correct 117 ms 402628 KB Output is correct
27 Correct 118 ms 402508 KB Output is correct
28 Correct 119 ms 402720 KB Output is correct
29 Correct 117 ms 402508 KB Output is correct
30 Correct 121 ms 402792 KB Output is correct
31 Correct 640 ms 427568 KB Output is correct
32 Correct 282 ms 408616 KB Output is correct
33 Correct 593 ms 427720 KB Output is correct
34 Correct 609 ms 426368 KB Output is correct
35 Correct 682 ms 428380 KB Output is correct
36 Correct 624 ms 429000 KB Output is correct
37 Correct 431 ms 427204 KB Output is correct
38 Correct 427 ms 427440 KB Output is correct
39 Correct 381 ms 426528 KB Output is correct
40 Correct 389 ms 427108 KB Output is correct
41 Correct 396 ms 416708 KB Output is correct
42 Correct 350 ms 416456 KB Output is correct
43 Correct 228 ms 409172 KB Output is correct
44 Correct 362 ms 416712 KB Output is correct
45 Correct 383 ms 416800 KB Output is correct
46 Correct 360 ms 416036 KB Output is correct
47 Correct 250 ms 413016 KB Output is correct
48 Correct 261 ms 414152 KB Output is correct
49 Correct 286 ms 414920 KB Output is correct
50 Correct 284 ms 414620 KB Output is correct
51 Correct 292 ms 415432 KB Output is correct
52 Correct 236 ms 412112 KB Output is correct
53 Correct 221 ms 410444 KB Output is correct
54 Correct 360 ms 417780 KB Output is correct
55 Correct 342 ms 417112 KB Output is correct
56 Correct 312 ms 415812 KB Output is correct
57 Correct 385 ms 418520 KB Output is correct
58 Correct 328 ms 415944 KB Output is correct
59 Correct 303 ms 414920 KB Output is correct
60 Correct 358 ms 418252 KB Output is correct
61 Correct 197 ms 411284 KB Output is correct
62 Correct 244 ms 412356 KB Output is correct
63 Correct 319 ms 415044 KB Output is correct
64 Correct 346 ms 416336 KB Output is correct
65 Correct 389 ms 418252 KB Output is correct
66 Correct 371 ms 419316 KB Output is correct
67 Correct 265 ms 410076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 402516 KB Output is correct
2 Correct 119 ms 402512 KB Output is correct
3 Correct 119 ms 402436 KB Output is correct
4 Correct 115 ms 402488 KB Output is correct
5 Correct 122 ms 402448 KB Output is correct
6 Correct 118 ms 402780 KB Output is correct
7 Correct 117 ms 402628 KB Output is correct
8 Correct 116 ms 402512 KB Output is correct
9 Correct 118 ms 402656 KB Output is correct
10 Correct 118 ms 402768 KB Output is correct
11 Correct 117 ms 402772 KB Output is correct
12 Correct 117 ms 402772 KB Output is correct
13 Correct 117 ms 402512 KB Output is correct
14 Correct 118 ms 402736 KB Output is correct
15 Correct 117 ms 402732 KB Output is correct
16 Correct 117 ms 402512 KB Output is correct
17 Correct 121 ms 402516 KB Output is correct
18 Correct 120 ms 402736 KB Output is correct
19 Correct 119 ms 402516 KB Output is correct
20 Correct 116 ms 402512 KB Output is correct
21 Correct 123 ms 402692 KB Output is correct
22 Correct 119 ms 402636 KB Output is correct
23 Correct 117 ms 402500 KB Output is correct
24 Correct 118 ms 402692 KB Output is correct
25 Correct 122 ms 402508 KB Output is correct
26 Correct 117 ms 402628 KB Output is correct
27 Correct 118 ms 402508 KB Output is correct
28 Correct 119 ms 402720 KB Output is correct
29 Correct 117 ms 402508 KB Output is correct
30 Correct 121 ms 402792 KB Output is correct
31 Correct 640 ms 427568 KB Output is correct
32 Correct 282 ms 408616 KB Output is correct
33 Correct 593 ms 427720 KB Output is correct
34 Correct 609 ms 426368 KB Output is correct
35 Correct 682 ms 428380 KB Output is correct
36 Correct 624 ms 429000 KB Output is correct
37 Correct 431 ms 427204 KB Output is correct
38 Correct 427 ms 427440 KB Output is correct
39 Correct 381 ms 426528 KB Output is correct
40 Correct 389 ms 427108 KB Output is correct
41 Correct 396 ms 416708 KB Output is correct
42 Correct 350 ms 416456 KB Output is correct
43 Correct 228 ms 409172 KB Output is correct
44 Correct 362 ms 416712 KB Output is correct
45 Correct 383 ms 416800 KB Output is correct
46 Correct 360 ms 416036 KB Output is correct
47 Correct 250 ms 413016 KB Output is correct
48 Correct 261 ms 414152 KB Output is correct
49 Correct 286 ms 414920 KB Output is correct
50 Correct 284 ms 414620 KB Output is correct
51 Correct 292 ms 415432 KB Output is correct
52 Correct 2074 ms 472408 KB Output is correct
53 Correct 3113 ms 501568 KB Output is correct
54 Correct 885 ms 450480 KB Output is correct
55 Correct 1840 ms 475980 KB Output is correct
56 Correct 2803 ms 513692 KB Output is correct
57 Correct 3099 ms 514084 KB Output is correct
58 Correct 775 ms 450548 KB Output is correct
59 Correct 1449 ms 469604 KB Output is correct
60 Correct 1987 ms 493144 KB Output is correct
61 Correct 2362 ms 512984 KB Output is correct
62 Correct 1566 ms 509432 KB Output is correct
63 Correct 1710 ms 509608 KB Output is correct
64 Correct 3669 ms 520880 KB Output is correct
65 Correct 1006 ms 441848 KB Output is correct
66 Correct 3990 ms 545584 KB Output is correct
67 Correct 817 ms 453808 KB Output is correct
68 Correct 2225 ms 491228 KB Output is correct
69 Correct 1851 ms 477496 KB Output is correct
70 Correct 3656 ms 545104 KB Output is correct
71 Correct 3959 ms 545684 KB Output is correct
72 Correct 878 ms 455088 KB Output is correct
73 Correct 2007 ms 488828 KB Output is correct
74 Correct 3090 ms 528876 KB Output is correct
75 Correct 3367 ms 550496 KB Output is correct
76 Correct 1591 ms 534684 KB Output is correct
77 Correct 1535 ms 536744 KB Output is correct
78 Correct 1829 ms 544404 KB Output is correct
79 Correct 2040 ms 543008 KB Output is correct
80 Correct 1800 ms 545204 KB Output is correct
81 Correct 236 ms 412112 KB Output is correct
82 Correct 221 ms 410444 KB Output is correct
83 Correct 360 ms 417780 KB Output is correct
84 Correct 342 ms 417112 KB Output is correct
85 Correct 312 ms 415812 KB Output is correct
86 Correct 385 ms 418520 KB Output is correct
87 Correct 328 ms 415944 KB Output is correct
88 Correct 303 ms 414920 KB Output is correct
89 Correct 358 ms 418252 KB Output is correct
90 Correct 197 ms 411284 KB Output is correct
91 Correct 244 ms 412356 KB Output is correct
92 Correct 319 ms 415044 KB Output is correct
93 Correct 346 ms 416336 KB Output is correct
94 Correct 389 ms 418252 KB Output is correct
95 Correct 371 ms 419316 KB Output is correct
96 Correct 265 ms 410076 KB Output is correct
97 Correct 834 ms 455812 KB Output is correct
98 Correct 1075 ms 435124 KB Output is correct
99 Correct 3764 ms 541656 KB Output is correct
100 Correct 778 ms 446388 KB Output is correct
101 Correct 1892 ms 479468 KB Output is correct
102 Correct 4024 ms 546496 KB Output is correct
103 Correct 2632 ms 540972 KB Output is correct
104 Correct 2590 ms 540932 KB Output is correct
105 Correct 1636 ms 539960 KB Output is correct
106 Correct 1716 ms 540176 KB Output is correct
107 Correct 1549 ms 476696 KB Output is correct
108 Correct 1389 ms 472948 KB Output is correct
109 Correct 1807 ms 485208 KB Output is correct
110 Correct 1520 ms 473056 KB Output is correct
111 Correct 1352 ms 470480 KB Output is correct
112 Correct 1725 ms 485728 KB Output is correct
113 Correct 535 ms 449116 KB Output is correct
114 Correct 922 ms 456204 KB Output is correct
115 Correct 1522 ms 470516 KB Output is correct
116 Correct 1719 ms 476540 KB Output is correct
117 Correct 2083 ms 483516 KB Output is correct
118 Correct 2032 ms 488232 KB Output is correct
119 Correct 863 ms 439220 KB Output is correct
120 Correct 714 ms 461572 KB Output is correct
121 Correct 965 ms 478208 KB Output is correct
122 Correct 955 ms 478672 KB Output is correct
123 Correct 1051 ms 481200 KB Output is correct
124 Correct 1179 ms 482184 KB Output is correct
125 Correct 1096 ms 482064 KB Output is correct
126 Correct 1127 ms 477468 KB Output is correct