#include <bits/stdc++.h>
using namespace std;
using ari2 = array<int, 2>;
using ari3 = array<int, 3>;
using ari4 = array<int, 4>;
#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)
#define REP(a, b, c, d) for (int a = (b); (d) > 0 ? a <= (c) : a >= (c); a += (d))
#define FOR(a, b, c) REP(a, b, c, 1)
#define ROF(a, b, c) REP(a, b, c, -1)
constexpr int mxN = 300000;
int N, K, Q;
vt<int> cmp;
struct ST {
priority_queue<int> st[mxN<<1], die[mxN<<1];
void kill(int i) {
while (size(st[i]) && size(die[i]) && st[i].top() == die[i].top())
st[i].pop(), die[i].pop();
}
void insert(int ql, int qr, int v) {
for (ql += size(cmp), qr += size(cmp) + 1; ql < qr; ql >>= 1, qr >>= 1) {
if (ql & 1)
st[ql++].push(v);
if (qr & 1)
st[--qr].push(v);
}
}
void erase(int ql, int qr, int v) {
for (ql += size(cmp), qr += size(cmp) + 1; ql < qr; ql >>= 1, qr >>= 1) {
if (ql & 1)
die[ql++].push(v);
if (qr & 1)
die[--qr].push(v);
}
}
int qry(int i) {
int res = INT_MIN;
for (i += size(cmp); i > 0; i >>= 1)
kill(i), res = max(res, size(st[i]) ? st[i].top() : res);
return res;
}
} st_left, st_right;
signed main() {
#ifndef DEBUG
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cin >> N >> K >> Q;
vt<ari4> events;
FOR(i, 0, N-1) {
int x, t, a, b;
cin >> x >> t >> a >> b, t--;
events.push_back({a, 0, x, t}); // time, (0 = insert, 1 = remove, 2 = is year), location, type
events.push_back({b+1, 1, x, t});
}
FOR(i, 0, Q-1) {
int year, x;
cin >> x >> year;
events.push_back({year, 2, x, i}); // time, is year, location, query index
cmp.push_back(x);
}
sort(all(cmp));
cmp.erase(unique(all(cmp)), end(cmp));
sort(all(events));
auto ind = [&](int v) {
return lower_bound(all(cmp), v) - begin(cmp);
};
auto erase = [&](int l, int r) {
assert(l + r + 1 >= 0);
int m = l + r + 1 >> 1;
int li = ind(l), ri = ind(r), mi = ind(m);
ri -= cmp[ri] > r;
if (mi - 1 >= li)
st_left.erase(li, mi-1, -l);
if (ri >= mi)
st_right.erase(mi, ri, r);
};
auto insert = [&](int l, int r) {
assert(l + r + 1 >= 0);
int m = l + r + 1 >> 1;
int li = ind(l), ri = ind(r), mi = ind(m);
ri -= cmp[ri] > r;
if (mi - 1 >= li)
st_left.insert(li, mi-1, -l);
if (ri >= mi)
st_right.insert(mi, ri, r);
};
vt<int> ans(Q), present(K);
vt<map<int, int>> positions(K);
int present_cnt = 0;
for (auto &[t, is_query, x, type] : events) {
if (is_query == 2) {
if (present_cnt < K) {
ans[type] = -1;
continue;
}
int i = ind(x);
int l = st_left.qry(i);
if (l != INT_MIN)
l = -l;
else
l = x;
int r = st_right.qry(i);
l = min(l, x);
r = max(r, x);
ans[type] = max(r - x, x - l);
#ifdef DEBUG
cout << "at time " << t << " answer for " << x << " is:\n";
cout << "L: " << l << '\n';
cout << "R: " << r << '\n';
#endif
} else if (is_query) {
auto &xs = positions[type];
if (xs[x] == 1) {
auto it = xs.find(x);
int l = it != begin(xs) ? prev(it)->first : -1;
int r = next(it) != end(xs) ? next(it)->first : INT_MAX;
xs.erase(it);
if (l >= 0)
erase(l, x);
else
erase(-x, x);
if (r < INT_MAX)
erase(x, r);
else
erase(x, INT_MAX - x - 1);
if (l >= 0 && r < INT_MAX)
insert(l, r);
else if (l >= 0)
insert(l, INT_MAX - l - 1);
else if (r < INT_MAX)
insert(-r, r);
} else {
xs[x]--;
}
present_cnt -= --present[type] == 0;
} else {
auto &xs = positions[type];
if (xs[x])
xs[x]++;
else {
xs[x] = 1;
auto it = xs.find(x);
int l = it != begin(xs) ? prev(it)->first : -1;
int r = next(it) != end(xs) ? next(it)->first : INT_MAX;
if (l >= 0 && r < INT_MAX)
erase(l, r);
else if (l >= 0)
erase(l, INT_MAX - l - 1);
else if (r < INT_MAX)
erase(-r, r);
if (l >= 0)
insert(l, x);
else
insert(-x, x);
if (r < INT_MAX)
insert(x, r);
else
insert(x, INT_MAX - x - 1);
}
present_cnt += ++present[type] == 1;
}
}
for (int &i : ans)
cout << i << '\n';
}
Compilation message
new_home.cpp: In lambda function:
new_home.cpp:77:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | int m = l + r + 1 >> 1;
| ~~~~~~^~~
new_home.cpp: In lambda function:
new_home.cpp:87:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int m = l + r + 1 >> 1;
| ~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
75608 KB |
Output is correct |
2 |
Correct |
17 ms |
75496 KB |
Output is correct |
3 |
Correct |
19 ms |
75612 KB |
Output is correct |
4 |
Correct |
16 ms |
75492 KB |
Output is correct |
5 |
Correct |
18 ms |
75612 KB |
Output is correct |
6 |
Correct |
18 ms |
75612 KB |
Output is correct |
7 |
Correct |
19 ms |
75608 KB |
Output is correct |
8 |
Correct |
18 ms |
75608 KB |
Output is correct |
9 |
Correct |
18 ms |
75612 KB |
Output is correct |
10 |
Correct |
18 ms |
75612 KB |
Output is correct |
11 |
Correct |
17 ms |
75616 KB |
Output is correct |
12 |
Correct |
19 ms |
75612 KB |
Output is correct |
13 |
Correct |
18 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75616 KB |
Output is correct |
15 |
Correct |
17 ms |
75608 KB |
Output is correct |
16 |
Correct |
20 ms |
75612 KB |
Output is correct |
17 |
Correct |
18 ms |
75612 KB |
Output is correct |
18 |
Correct |
19 ms |
75648 KB |
Output is correct |
19 |
Correct |
18 ms |
75612 KB |
Output is correct |
20 |
Correct |
19 ms |
75608 KB |
Output is correct |
21 |
Correct |
16 ms |
75612 KB |
Output is correct |
22 |
Correct |
17 ms |
75612 KB |
Output is correct |
23 |
Correct |
18 ms |
75608 KB |
Output is correct |
24 |
Correct |
18 ms |
75612 KB |
Output is correct |
25 |
Correct |
18 ms |
75608 KB |
Output is correct |
26 |
Correct |
18 ms |
75612 KB |
Output is correct |
27 |
Correct |
17 ms |
75500 KB |
Output is correct |
28 |
Correct |
20 ms |
75612 KB |
Output is correct |
29 |
Correct |
17 ms |
75752 KB |
Output is correct |
30 |
Correct |
18 ms |
75608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
75608 KB |
Output is correct |
2 |
Correct |
17 ms |
75496 KB |
Output is correct |
3 |
Correct |
19 ms |
75612 KB |
Output is correct |
4 |
Correct |
16 ms |
75492 KB |
Output is correct |
5 |
Correct |
18 ms |
75612 KB |
Output is correct |
6 |
Correct |
18 ms |
75612 KB |
Output is correct |
7 |
Correct |
19 ms |
75608 KB |
Output is correct |
8 |
Correct |
18 ms |
75608 KB |
Output is correct |
9 |
Correct |
18 ms |
75612 KB |
Output is correct |
10 |
Correct |
18 ms |
75612 KB |
Output is correct |
11 |
Correct |
17 ms |
75616 KB |
Output is correct |
12 |
Correct |
19 ms |
75612 KB |
Output is correct |
13 |
Correct |
18 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75616 KB |
Output is correct |
15 |
Correct |
17 ms |
75608 KB |
Output is correct |
16 |
Correct |
20 ms |
75612 KB |
Output is correct |
17 |
Correct |
18 ms |
75612 KB |
Output is correct |
18 |
Correct |
19 ms |
75648 KB |
Output is correct |
19 |
Correct |
18 ms |
75612 KB |
Output is correct |
20 |
Correct |
19 ms |
75608 KB |
Output is correct |
21 |
Correct |
16 ms |
75612 KB |
Output is correct |
22 |
Correct |
17 ms |
75612 KB |
Output is correct |
23 |
Correct |
18 ms |
75608 KB |
Output is correct |
24 |
Correct |
18 ms |
75612 KB |
Output is correct |
25 |
Correct |
18 ms |
75608 KB |
Output is correct |
26 |
Correct |
18 ms |
75612 KB |
Output is correct |
27 |
Correct |
17 ms |
75500 KB |
Output is correct |
28 |
Correct |
20 ms |
75612 KB |
Output is correct |
29 |
Correct |
17 ms |
75752 KB |
Output is correct |
30 |
Correct |
18 ms |
75608 KB |
Output is correct |
31 |
Correct |
1002 ms |
116672 KB |
Output is correct |
32 |
Correct |
68 ms |
81356 KB |
Output is correct |
33 |
Correct |
642 ms |
101000 KB |
Output is correct |
34 |
Correct |
941 ms |
117728 KB |
Output is correct |
35 |
Correct |
899 ms |
112088 KB |
Output is correct |
36 |
Correct |
660 ms |
99812 KB |
Output is correct |
37 |
Correct |
455 ms |
102652 KB |
Output is correct |
38 |
Correct |
357 ms |
95696 KB |
Output is correct |
39 |
Correct |
323 ms |
97216 KB |
Output is correct |
40 |
Correct |
303 ms |
95420 KB |
Output is correct |
41 |
Correct |
717 ms |
104632 KB |
Output is correct |
42 |
Correct |
671 ms |
110008 KB |
Output is correct |
43 |
Correct |
69 ms |
81824 KB |
Output is correct |
44 |
Correct |
704 ms |
103700 KB |
Output is correct |
45 |
Correct |
659 ms |
100796 KB |
Output is correct |
46 |
Correct |
641 ms |
98256 KB |
Output is correct |
47 |
Correct |
319 ms |
103008 KB |
Output is correct |
48 |
Correct |
297 ms |
100540 KB |
Output is correct |
49 |
Correct |
395 ms |
104740 KB |
Output is correct |
50 |
Correct |
413 ms |
109324 KB |
Output is correct |
51 |
Correct |
421 ms |
103304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3229 ms |
299380 KB |
Output is correct |
2 |
Correct |
2188 ms |
255968 KB |
Output is correct |
3 |
Correct |
1339 ms |
243460 KB |
Output is correct |
4 |
Correct |
2950 ms |
300960 KB |
Output is correct |
5 |
Correct |
1941 ms |
254592 KB |
Output is correct |
6 |
Correct |
2127 ms |
254416 KB |
Output is correct |
7 |
Correct |
1416 ms |
242608 KB |
Output is correct |
8 |
Correct |
2970 ms |
313776 KB |
Output is correct |
9 |
Correct |
3018 ms |
292008 KB |
Output is correct |
10 |
Correct |
2613 ms |
294880 KB |
Output is correct |
11 |
Correct |
2045 ms |
261036 KB |
Output is correct |
12 |
Correct |
2156 ms |
265904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5036 ms |
310752 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
75608 KB |
Output is correct |
2 |
Correct |
17 ms |
75496 KB |
Output is correct |
3 |
Correct |
19 ms |
75612 KB |
Output is correct |
4 |
Correct |
16 ms |
75492 KB |
Output is correct |
5 |
Correct |
18 ms |
75612 KB |
Output is correct |
6 |
Correct |
18 ms |
75612 KB |
Output is correct |
7 |
Correct |
19 ms |
75608 KB |
Output is correct |
8 |
Correct |
18 ms |
75608 KB |
Output is correct |
9 |
Correct |
18 ms |
75612 KB |
Output is correct |
10 |
Correct |
18 ms |
75612 KB |
Output is correct |
11 |
Correct |
17 ms |
75616 KB |
Output is correct |
12 |
Correct |
19 ms |
75612 KB |
Output is correct |
13 |
Correct |
18 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75616 KB |
Output is correct |
15 |
Correct |
17 ms |
75608 KB |
Output is correct |
16 |
Correct |
20 ms |
75612 KB |
Output is correct |
17 |
Correct |
18 ms |
75612 KB |
Output is correct |
18 |
Correct |
19 ms |
75648 KB |
Output is correct |
19 |
Correct |
18 ms |
75612 KB |
Output is correct |
20 |
Correct |
19 ms |
75608 KB |
Output is correct |
21 |
Correct |
16 ms |
75612 KB |
Output is correct |
22 |
Correct |
17 ms |
75612 KB |
Output is correct |
23 |
Correct |
18 ms |
75608 KB |
Output is correct |
24 |
Correct |
18 ms |
75612 KB |
Output is correct |
25 |
Correct |
18 ms |
75608 KB |
Output is correct |
26 |
Correct |
18 ms |
75612 KB |
Output is correct |
27 |
Correct |
17 ms |
75500 KB |
Output is correct |
28 |
Correct |
20 ms |
75612 KB |
Output is correct |
29 |
Correct |
17 ms |
75752 KB |
Output is correct |
30 |
Correct |
18 ms |
75608 KB |
Output is correct |
31 |
Correct |
1002 ms |
116672 KB |
Output is correct |
32 |
Correct |
68 ms |
81356 KB |
Output is correct |
33 |
Correct |
642 ms |
101000 KB |
Output is correct |
34 |
Correct |
941 ms |
117728 KB |
Output is correct |
35 |
Correct |
899 ms |
112088 KB |
Output is correct |
36 |
Correct |
660 ms |
99812 KB |
Output is correct |
37 |
Correct |
455 ms |
102652 KB |
Output is correct |
38 |
Correct |
357 ms |
95696 KB |
Output is correct |
39 |
Correct |
323 ms |
97216 KB |
Output is correct |
40 |
Correct |
303 ms |
95420 KB |
Output is correct |
41 |
Correct |
717 ms |
104632 KB |
Output is correct |
42 |
Correct |
671 ms |
110008 KB |
Output is correct |
43 |
Correct |
69 ms |
81824 KB |
Output is correct |
44 |
Correct |
704 ms |
103700 KB |
Output is correct |
45 |
Correct |
659 ms |
100796 KB |
Output is correct |
46 |
Correct |
641 ms |
98256 KB |
Output is correct |
47 |
Correct |
319 ms |
103008 KB |
Output is correct |
48 |
Correct |
297 ms |
100540 KB |
Output is correct |
49 |
Correct |
395 ms |
104740 KB |
Output is correct |
50 |
Correct |
413 ms |
109324 KB |
Output is correct |
51 |
Correct |
421 ms |
103304 KB |
Output is correct |
52 |
Correct |
271 ms |
104376 KB |
Output is correct |
53 |
Correct |
287 ms |
102692 KB |
Output is correct |
54 |
Correct |
561 ms |
117696 KB |
Output is correct |
55 |
Correct |
560 ms |
106600 KB |
Output is correct |
56 |
Correct |
469 ms |
106940 KB |
Output is correct |
57 |
Correct |
649 ms |
105820 KB |
Output is correct |
58 |
Correct |
605 ms |
107708 KB |
Output is correct |
59 |
Correct |
493 ms |
108628 KB |
Output is correct |
60 |
Correct |
628 ms |
108804 KB |
Output is correct |
61 |
Correct |
72 ms |
86792 KB |
Output is correct |
62 |
Correct |
286 ms |
104016 KB |
Output is correct |
63 |
Correct |
456 ms |
112624 KB |
Output is correct |
64 |
Correct |
561 ms |
114108 KB |
Output is correct |
65 |
Correct |
658 ms |
113828 KB |
Output is correct |
66 |
Correct |
719 ms |
107128 KB |
Output is correct |
67 |
Correct |
139 ms |
87232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
75608 KB |
Output is correct |
2 |
Correct |
17 ms |
75496 KB |
Output is correct |
3 |
Correct |
19 ms |
75612 KB |
Output is correct |
4 |
Correct |
16 ms |
75492 KB |
Output is correct |
5 |
Correct |
18 ms |
75612 KB |
Output is correct |
6 |
Correct |
18 ms |
75612 KB |
Output is correct |
7 |
Correct |
19 ms |
75608 KB |
Output is correct |
8 |
Correct |
18 ms |
75608 KB |
Output is correct |
9 |
Correct |
18 ms |
75612 KB |
Output is correct |
10 |
Correct |
18 ms |
75612 KB |
Output is correct |
11 |
Correct |
17 ms |
75616 KB |
Output is correct |
12 |
Correct |
19 ms |
75612 KB |
Output is correct |
13 |
Correct |
18 ms |
75612 KB |
Output is correct |
14 |
Correct |
18 ms |
75616 KB |
Output is correct |
15 |
Correct |
17 ms |
75608 KB |
Output is correct |
16 |
Correct |
20 ms |
75612 KB |
Output is correct |
17 |
Correct |
18 ms |
75612 KB |
Output is correct |
18 |
Correct |
19 ms |
75648 KB |
Output is correct |
19 |
Correct |
18 ms |
75612 KB |
Output is correct |
20 |
Correct |
19 ms |
75608 KB |
Output is correct |
21 |
Correct |
16 ms |
75612 KB |
Output is correct |
22 |
Correct |
17 ms |
75612 KB |
Output is correct |
23 |
Correct |
18 ms |
75608 KB |
Output is correct |
24 |
Correct |
18 ms |
75612 KB |
Output is correct |
25 |
Correct |
18 ms |
75608 KB |
Output is correct |
26 |
Correct |
18 ms |
75612 KB |
Output is correct |
27 |
Correct |
17 ms |
75500 KB |
Output is correct |
28 |
Correct |
20 ms |
75612 KB |
Output is correct |
29 |
Correct |
17 ms |
75752 KB |
Output is correct |
30 |
Correct |
18 ms |
75608 KB |
Output is correct |
31 |
Correct |
1002 ms |
116672 KB |
Output is correct |
32 |
Correct |
68 ms |
81356 KB |
Output is correct |
33 |
Correct |
642 ms |
101000 KB |
Output is correct |
34 |
Correct |
941 ms |
117728 KB |
Output is correct |
35 |
Correct |
899 ms |
112088 KB |
Output is correct |
36 |
Correct |
660 ms |
99812 KB |
Output is correct |
37 |
Correct |
455 ms |
102652 KB |
Output is correct |
38 |
Correct |
357 ms |
95696 KB |
Output is correct |
39 |
Correct |
323 ms |
97216 KB |
Output is correct |
40 |
Correct |
303 ms |
95420 KB |
Output is correct |
41 |
Correct |
717 ms |
104632 KB |
Output is correct |
42 |
Correct |
671 ms |
110008 KB |
Output is correct |
43 |
Correct |
69 ms |
81824 KB |
Output is correct |
44 |
Correct |
704 ms |
103700 KB |
Output is correct |
45 |
Correct |
659 ms |
100796 KB |
Output is correct |
46 |
Correct |
641 ms |
98256 KB |
Output is correct |
47 |
Correct |
319 ms |
103008 KB |
Output is correct |
48 |
Correct |
297 ms |
100540 KB |
Output is correct |
49 |
Correct |
395 ms |
104740 KB |
Output is correct |
50 |
Correct |
413 ms |
109324 KB |
Output is correct |
51 |
Correct |
421 ms |
103304 KB |
Output is correct |
52 |
Correct |
3229 ms |
299380 KB |
Output is correct |
53 |
Correct |
2188 ms |
255968 KB |
Output is correct |
54 |
Correct |
1339 ms |
243460 KB |
Output is correct |
55 |
Correct |
2950 ms |
300960 KB |
Output is correct |
56 |
Correct |
1941 ms |
254592 KB |
Output is correct |
57 |
Correct |
2127 ms |
254416 KB |
Output is correct |
58 |
Correct |
1416 ms |
242608 KB |
Output is correct |
59 |
Correct |
2970 ms |
313776 KB |
Output is correct |
60 |
Correct |
3018 ms |
292008 KB |
Output is correct |
61 |
Correct |
2613 ms |
294880 KB |
Output is correct |
62 |
Correct |
2045 ms |
261036 KB |
Output is correct |
63 |
Correct |
2156 ms |
265904 KB |
Output is correct |
64 |
Execution timed out |
5036 ms |
310752 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |