# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
934889 |
2024-02-28T06:27:12 Z |
IBory |
New Home (APIO18_new_home) |
C++17 |
|
5000 ms |
564768 KB |
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int SZ = 1 << 20;
int P[SZ], S[SZ], X[SZ], ans[SZ];
multiset<int> CX[SZ];
struct Seg {
priority_queue<int> T[SZ << 1], E[SZ << 1];
int CL[SZ << 1], CR[SZ << 1];
void Update(int L, int R, int v, bool e, int sL = 1, int sR = SZ, int n = 1) {
if (R < sL || sR < L) return;
if (L <= sL && sR <= R) {
(e ? E[n] : T[n]).push(v);
return;
}
int mid = (sL + sR) >> 1;
Update(L, R, v, e, sL, mid, n * 2);
Update(L, R, v, e, mid + 1, sR, n * 2 + 1);
}
int Query(int p) {
int sL = 1, sR = SZ, n = 1, ret = -1e9;
while (1) {
while (!T[n].empty() && !E[n].empty() && T[n].top() == E[n].top()) T[n].pop(), E[n].pop();
if (!T[n].empty()) ret = max(ret, T[n].top());
if (sL == sR) return ret;
int mid = (sL + sR) >> 1;
if (p <= mid) sR = mid, n = n * 2;
else sL = mid + 1, n = 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
if (0 < id && id < SZ) {
int pos = P[abs(id)], type = S[abs(id)], x = Find(pos);
if (CX[type].empty()) curOpen++;
auto it = CX[type].insert(pos);
bool isPrev = it != CX[type].begin();
bool isNext = next(it) != CX[type].end();
// Add
T2.Update(isPrev ? Find(pos - (pos - *prev(it)) / 2) : L_LIM, x, pos, 0);
T1.Update(x, isNext ? Find(pos + (*next(it) - pos) / 2) : R_LIM, -pos, 0);
// Modify
if (isNext) {
int nx = Find(*next(it));
T2.Update(isPrev ? Find(*next(it) - (*next(it) - *prev(it)) / 2) : L_LIM, nx, *next(it), 1);
T2.Update(Find(*next(it) - (*next(it) - pos) / 2), nx, *next(it), 0);
}
if (isPrev) {
int px = Find(*prev(it));
T1.Update(px, isNext ? Find(*prev(it) + (*next(it) - *prev(it)) / 2) : R_LIM, -*prev(it), 1);
T1.Update(px, Find(*prev(it) + (pos - *prev(it)) / 2), -*prev(it), 0);
}
}
// Line Erase
else if (id < 0) {
int pos = P[abs(id)], type = S[abs(id)], x = Find(pos);
auto it = CX[type].find(pos);
bool isPrev = it != CX[type].begin();
bool isNext = next(it) != CX[type].end();
// Add
T2.Update(isPrev ? Find(pos - (pos - *prev(it)) / 2) : L_LIM, x, pos, 1);
T1.Update(x, isNext ? Find(pos + (*next(it) - pos) / 2) : R_LIM, -pos, 1);
// Modify
if (isNext) {
int nx = Find(*next(it));
T2.Update(isPrev ? Find(*next(it) - (*next(it) - *prev(it)) / 2) : L_LIM, nx, *next(it), 0);
T2.Update(Find(*next(it) - (*next(it) - pos) / 2), nx, *next(it), 1);
}
if (isPrev) {
int px = Find(*prev(it));
T1.Update(px, isNext ? Find(*prev(it) + (*next(it) - *prev(it)) / 2) : R_LIM, -*prev(it), 0);
T1.Update(px, Find(*prev(it) + (pos - *prev(it)) / 2), -*prev(it), 1);
}
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);
int b2 = T2.Query(p);
// cout << "Query: " << p << ' ' << b1 << ' ' << b2 << '\n';
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
320336 KB |
Output is correct |
2 |
Correct |
86 ms |
320288 KB |
Output is correct |
3 |
Correct |
86 ms |
320176 KB |
Output is correct |
4 |
Correct |
82 ms |
320288 KB |
Output is correct |
5 |
Correct |
81 ms |
320340 KB |
Output is correct |
6 |
Correct |
81 ms |
320596 KB |
Output is correct |
7 |
Correct |
79 ms |
320344 KB |
Output is correct |
8 |
Correct |
79 ms |
320596 KB |
Output is correct |
9 |
Correct |
81 ms |
320520 KB |
Output is correct |
10 |
Correct |
81 ms |
320668 KB |
Output is correct |
11 |
Correct |
77 ms |
320596 KB |
Output is correct |
12 |
Correct |
84 ms |
320484 KB |
Output is correct |
13 |
Correct |
87 ms |
320340 KB |
Output is correct |
14 |
Correct |
81 ms |
320592 KB |
Output is correct |
15 |
Correct |
82 ms |
320596 KB |
Output is correct |
16 |
Correct |
78 ms |
320492 KB |
Output is correct |
17 |
Correct |
79 ms |
320592 KB |
Output is correct |
18 |
Correct |
78 ms |
320504 KB |
Output is correct |
19 |
Correct |
80 ms |
320852 KB |
Output is correct |
20 |
Correct |
77 ms |
320676 KB |
Output is correct |
21 |
Correct |
78 ms |
320340 KB |
Output is correct |
22 |
Correct |
78 ms |
320336 KB |
Output is correct |
23 |
Correct |
77 ms |
320624 KB |
Output is correct |
24 |
Correct |
78 ms |
320600 KB |
Output is correct |
25 |
Correct |
77 ms |
320560 KB |
Output is correct |
26 |
Correct |
79 ms |
320656 KB |
Output is correct |
27 |
Correct |
79 ms |
320508 KB |
Output is correct |
28 |
Correct |
77 ms |
320592 KB |
Output is correct |
29 |
Correct |
77 ms |
320508 KB |
Output is correct |
30 |
Correct |
84 ms |
320336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
320336 KB |
Output is correct |
2 |
Correct |
86 ms |
320288 KB |
Output is correct |
3 |
Correct |
86 ms |
320176 KB |
Output is correct |
4 |
Correct |
82 ms |
320288 KB |
Output is correct |
5 |
Correct |
81 ms |
320340 KB |
Output is correct |
6 |
Correct |
81 ms |
320596 KB |
Output is correct |
7 |
Correct |
79 ms |
320344 KB |
Output is correct |
8 |
Correct |
79 ms |
320596 KB |
Output is correct |
9 |
Correct |
81 ms |
320520 KB |
Output is correct |
10 |
Correct |
81 ms |
320668 KB |
Output is correct |
11 |
Correct |
77 ms |
320596 KB |
Output is correct |
12 |
Correct |
84 ms |
320484 KB |
Output is correct |
13 |
Correct |
87 ms |
320340 KB |
Output is correct |
14 |
Correct |
81 ms |
320592 KB |
Output is correct |
15 |
Correct |
82 ms |
320596 KB |
Output is correct |
16 |
Correct |
78 ms |
320492 KB |
Output is correct |
17 |
Correct |
79 ms |
320592 KB |
Output is correct |
18 |
Correct |
78 ms |
320504 KB |
Output is correct |
19 |
Correct |
80 ms |
320852 KB |
Output is correct |
20 |
Correct |
77 ms |
320676 KB |
Output is correct |
21 |
Correct |
78 ms |
320340 KB |
Output is correct |
22 |
Correct |
78 ms |
320336 KB |
Output is correct |
23 |
Correct |
77 ms |
320624 KB |
Output is correct |
24 |
Correct |
78 ms |
320600 KB |
Output is correct |
25 |
Correct |
77 ms |
320560 KB |
Output is correct |
26 |
Correct |
79 ms |
320656 KB |
Output is correct |
27 |
Correct |
79 ms |
320508 KB |
Output is correct |
28 |
Correct |
77 ms |
320592 KB |
Output is correct |
29 |
Correct |
77 ms |
320508 KB |
Output is correct |
30 |
Correct |
84 ms |
320336 KB |
Output is correct |
31 |
Correct |
1926 ms |
388284 KB |
Output is correct |
32 |
Correct |
293 ms |
327620 KB |
Output is correct |
33 |
Correct |
1524 ms |
374112 KB |
Output is correct |
34 |
Correct |
1915 ms |
388944 KB |
Output is correct |
35 |
Correct |
1811 ms |
383688 KB |
Output is correct |
36 |
Correct |
1425 ms |
373088 KB |
Output is correct |
37 |
Correct |
1121 ms |
376104 KB |
Output is correct |
38 |
Correct |
853 ms |
370632 KB |
Output is correct |
39 |
Correct |
819 ms |
371052 KB |
Output is correct |
40 |
Correct |
812 ms |
370296 KB |
Output is correct |
41 |
Correct |
1136 ms |
362220 KB |
Output is correct |
42 |
Correct |
1135 ms |
368592 KB |
Output is correct |
43 |
Correct |
179 ms |
330724 KB |
Output is correct |
44 |
Correct |
1152 ms |
364204 KB |
Output is correct |
45 |
Correct |
1101 ms |
357956 KB |
Output is correct |
46 |
Correct |
1027 ms |
358104 KB |
Output is correct |
47 |
Correct |
546 ms |
359880 KB |
Output is correct |
48 |
Correct |
555 ms |
359624 KB |
Output is correct |
49 |
Correct |
680 ms |
363976 KB |
Output is correct |
50 |
Correct |
728 ms |
368580 KB |
Output is correct |
51 |
Correct |
719 ms |
362984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5045 ms |
564768 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5069 ms |
548076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
320336 KB |
Output is correct |
2 |
Correct |
86 ms |
320288 KB |
Output is correct |
3 |
Correct |
86 ms |
320176 KB |
Output is correct |
4 |
Correct |
82 ms |
320288 KB |
Output is correct |
5 |
Correct |
81 ms |
320340 KB |
Output is correct |
6 |
Correct |
81 ms |
320596 KB |
Output is correct |
7 |
Correct |
79 ms |
320344 KB |
Output is correct |
8 |
Correct |
79 ms |
320596 KB |
Output is correct |
9 |
Correct |
81 ms |
320520 KB |
Output is correct |
10 |
Correct |
81 ms |
320668 KB |
Output is correct |
11 |
Correct |
77 ms |
320596 KB |
Output is correct |
12 |
Correct |
84 ms |
320484 KB |
Output is correct |
13 |
Correct |
87 ms |
320340 KB |
Output is correct |
14 |
Correct |
81 ms |
320592 KB |
Output is correct |
15 |
Correct |
82 ms |
320596 KB |
Output is correct |
16 |
Correct |
78 ms |
320492 KB |
Output is correct |
17 |
Correct |
79 ms |
320592 KB |
Output is correct |
18 |
Correct |
78 ms |
320504 KB |
Output is correct |
19 |
Correct |
80 ms |
320852 KB |
Output is correct |
20 |
Correct |
77 ms |
320676 KB |
Output is correct |
21 |
Correct |
78 ms |
320340 KB |
Output is correct |
22 |
Correct |
78 ms |
320336 KB |
Output is correct |
23 |
Correct |
77 ms |
320624 KB |
Output is correct |
24 |
Correct |
78 ms |
320600 KB |
Output is correct |
25 |
Correct |
77 ms |
320560 KB |
Output is correct |
26 |
Correct |
79 ms |
320656 KB |
Output is correct |
27 |
Correct |
79 ms |
320508 KB |
Output is correct |
28 |
Correct |
77 ms |
320592 KB |
Output is correct |
29 |
Correct |
77 ms |
320508 KB |
Output is correct |
30 |
Correct |
84 ms |
320336 KB |
Output is correct |
31 |
Correct |
1926 ms |
388284 KB |
Output is correct |
32 |
Correct |
293 ms |
327620 KB |
Output is correct |
33 |
Correct |
1524 ms |
374112 KB |
Output is correct |
34 |
Correct |
1915 ms |
388944 KB |
Output is correct |
35 |
Correct |
1811 ms |
383688 KB |
Output is correct |
36 |
Correct |
1425 ms |
373088 KB |
Output is correct |
37 |
Correct |
1121 ms |
376104 KB |
Output is correct |
38 |
Correct |
853 ms |
370632 KB |
Output is correct |
39 |
Correct |
819 ms |
371052 KB |
Output is correct |
40 |
Correct |
812 ms |
370296 KB |
Output is correct |
41 |
Correct |
1136 ms |
362220 KB |
Output is correct |
42 |
Correct |
1135 ms |
368592 KB |
Output is correct |
43 |
Correct |
179 ms |
330724 KB |
Output is correct |
44 |
Correct |
1152 ms |
364204 KB |
Output is correct |
45 |
Correct |
1101 ms |
357956 KB |
Output is correct |
46 |
Correct |
1027 ms |
358104 KB |
Output is correct |
47 |
Correct |
546 ms |
359880 KB |
Output is correct |
48 |
Correct |
555 ms |
359624 KB |
Output is correct |
49 |
Correct |
680 ms |
363976 KB |
Output is correct |
50 |
Correct |
728 ms |
368580 KB |
Output is correct |
51 |
Correct |
719 ms |
362984 KB |
Output is correct |
52 |
Correct |
448 ms |
347332 KB |
Output is correct |
53 |
Correct |
463 ms |
346676 KB |
Output is correct |
54 |
Correct |
969 ms |
373496 KB |
Output is correct |
55 |
Correct |
909 ms |
361760 KB |
Output is correct |
56 |
Correct |
781 ms |
359388 KB |
Output is correct |
57 |
Correct |
1097 ms |
364904 KB |
Output is correct |
58 |
Correct |
891 ms |
363468 KB |
Output is correct |
59 |
Correct |
775 ms |
361924 KB |
Output is correct |
60 |
Correct |
1090 ms |
368596 KB |
Output is correct |
61 |
Correct |
159 ms |
329312 KB |
Output is correct |
62 |
Correct |
400 ms |
349024 KB |
Output is correct |
63 |
Correct |
750 ms |
362444 KB |
Output is correct |
64 |
Correct |
896 ms |
368264 KB |
Output is correct |
65 |
Correct |
1192 ms |
372312 KB |
Output is correct |
66 |
Correct |
1220 ms |
366360 KB |
Output is correct |
67 |
Correct |
283 ms |
332488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
320336 KB |
Output is correct |
2 |
Correct |
86 ms |
320288 KB |
Output is correct |
3 |
Correct |
86 ms |
320176 KB |
Output is correct |
4 |
Correct |
82 ms |
320288 KB |
Output is correct |
5 |
Correct |
81 ms |
320340 KB |
Output is correct |
6 |
Correct |
81 ms |
320596 KB |
Output is correct |
7 |
Correct |
79 ms |
320344 KB |
Output is correct |
8 |
Correct |
79 ms |
320596 KB |
Output is correct |
9 |
Correct |
81 ms |
320520 KB |
Output is correct |
10 |
Correct |
81 ms |
320668 KB |
Output is correct |
11 |
Correct |
77 ms |
320596 KB |
Output is correct |
12 |
Correct |
84 ms |
320484 KB |
Output is correct |
13 |
Correct |
87 ms |
320340 KB |
Output is correct |
14 |
Correct |
81 ms |
320592 KB |
Output is correct |
15 |
Correct |
82 ms |
320596 KB |
Output is correct |
16 |
Correct |
78 ms |
320492 KB |
Output is correct |
17 |
Correct |
79 ms |
320592 KB |
Output is correct |
18 |
Correct |
78 ms |
320504 KB |
Output is correct |
19 |
Correct |
80 ms |
320852 KB |
Output is correct |
20 |
Correct |
77 ms |
320676 KB |
Output is correct |
21 |
Correct |
78 ms |
320340 KB |
Output is correct |
22 |
Correct |
78 ms |
320336 KB |
Output is correct |
23 |
Correct |
77 ms |
320624 KB |
Output is correct |
24 |
Correct |
78 ms |
320600 KB |
Output is correct |
25 |
Correct |
77 ms |
320560 KB |
Output is correct |
26 |
Correct |
79 ms |
320656 KB |
Output is correct |
27 |
Correct |
79 ms |
320508 KB |
Output is correct |
28 |
Correct |
77 ms |
320592 KB |
Output is correct |
29 |
Correct |
77 ms |
320508 KB |
Output is correct |
30 |
Correct |
84 ms |
320336 KB |
Output is correct |
31 |
Correct |
1926 ms |
388284 KB |
Output is correct |
32 |
Correct |
293 ms |
327620 KB |
Output is correct |
33 |
Correct |
1524 ms |
374112 KB |
Output is correct |
34 |
Correct |
1915 ms |
388944 KB |
Output is correct |
35 |
Correct |
1811 ms |
383688 KB |
Output is correct |
36 |
Correct |
1425 ms |
373088 KB |
Output is correct |
37 |
Correct |
1121 ms |
376104 KB |
Output is correct |
38 |
Correct |
853 ms |
370632 KB |
Output is correct |
39 |
Correct |
819 ms |
371052 KB |
Output is correct |
40 |
Correct |
812 ms |
370296 KB |
Output is correct |
41 |
Correct |
1136 ms |
362220 KB |
Output is correct |
42 |
Correct |
1135 ms |
368592 KB |
Output is correct |
43 |
Correct |
179 ms |
330724 KB |
Output is correct |
44 |
Correct |
1152 ms |
364204 KB |
Output is correct |
45 |
Correct |
1101 ms |
357956 KB |
Output is correct |
46 |
Correct |
1027 ms |
358104 KB |
Output is correct |
47 |
Correct |
546 ms |
359880 KB |
Output is correct |
48 |
Correct |
555 ms |
359624 KB |
Output is correct |
49 |
Correct |
680 ms |
363976 KB |
Output is correct |
50 |
Correct |
728 ms |
368580 KB |
Output is correct |
51 |
Correct |
719 ms |
362984 KB |
Output is correct |
52 |
Execution timed out |
5045 ms |
564768 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |