#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
template<class... Args>
vec(size_t n = 0, Args... args)
: vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
template<class... Args>
vec(Args... args)
: vector<T>(args...) {}
};
template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }
mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));
struct IT {
int n;
struct Node {
array<stack<pair<int, int>>, 2> segments;
vec<pair<int, int>, 1> queries;
Node()
: segments({}),
queries() {}
};
vec<Node, 1> nodes;
IT(int t_n, const vec<pair<int, int>, 2>& queries_time_marks)
: n(t_n),
nodes(n << 1) {
for (int i = 0; i < n; ++i) {
nodes[i + n].queries = queries_time_marks[i];
}
for (int i = n - 1; i; --i) {
merge(ALL(nodes[i << 1].queries), ALL(nodes[i << 1 | 1].queries), back_inserter(nodes[i].queries));
}
}
void InsertSegment(int be, int en, bool slope, pair<int, int> val) {
for (be += n, en += n; be < en; be >>= 1, en >>= 1) {
if (be & 1) {
nodes[be++].segments[slope].emplace(val);
}
if (en & 1) {
nodes[--en].segments[slope].emplace(val);
}
}
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
constexpr int kMaxX = 2e8;
int n_stores, n_types, n_queries; cin >> n_stores >> n_types >> n_queries;
vec<int, 1> time_marks; time_marks.reserve((n_stores << 1) + n_queries);
vec<tuple<int, int, int>, 1> changes; changes.reserve(n_stores << 1);
while (n_stores--) {
int x, type, l, r; cin >> x >> type >> l >> r; x <<= 1; --type;
changes.emplace_back(l, x, type);
changes.emplace_back(r + 1, -x, type);
time_marks.emplace_back(l);
time_marks.emplace_back(r + 1);
}
sort(ALL(changes));
vec<pair<int, int>, 1> queries(n_queries);
for (auto& i : queries) {
cin >> i.first >> i.second; i.first <<= 1;
time_marks.emplace_back(i.second);
}
sort(ALL(time_marks));
time_marks.erase(unique(ALL(time_marks)), time_marks.end());
vec<int, 1> answers(n_queries);
vec<int, 1> ord_queries(n_queries); iota(ALL(ord_queries), 0);
sort(ALL(ord_queries), [&](int i, int j) { return queries[i].second < queries[j].second; });
ord_queries.emplace_back(-1);
vec<map<int, int>, 1> begin_times_types(n_types);
array<vec<tuple<int, int, int, int>, 1>, 2> segments;
for (auto& i_query : ord_queries) {
static auto it_changes = changes.begin();
static int n_open_types = 0;
static vec<map<int, int>, 1> n_occurrences_xs_types(n_types);
for (; it_changes != changes.end() && (!~i_query || get<0>(*it_changes) <= queries[i_query].second); ++it_changes) {
int cur_time, x, type; tie(cur_time, x, type) = *it_changes;
n_open_types -= !!SZ(n_occurrences_xs_types[type]);
cur_time = static_cast<int>(lower_bound(ALL(time_marks), cur_time) - time_marks.begin());
auto Insert = [&](int a) {
begin_times_types[type][a] = cur_time;
};
auto Erase = [&](int a, int b) {
int pre_time;
if (!a) {
if ((pre_time = begin_times_types[type][0]) < cur_time) {
segments[true].emplace_back(2, b, pre_time, cur_time);
}
} else if (b > kMaxX) {
if ((pre_time = begin_times_types[type][a]) < cur_time) {
segments[false].emplace_back(a, kMaxX, pre_time, cur_time);
}
} else {
if ((pre_time = begin_times_types[type][a]) < cur_time) {
segments[false].emplace_back(a, (a + b) >> 1, pre_time, cur_time);
segments[true].emplace_back((a + b) >> 1, b, pre_time, cur_time);
}
}
};
if (x > 0) {
auto it = n_occurrences_xs_types[type].find(x);
if (it == n_occurrences_xs_types[type].end()) {
it = n_occurrences_xs_types[type].emplace(x, 1).first;
if (it == n_occurrences_xs_types[type].begin() && next(it) == n_occurrences_xs_types[type].end()) {
Insert(0); Insert(x);
} else if (it == n_occurrences_xs_types[type].begin()) {
Erase(0, next(it)->first);
Insert(0); Insert(x);
} else if (next(it) == n_occurrences_xs_types[type].end()) {
Erase(prev(it)->first, kMaxX + 2);
Insert(prev(it)->first); Insert(x);
} else {
Erase(prev(it)->first, next(it)->first);
Insert(prev(it)->first); Insert(x);
}
} else {
++it->second;
}
} else {
x = -x;
auto it = n_occurrences_xs_types[type].find(x);
if (it->second == 1) {
if (it == n_occurrences_xs_types[type].begin() && next(it) == n_occurrences_xs_types[type].end()) {
Erase(0, x); Erase(x, kMaxX + 2);
} else if (it == n_occurrences_xs_types[type].begin()) {
Erase(0, x); Erase(x, next(it)->first);
Insert(0);
} else if (next(it) == n_occurrences_xs_types[type].end()) {
Erase(prev(it)->first, x); Erase(x, kMaxX + 2);
Insert(prev(it)->first);
} else {
Erase(prev(it)->first, x); Erase(x, next(it)->first);
Insert(prev(it)->first);
}
n_occurrences_xs_types[type].erase(it);
} else {
--it->second;
}
}
n_open_types += !!SZ(n_occurrences_xs_types[type]);
}
if (n_open_types != n_types && ~i_query) {
answers[i_query] = -1;
}
}
vec<pair<int, int>, 2> queries_time_marks(SZ(time_marks));
for (int i = 0; i < n_queries; ++i) {
if (answers[i]) {
continue;
}
queries_time_marks[static_cast<int>(lower_bound(ALL(time_marks), queries[i].second) - time_marks.begin())].emplace_back(queries[i].first, i);
}
for (auto& queries_time_mark : queries_time_marks) {
sort(ALL(queries_time_mark));
}
IT it(SZ(time_marks), queries_time_marks);
sort(ALL(segments[true]), [&](auto i, auto j) { return get<0>(i) > get<0>(j); });
for (auto& segment : segments[true]) {
int l_x, r_x, be_time, en_time; tie(l_x, r_x, be_time, en_time) = segment;
it.InsertSegment(be_time, en_time, true, make_pair(l_x, r_x));
}
sort(ALL(segments[false]), [&](auto i, auto j) { return get<1>(i) < get<1>(j); });
for (auto& segment : segments[false]) {
int l_x, r_x, be_time, en_time; tie(l_x, r_x, be_time, en_time) = segment;
it.InsertSegment(be_time, en_time, false, make_pair(l_x, r_x));
}
for (int i = 1; i < SZ(it.nodes); ++i) {
int max_x = 0;
for (auto& query : it.nodes[i].queries) {
for (; SZ(it.nodes[i].segments[true]) && it.nodes[i].segments[true].top().first <= query.first; it.nodes[i].segments[true].pop()) {
Maximize(max_x, it.nodes[i].segments[true].top().second);
}
Maximize(answers[query.second], max_x - query.first);
}
int min_x = kMaxX;
reverse(ALL(it.nodes[i].queries));
for (auto& query : it.nodes[i].queries) {
for (; SZ(it.nodes[i].segments[false]) && it.nodes[i].segments[false].top().second >= query.first; it.nodes[i].segments[false].pop()) {
Minimize(min_x, it.nodes[i].segments[false].top().first);
}
Maximize(answers[query.second], query.first - min_x);
}
}
for (auto& answer : answers) {
cout << (~answer ? answer >> 1 : -1) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
3840 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
3840 KB |
Output is correct |
9 |
Correct |
3 ms |
3840 KB |
Output is correct |
10 |
Correct |
5 ms |
3840 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
4 ms |
3840 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
5 ms |
3712 KB |
Output is correct |
15 |
Correct |
5 ms |
3840 KB |
Output is correct |
16 |
Correct |
5 ms |
3840 KB |
Output is correct |
17 |
Correct |
4 ms |
3840 KB |
Output is correct |
18 |
Correct |
4 ms |
3840 KB |
Output is correct |
19 |
Correct |
4 ms |
3840 KB |
Output is correct |
20 |
Correct |
4 ms |
3840 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
3840 KB |
Output is correct |
23 |
Correct |
4 ms |
3840 KB |
Output is correct |
24 |
Correct |
4 ms |
3840 KB |
Output is correct |
25 |
Correct |
4 ms |
3840 KB |
Output is correct |
26 |
Correct |
4 ms |
3888 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
3712 KB |
Output is correct |
29 |
Correct |
4 ms |
3712 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
3840 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
3840 KB |
Output is correct |
9 |
Correct |
3 ms |
3840 KB |
Output is correct |
10 |
Correct |
5 ms |
3840 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
4 ms |
3840 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
5 ms |
3712 KB |
Output is correct |
15 |
Correct |
5 ms |
3840 KB |
Output is correct |
16 |
Correct |
5 ms |
3840 KB |
Output is correct |
17 |
Correct |
4 ms |
3840 KB |
Output is correct |
18 |
Correct |
4 ms |
3840 KB |
Output is correct |
19 |
Correct |
4 ms |
3840 KB |
Output is correct |
20 |
Correct |
4 ms |
3840 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
3840 KB |
Output is correct |
23 |
Correct |
4 ms |
3840 KB |
Output is correct |
24 |
Correct |
4 ms |
3840 KB |
Output is correct |
25 |
Correct |
4 ms |
3840 KB |
Output is correct |
26 |
Correct |
4 ms |
3888 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
3712 KB |
Output is correct |
29 |
Correct |
4 ms |
3712 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
31 |
Correct |
1085 ms |
547236 KB |
Output is correct |
32 |
Correct |
67 ms |
7304 KB |
Output is correct |
33 |
Correct |
1022 ms |
543632 KB |
Output is correct |
34 |
Correct |
1110 ms |
542384 KB |
Output is correct |
35 |
Correct |
1106 ms |
547900 KB |
Output is correct |
36 |
Correct |
1093 ms |
548128 KB |
Output is correct |
37 |
Correct |
868 ms |
536424 KB |
Output is correct |
38 |
Correct |
833 ms |
536424 KB |
Output is correct |
39 |
Correct |
711 ms |
528116 KB |
Output is correct |
40 |
Correct |
715 ms |
529540 KB |
Output is correct |
41 |
Correct |
770 ms |
527220 KB |
Output is correct |
42 |
Correct |
736 ms |
520328 KB |
Output is correct |
43 |
Correct |
61 ms |
6792 KB |
Output is correct |
44 |
Correct |
775 ms |
527612 KB |
Output is correct |
45 |
Correct |
755 ms |
525940 KB |
Output is correct |
46 |
Correct |
810 ms |
526196 KB |
Output is correct |
47 |
Correct |
567 ms |
503140 KB |
Output is correct |
48 |
Correct |
572 ms |
503312 KB |
Output is correct |
49 |
Correct |
597 ms |
510708 KB |
Output is correct |
50 |
Correct |
646 ms |
515312 KB |
Output is correct |
51 |
Correct |
623 ms |
510068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2161 ms |
1046128 KB |
Output is correct |
2 |
Correct |
2020 ms |
1037056 KB |
Output is correct |
3 |
Runtime error |
1481 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1822 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
3840 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
3840 KB |
Output is correct |
9 |
Correct |
3 ms |
3840 KB |
Output is correct |
10 |
Correct |
5 ms |
3840 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
4 ms |
3840 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
5 ms |
3712 KB |
Output is correct |
15 |
Correct |
5 ms |
3840 KB |
Output is correct |
16 |
Correct |
5 ms |
3840 KB |
Output is correct |
17 |
Correct |
4 ms |
3840 KB |
Output is correct |
18 |
Correct |
4 ms |
3840 KB |
Output is correct |
19 |
Correct |
4 ms |
3840 KB |
Output is correct |
20 |
Correct |
4 ms |
3840 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
3840 KB |
Output is correct |
23 |
Correct |
4 ms |
3840 KB |
Output is correct |
24 |
Correct |
4 ms |
3840 KB |
Output is correct |
25 |
Correct |
4 ms |
3840 KB |
Output is correct |
26 |
Correct |
4 ms |
3888 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
3712 KB |
Output is correct |
29 |
Correct |
4 ms |
3712 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
31 |
Correct |
1085 ms |
547236 KB |
Output is correct |
32 |
Correct |
67 ms |
7304 KB |
Output is correct |
33 |
Correct |
1022 ms |
543632 KB |
Output is correct |
34 |
Correct |
1110 ms |
542384 KB |
Output is correct |
35 |
Correct |
1106 ms |
547900 KB |
Output is correct |
36 |
Correct |
1093 ms |
548128 KB |
Output is correct |
37 |
Correct |
868 ms |
536424 KB |
Output is correct |
38 |
Correct |
833 ms |
536424 KB |
Output is correct |
39 |
Correct |
711 ms |
528116 KB |
Output is correct |
40 |
Correct |
715 ms |
529540 KB |
Output is correct |
41 |
Correct |
770 ms |
527220 KB |
Output is correct |
42 |
Correct |
736 ms |
520328 KB |
Output is correct |
43 |
Correct |
61 ms |
6792 KB |
Output is correct |
44 |
Correct |
775 ms |
527612 KB |
Output is correct |
45 |
Correct |
755 ms |
525940 KB |
Output is correct |
46 |
Correct |
810 ms |
526196 KB |
Output is correct |
47 |
Correct |
567 ms |
503140 KB |
Output is correct |
48 |
Correct |
572 ms |
503312 KB |
Output is correct |
49 |
Correct |
597 ms |
510708 KB |
Output is correct |
50 |
Correct |
646 ms |
515312 KB |
Output is correct |
51 |
Correct |
623 ms |
510068 KB |
Output is correct |
52 |
Correct |
619 ms |
525056 KB |
Output is correct |
53 |
Correct |
619 ms |
519812 KB |
Output is correct |
54 |
Correct |
890 ms |
526324 KB |
Output is correct |
55 |
Correct |
729 ms |
528612 KB |
Output is correct |
56 |
Correct |
748 ms |
528624 KB |
Output is correct |
57 |
Correct |
901 ms |
527348 KB |
Output is correct |
58 |
Correct |
832 ms |
524784 KB |
Output is correct |
59 |
Correct |
675 ms |
524916 KB |
Output is correct |
60 |
Correct |
742 ms |
522484 KB |
Output is correct |
61 |
Correct |
93 ms |
23808 KB |
Output is correct |
62 |
Correct |
585 ms |
527948 KB |
Output is correct |
63 |
Correct |
851 ms |
528500 KB |
Output is correct |
64 |
Correct |
890 ms |
531316 KB |
Output is correct |
65 |
Correct |
876 ms |
532488 KB |
Output is correct |
66 |
Correct |
775 ms |
529424 KB |
Output is correct |
67 |
Correct |
145 ms |
17520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
3840 KB |
Output is correct |
7 |
Correct |
3 ms |
3840 KB |
Output is correct |
8 |
Correct |
3 ms |
3840 KB |
Output is correct |
9 |
Correct |
3 ms |
3840 KB |
Output is correct |
10 |
Correct |
5 ms |
3840 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
4 ms |
3840 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
5 ms |
3712 KB |
Output is correct |
15 |
Correct |
5 ms |
3840 KB |
Output is correct |
16 |
Correct |
5 ms |
3840 KB |
Output is correct |
17 |
Correct |
4 ms |
3840 KB |
Output is correct |
18 |
Correct |
4 ms |
3840 KB |
Output is correct |
19 |
Correct |
4 ms |
3840 KB |
Output is correct |
20 |
Correct |
4 ms |
3840 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
3840 KB |
Output is correct |
23 |
Correct |
4 ms |
3840 KB |
Output is correct |
24 |
Correct |
4 ms |
3840 KB |
Output is correct |
25 |
Correct |
4 ms |
3840 KB |
Output is correct |
26 |
Correct |
4 ms |
3888 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
3712 KB |
Output is correct |
29 |
Correct |
4 ms |
3712 KB |
Output is correct |
30 |
Correct |
3 ms |
3328 KB |
Output is correct |
31 |
Correct |
1085 ms |
547236 KB |
Output is correct |
32 |
Correct |
67 ms |
7304 KB |
Output is correct |
33 |
Correct |
1022 ms |
543632 KB |
Output is correct |
34 |
Correct |
1110 ms |
542384 KB |
Output is correct |
35 |
Correct |
1106 ms |
547900 KB |
Output is correct |
36 |
Correct |
1093 ms |
548128 KB |
Output is correct |
37 |
Correct |
868 ms |
536424 KB |
Output is correct |
38 |
Correct |
833 ms |
536424 KB |
Output is correct |
39 |
Correct |
711 ms |
528116 KB |
Output is correct |
40 |
Correct |
715 ms |
529540 KB |
Output is correct |
41 |
Correct |
770 ms |
527220 KB |
Output is correct |
42 |
Correct |
736 ms |
520328 KB |
Output is correct |
43 |
Correct |
61 ms |
6792 KB |
Output is correct |
44 |
Correct |
775 ms |
527612 KB |
Output is correct |
45 |
Correct |
755 ms |
525940 KB |
Output is correct |
46 |
Correct |
810 ms |
526196 KB |
Output is correct |
47 |
Correct |
567 ms |
503140 KB |
Output is correct |
48 |
Correct |
572 ms |
503312 KB |
Output is correct |
49 |
Correct |
597 ms |
510708 KB |
Output is correct |
50 |
Correct |
646 ms |
515312 KB |
Output is correct |
51 |
Correct |
623 ms |
510068 KB |
Output is correct |
52 |
Correct |
2161 ms |
1046128 KB |
Output is correct |
53 |
Correct |
2020 ms |
1037056 KB |
Output is correct |
54 |
Runtime error |
1481 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
55 |
Halted |
0 ms |
0 KB |
- |