답안 #252940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
252940 2020-07-26T14:04:37 Z EntityIT 새 집 (APIO18_new_home) C++14
57 / 100
5000 ms 307148 KB
#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 Node {
	array<vec<int, 1>, 2> segment_indices;
	vec<int, 1> query_indices;
	Node()
			: segment_indices({}),
				query_indices({}) {}
};

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, bool>, 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, true);
    changes.emplace_back(r + 1, x, type, false);
    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);
  array<vec<map<int, int>, 1>, 2> begin_times_types{vec<map<int, int>, 1>(n_types), vec<map<int, int>, 1>(n_types)};
  array<vec<tuple<int, int, int, int>, 1>, 2> segments; segments[0].reserve(SZ(changes) << 1); segments[1].reserve(SZ(changes) << 1);
  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; bool insertion; tie(cur_time, x, type, insertion) = *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, int b) {
        if (!a) {
          begin_times_types[true][type][2] = cur_time;
        } else if (b > kMaxX) {
          begin_times_types[false][type][a] = cur_time;
        } else {
          begin_times_types[false][type][a] = begin_times_types[true][type][(a + b) >> 1] = cur_time;
        }
      };
      auto Erase = [&](int a, int b) {
        if (!a) {
          segments[true].emplace_back(2, b, begin_times_types[true][type][2], cur_time);
          if (get<2>(segments[true].back()) == get<3>(segments[true].back())) {
            segments[true].pop_back();
          }
          begin_times_types[true][type].erase(2);
        } else if (b > kMaxX) {
          segments[false].emplace_back(a, kMaxX, begin_times_types[false][type][a], cur_time);
          if (get<2>(segments[false].back()) == get<3>(segments[false].back())) {
            segments[false].pop_back();
          }
          begin_times_types[false][type].erase(a);
        } else {
          segments[false].emplace_back(a, (a + b) >> 1, begin_times_types[false][type][a], cur_time);
          if (get<2>(segments[false].back()) == get<3>(segments[false].back())) {
            segments[false].pop_back();
          }
          begin_times_types[false][type].erase(a);
          segments[true].emplace_back((a + b) >> 1, b, begin_times_types[true][type][(a + b) >> 1], cur_time);
          if (get<2>(segments[true].back()) == get<3>(segments[true].back())) {
            segments[true].pop_back();
          }
          begin_times_types[true][type].erase((a + b) >> 1);
        }
      };

			auto it = n_occurrences_xs_types[type].find(x);
      if (insertion) {
        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, x); Insert(x, kMaxX + 2);
          } else if (it == n_occurrences_xs_types[type].begin()) {
            Erase(0, next(it)->first);
            Insert(0, x); Insert(x, next(it)->first);
          } else if (next(it) == n_occurrences_xs_types[type].end()) {
            Erase(prev(it)->first, kMaxX + 2);
            Insert(prev(it)->first, x); Insert(x, kMaxX + 2);
          } else {
            Erase(prev(it)->first, next(it)->first);
            Insert(prev(it)->first, x); Insert(x, next(it)->first);
          }
        } else {
					++it->second;
				}
      } else {
        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, next(it)->first);
          } else if (next(it) == n_occurrences_xs_types[type].end()) {
            Erase(prev(it)->first, x); Erase(x, kMaxX + 2);
            Insert(prev(it)->first, kMaxX + 2);
          } else {
            Erase(prev(it)->first, x); Erase(x, next(it)->first);
            Insert(prev(it)->first, next(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<Node, 1> nodes(SZ(time_marks) << 1);
  for (int i = 0; i < n_queries; ++i) {
    if (answers[i]) {
      continue;
    }
    nodes[SZ(time_marks) + static_cast<int>(lower_bound(ALL(time_marks), queries[i].second) - time_marks.begin())].query_indices.emplace_back(i);
  }
  for (int i = 0; i < SZ(time_marks); ++i) {
		sort(ALL(nodes[SZ(time_marks) + i].query_indices), [&](int x, int y) { return queries[x].first < queries[y].first; });
	}
	for (int i = SZ(time_marks) - 1; i; --i) {
		merge(ALL(nodes[i << 1].query_indices), ALL(nodes[i << 1 | 1].query_indices), back_inserter(nodes[i].query_indices), [&](int x, int y) { return queries[x].first < queries[y].first; });
	}

	array<vec<int, 1>, 2> segment_indices;

  segment_indices[true] = vec<int, 1>(SZ(segments[true])); iota(ALL(segment_indices[true]), 0);
	sort(ALL(segment_indices[true]), [&](int i, int j) { return get<0>(segments[true][i]) < get<0>(segments[true][j]); });
	for (auto& index : segment_indices[true]) {
		int be = get<2>(segments[true][index]), en = get<3>(segments[true][index]);
		for (be += SZ(time_marks), en += SZ(time_marks); be < en; be >>= 1, en >>= 1) {
			if (be & 1) {
				nodes[be++].segment_indices[true].emplace_back(index);
			}
			if (en & 1) {
				nodes[--en].segment_indices[true].emplace_back(index);
			}
		}
	}

	segment_indices[false] = vec<int, 1>(SZ(segments[false])); iota(ALL(segment_indices[false]), 0);
	sort(ALL(segment_indices[false]), [&](int i, int j) { return get<1>(segments[false][i]) > get<1>(segments[false][j]); });
	for (auto& index : segment_indices[false]) {
		int be = get<2>(segments[false][index]), en = get<3>(segments[false][index]);
		for (be += SZ(time_marks), en += SZ(time_marks); be < en; be >>= 1, en >>= 1) {
			if (be & 1) {
				nodes[be++].segment_indices[false].emplace_back(index);
			}
			if (en & 1) {
				nodes[--en].segment_indices[false].emplace_back(index);
			}
		}
	}

  for (int i = 1; i < SZ(nodes); ++i) {
    int max_x = 0;
    auto it_segment_indices = nodes[i].segment_indices[true].begin();
    for (auto& query_index : nodes[i].query_indices) {
      for (; it_segment_indices != nodes[i].segment_indices[true].end() && get<0>(segments[true][*it_segment_indices]) <= queries[query_index].first; ++it_segment_indices) {
        Maximize(max_x, get<1>(segments[true][*it_segment_indices]));
      }
      Maximize(answers[query_index], max_x - queries[query_index].first);
    }

    int min_x = kMaxX;
    it_segment_indices = nodes[i].segment_indices[false].begin();
    reverse(ALL(nodes[i].query_indices));
    for (auto& query_index : nodes[i].query_indices) {
      for (; it_segment_indices != nodes[i].segment_indices[false].end() && get<1>(segments[false][*it_segment_indices]) >= queries[query_index].first; ++it_segment_indices) {
        Minimize(min_x, get<0>(segments[false][*it_segment_indices]));
      }
      Maximize(answers[query_index], queries[query_index].first - min_x);
    }
  }

  for (auto& answer : answers) {
    cout << (~answer ? answer >> 1 : -1) << '\n';
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 2 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 2 ms 768 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 768 KB Output is correct
16 Correct 3 ms 768 KB Output is correct
17 Correct 3 ms 768 KB Output is correct
18 Correct 2 ms 768 KB Output is correct
19 Correct 2 ms 768 KB Output is correct
20 Correct 2 ms 768 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 2 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 2 ms 768 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 2 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 2 ms 768 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 768 KB Output is correct
16 Correct 3 ms 768 KB Output is correct
17 Correct 3 ms 768 KB Output is correct
18 Correct 2 ms 768 KB Output is correct
19 Correct 2 ms 768 KB Output is correct
20 Correct 2 ms 768 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 2 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 2 ms 768 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1002 ms 87696 KB Output is correct
32 Correct 62 ms 6540 KB Output is correct
33 Correct 977 ms 85520 KB Output is correct
34 Correct 997 ms 84364 KB Output is correct
35 Correct 1056 ms 88244 KB Output is correct
36 Correct 1163 ms 88464 KB Output is correct
37 Correct 762 ms 81060 KB Output is correct
38 Correct 792 ms 81292 KB Output is correct
39 Correct 561 ms 74124 KB Output is correct
40 Correct 664 ms 76048 KB Output is correct
41 Correct 629 ms 71184 KB Output is correct
42 Correct 558 ms 66188 KB Output is correct
43 Correct 72 ms 7824 KB Output is correct
44 Correct 654 ms 70540 KB Output is correct
45 Correct 691 ms 67852 KB Output is correct
46 Correct 498 ms 63884 KB Output is correct
47 Correct 291 ms 55820 KB Output is correct
48 Correct 297 ms 57868 KB Output is correct
49 Correct 350 ms 60940 KB Output is correct
50 Correct 376 ms 62988 KB Output is correct
51 Correct 350 ms 60684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2047 ms 201296 KB Output is correct
2 Correct 1462 ms 188188 KB Output is correct
3 Correct 1553 ms 233220 KB Output is correct
4 Correct 1590 ms 203620 KB Output is correct
5 Correct 1650 ms 186852 KB Output is correct
6 Correct 1391 ms 187236 KB Output is correct
7 Correct 1507 ms 231832 KB Output is correct
8 Correct 1904 ms 203348 KB Output is correct
9 Correct 2114 ms 193252 KB Output is correct
10 Correct 1707 ms 187620 KB Output is correct
11 Correct 1226 ms 189064 KB Output is correct
12 Correct 1466 ms 189732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5098 ms 307148 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 2 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 2 ms 768 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 768 KB Output is correct
16 Correct 3 ms 768 KB Output is correct
17 Correct 3 ms 768 KB Output is correct
18 Correct 2 ms 768 KB Output is correct
19 Correct 2 ms 768 KB Output is correct
20 Correct 2 ms 768 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 2 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 2 ms 768 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1002 ms 87696 KB Output is correct
32 Correct 62 ms 6540 KB Output is correct
33 Correct 977 ms 85520 KB Output is correct
34 Correct 997 ms 84364 KB Output is correct
35 Correct 1056 ms 88244 KB Output is correct
36 Correct 1163 ms 88464 KB Output is correct
37 Correct 762 ms 81060 KB Output is correct
38 Correct 792 ms 81292 KB Output is correct
39 Correct 561 ms 74124 KB Output is correct
40 Correct 664 ms 76048 KB Output is correct
41 Correct 629 ms 71184 KB Output is correct
42 Correct 558 ms 66188 KB Output is correct
43 Correct 72 ms 7824 KB Output is correct
44 Correct 654 ms 70540 KB Output is correct
45 Correct 691 ms 67852 KB Output is correct
46 Correct 498 ms 63884 KB Output is correct
47 Correct 291 ms 55820 KB Output is correct
48 Correct 297 ms 57868 KB Output is correct
49 Correct 350 ms 60940 KB Output is correct
50 Correct 376 ms 62988 KB Output is correct
51 Correct 350 ms 60684 KB Output is correct
52 Correct 368 ms 60556 KB Output is correct
53 Correct 347 ms 61068 KB Output is correct
54 Correct 656 ms 67052 KB Output is correct
55 Correct 588 ms 70156 KB Output is correct
56 Correct 558 ms 69900 KB Output is correct
57 Correct 579 ms 70748 KB Output is correct
58 Correct 502 ms 67380 KB Output is correct
59 Correct 534 ms 66188 KB Output is correct
60 Correct 574 ms 67724 KB Output is correct
61 Correct 93 ms 25740 KB Output is correct
62 Correct 321 ms 62276 KB Output is correct
63 Correct 468 ms 67016 KB Output is correct
64 Correct 525 ms 70284 KB Output is correct
65 Correct 603 ms 73484 KB Output is correct
66 Correct 660 ms 72972 KB Output is correct
67 Correct 141 ms 14888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 2 ms 768 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 2 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 2 ms 768 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 768 KB Output is correct
16 Correct 3 ms 768 KB Output is correct
17 Correct 3 ms 768 KB Output is correct
18 Correct 2 ms 768 KB Output is correct
19 Correct 2 ms 768 KB Output is correct
20 Correct 2 ms 768 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 2 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 2 ms 768 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1002 ms 87696 KB Output is correct
32 Correct 62 ms 6540 KB Output is correct
33 Correct 977 ms 85520 KB Output is correct
34 Correct 997 ms 84364 KB Output is correct
35 Correct 1056 ms 88244 KB Output is correct
36 Correct 1163 ms 88464 KB Output is correct
37 Correct 762 ms 81060 KB Output is correct
38 Correct 792 ms 81292 KB Output is correct
39 Correct 561 ms 74124 KB Output is correct
40 Correct 664 ms 76048 KB Output is correct
41 Correct 629 ms 71184 KB Output is correct
42 Correct 558 ms 66188 KB Output is correct
43 Correct 72 ms 7824 KB Output is correct
44 Correct 654 ms 70540 KB Output is correct
45 Correct 691 ms 67852 KB Output is correct
46 Correct 498 ms 63884 KB Output is correct
47 Correct 291 ms 55820 KB Output is correct
48 Correct 297 ms 57868 KB Output is correct
49 Correct 350 ms 60940 KB Output is correct
50 Correct 376 ms 62988 KB Output is correct
51 Correct 350 ms 60684 KB Output is correct
52 Correct 2047 ms 201296 KB Output is correct
53 Correct 1462 ms 188188 KB Output is correct
54 Correct 1553 ms 233220 KB Output is correct
55 Correct 1590 ms 203620 KB Output is correct
56 Correct 1650 ms 186852 KB Output is correct
57 Correct 1391 ms 187236 KB Output is correct
58 Correct 1507 ms 231832 KB Output is correct
59 Correct 1904 ms 203348 KB Output is correct
60 Correct 2114 ms 193252 KB Output is correct
61 Correct 1707 ms 187620 KB Output is correct
62 Correct 1226 ms 189064 KB Output is correct
63 Correct 1466 ms 189732 KB Output is correct
64 Execution timed out 5098 ms 307148 KB Time limit exceeded
65 Halted 0 ms 0 KB -