Submission #876327

# Submission time Handle Problem Language Result Execution time Memory
876327 2023-11-21T14:37:43 Z marvinthang New Home (APIO18_new_home) C++17
100 / 100
3420 ms 81632 KB
/*************************************
*    author: marvinthang             *
*    created: 21.11.2023 14:25:44    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

const int INF = 1e8 + 23;

struct Store {
	int x, t, a, b;
	Store(int x, int t, int a, int b): x(x), t(t), a(a), b(b) {}
	bool operator < (const Store &ot) const {
		return x < ot.x;
	}
};

void process(void) {
	int n, q, k; cin >> n >> k >> q;
	vector <Store> stores;
	vector <int> cp;
	vector <set <int>> pos(k);
	REP(i, n) {
		int x, t, a, b; cin >> x >> t >> a >> b; --t;
		stores.emplace_back(x, t, a, b);
	}
	sort(ALL(stores));
	vector <pair <int, int>> queries(q); cin >> queries;

	vector <pair <int, int>> events;
	REP(i, n) {
		events.emplace_back(stores[i].a, i);
		events.emplace_back(stores[i].b + 1, i);
	}
	REP(i, q) events.emplace_back(queries[i].se, i + n);
	sort(ALL(events));

	multiset <int> last;
	REP(i, k) last.insert(-1);

	auto cmp = [&] (const Store &a, const int &b) { return a.x < b; };

	vector <int> st(n * 4 + 23, INF);
	function <void(int, int, int, int, int)> update = [&] (int i, int l, int r, int p, int v) {
		if (r - l == 1) {
			st[i] = v;
			return;
		}
		int m = l + r >> 1;
		if (p < m) update(i << 1, l, m, p, v);
		else update(i << 1 | 1, m, r, p, v);
		st[i] = min(st[i << 1], st[i << 1 | 1]);
	};

	function <int(int, int, int, int)> get = [&] (int i, int l, int r, int p) {
		if (r - l == 1) return st[i];
		int m = l + r >> 1;
		if (p >= m) return get(i << 1 | 1, m, r, p);
		return min(get(i << 1, l, m, p), st[i << 1 | 1]);
	};

	update(1, 0, n + 1, n, -1);
	vector <int> res(q);
	for (auto [y, i]: events) {
		if (i < n) {
			auto [x, t, a, b] = stores[i];
			if (y == a) {
				auto it = pos[t].insert(i).fi;
				int prv = it == pos[t].begin() ? -1 : *prev(it);
				int nxt = next(it) == pos[t].end() ? n : *next(it);
				if (nxt == n) {
					last.erase(last.find(prv));
					last.insert(i);
					update(1, 0, n + 1, nxt, *last.begin());
				} else update(1, 0, n + 1, nxt, i);
				update(1, 0, n + 1, i, prv);
			} else {
				auto it = pos[t].find(i);
				int prv = it == pos[t].begin() ? -1 : *prev(it);
				int nxt = next(it) == pos[t].end() ? n : *next(it);
				if (nxt == n) {
					last.erase(i);
					last.insert(prv);
					update(1, 0, n + 1, nxt, *last.begin());
				} else update(1, 0, n + 1, nxt, prv);
				update(1, 0, n + 1, i, INF);
				pos[t].erase(it);
			}
		} else {
			i -= n;
			int l = 0, r = INF;
			while (l <= r) {
				int m = (l + r) >> 1;
				int ll = lower_bound(ALL(stores), queries[i].fi - m, cmp) - stores.begin();
				int rr = lower_bound(ALL(stores), queries[i].fi + m + 1, cmp) - stores.begin();
				if (ll < rr && get(1, 0, n + 1, rr) >= ll) r = m - 1;
				else l = m + 1;
			}
			res[i] = l > INF ? -1 : l;
		}
	}
	REP(i, q) cout << res[i] << '\n';
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("new_home");
	// int t; cin >> t; while (t--)
	process();
	// cerr << "Time elapsed: " << TIME << " s.\n";
	return (0^0);
}

Compilation message

new_home.cpp: In lambda function:
new_home.cpp:86:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |   int m = l + r >> 1;
      |           ~~^~~
new_home.cpp: In lambda function:
new_home.cpp:94:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |   int m = l + r >> 1;
      |           ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:144:2: note: in expansion of macro 'file'
  144 |  file("new_home");
      |  ^~~~
new_home.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:144:2: note: in expansion of macro 'file'
  144 |  file("new_home");
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 500 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 496 KB Output is correct
16 Correct 2 ms 500 KB Output is correct
17 Correct 2 ms 520 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 472 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 500 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 496 KB Output is correct
16 Correct 2 ms 500 KB Output is correct
17 Correct 2 ms 520 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 472 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 485 ms 10496 KB Output is correct
32 Correct 245 ms 6588 KB Output is correct
33 Correct 522 ms 9244 KB Output is correct
34 Correct 502 ms 8720 KB Output is correct
35 Correct 529 ms 10440 KB Output is correct
36 Correct 548 ms 10264 KB Output is correct
37 Correct 483 ms 7792 KB Output is correct
38 Correct 475 ms 7756 KB Output is correct
39 Correct 415 ms 7568 KB Output is correct
40 Correct 444 ms 7576 KB Output is correct
41 Correct 335 ms 7800 KB Output is correct
42 Correct 299 ms 7728 KB Output is correct
43 Correct 129 ms 9972 KB Output is correct
44 Correct 328 ms 7920 KB Output is correct
45 Correct 344 ms 7972 KB Output is correct
46 Correct 375 ms 7648 KB Output is correct
47 Correct 293 ms 7236 KB Output is correct
48 Correct 320 ms 7292 KB Output is correct
49 Correct 345 ms 7492 KB Output is correct
50 Correct 318 ms 7492 KB Output is correct
51 Correct 372 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1838 ms 56892 KB Output is correct
2 Correct 2804 ms 50292 KB Output is correct
3 Correct 1845 ms 81632 KB Output is correct
4 Correct 1835 ms 62544 KB Output is correct
5 Correct 2753 ms 49820 KB Output is correct
6 Correct 2809 ms 50216 KB Output is correct
7 Correct 1834 ms 80500 KB Output is correct
8 Correct 1952 ms 61668 KB Output is correct
9 Correct 2218 ms 54788 KB Output is correct
10 Correct 3168 ms 50832 KB Output is correct
11 Correct 2356 ms 49864 KB Output is correct
12 Correct 2475 ms 49500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2738 ms 50376 KB Output is correct
2 Correct 1375 ms 41040 KB Output is correct
3 Correct 3388 ms 48684 KB Output is correct
4 Correct 2086 ms 77872 KB Output is correct
5 Correct 2025 ms 55824 KB Output is correct
6 Correct 2044 ms 60728 KB Output is correct
7 Correct 3167 ms 47960 KB Output is correct
8 Correct 3312 ms 50208 KB Output is correct
9 Correct 2055 ms 78596 KB Output is correct
10 Correct 2170 ms 58304 KB Output is correct
11 Correct 2508 ms 53440 KB Output is correct
12 Correct 3365 ms 49708 KB Output is correct
13 Correct 2181 ms 47180 KB Output is correct
14 Correct 2185 ms 49348 KB Output is correct
15 Correct 2460 ms 47768 KB Output is correct
16 Correct 2549 ms 51128 KB Output is correct
17 Correct 2577 ms 49264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 500 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 496 KB Output is correct
16 Correct 2 ms 500 KB Output is correct
17 Correct 2 ms 520 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 472 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 485 ms 10496 KB Output is correct
32 Correct 245 ms 6588 KB Output is correct
33 Correct 522 ms 9244 KB Output is correct
34 Correct 502 ms 8720 KB Output is correct
35 Correct 529 ms 10440 KB Output is correct
36 Correct 548 ms 10264 KB Output is correct
37 Correct 483 ms 7792 KB Output is correct
38 Correct 475 ms 7756 KB Output is correct
39 Correct 415 ms 7568 KB Output is correct
40 Correct 444 ms 7576 KB Output is correct
41 Correct 335 ms 7800 KB Output is correct
42 Correct 299 ms 7728 KB Output is correct
43 Correct 129 ms 9972 KB Output is correct
44 Correct 328 ms 7920 KB Output is correct
45 Correct 344 ms 7972 KB Output is correct
46 Correct 375 ms 7648 KB Output is correct
47 Correct 293 ms 7236 KB Output is correct
48 Correct 320 ms 7292 KB Output is correct
49 Correct 345 ms 7492 KB Output is correct
50 Correct 318 ms 7492 KB Output is correct
51 Correct 372 ms 7372 KB Output is correct
52 Correct 323 ms 15948 KB Output is correct
53 Correct 307 ms 14204 KB Output is correct
54 Correct 297 ms 12364 KB Output is correct
55 Correct 339 ms 10848 KB Output is correct
56 Correct 332 ms 12104 KB Output is correct
57 Correct 338 ms 9040 KB Output is correct
58 Correct 312 ms 10572 KB Output is correct
59 Correct 319 ms 12060 KB Output is correct
60 Correct 308 ms 8636 KB Output is correct
61 Correct 149 ms 16712 KB Output is correct
62 Correct 317 ms 16204 KB Output is correct
63 Correct 305 ms 12936 KB Output is correct
64 Correct 305 ms 11596 KB Output is correct
65 Correct 304 ms 8944 KB Output is correct
66 Correct 315 ms 8016 KB Output is correct
67 Correct 243 ms 6956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 500 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 496 KB Output is correct
16 Correct 2 ms 500 KB Output is correct
17 Correct 2 ms 520 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 2 ms 472 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 485 ms 10496 KB Output is correct
32 Correct 245 ms 6588 KB Output is correct
33 Correct 522 ms 9244 KB Output is correct
34 Correct 502 ms 8720 KB Output is correct
35 Correct 529 ms 10440 KB Output is correct
36 Correct 548 ms 10264 KB Output is correct
37 Correct 483 ms 7792 KB Output is correct
38 Correct 475 ms 7756 KB Output is correct
39 Correct 415 ms 7568 KB Output is correct
40 Correct 444 ms 7576 KB Output is correct
41 Correct 335 ms 7800 KB Output is correct
42 Correct 299 ms 7728 KB Output is correct
43 Correct 129 ms 9972 KB Output is correct
44 Correct 328 ms 7920 KB Output is correct
45 Correct 344 ms 7972 KB Output is correct
46 Correct 375 ms 7648 KB Output is correct
47 Correct 293 ms 7236 KB Output is correct
48 Correct 320 ms 7292 KB Output is correct
49 Correct 345 ms 7492 KB Output is correct
50 Correct 318 ms 7492 KB Output is correct
51 Correct 372 ms 7372 KB Output is correct
52 Correct 1838 ms 56892 KB Output is correct
53 Correct 2804 ms 50292 KB Output is correct
54 Correct 1845 ms 81632 KB Output is correct
55 Correct 1835 ms 62544 KB Output is correct
56 Correct 2753 ms 49820 KB Output is correct
57 Correct 2809 ms 50216 KB Output is correct
58 Correct 1834 ms 80500 KB Output is correct
59 Correct 1952 ms 61668 KB Output is correct
60 Correct 2218 ms 54788 KB Output is correct
61 Correct 3168 ms 50832 KB Output is correct
62 Correct 2356 ms 49864 KB Output is correct
63 Correct 2475 ms 49500 KB Output is correct
64 Correct 2738 ms 50376 KB Output is correct
65 Correct 1375 ms 41040 KB Output is correct
66 Correct 3388 ms 48684 KB Output is correct
67 Correct 2086 ms 77872 KB Output is correct
68 Correct 2025 ms 55824 KB Output is correct
69 Correct 2044 ms 60728 KB Output is correct
70 Correct 3167 ms 47960 KB Output is correct
71 Correct 3312 ms 50208 KB Output is correct
72 Correct 2055 ms 78596 KB Output is correct
73 Correct 2170 ms 58304 KB Output is correct
74 Correct 2508 ms 53440 KB Output is correct
75 Correct 3365 ms 49708 KB Output is correct
76 Correct 2181 ms 47180 KB Output is correct
77 Correct 2185 ms 49348 KB Output is correct
78 Correct 2460 ms 47768 KB Output is correct
79 Correct 2549 ms 51128 KB Output is correct
80 Correct 2577 ms 49264 KB Output is correct
81 Correct 323 ms 15948 KB Output is correct
82 Correct 307 ms 14204 KB Output is correct
83 Correct 297 ms 12364 KB Output is correct
84 Correct 339 ms 10848 KB Output is correct
85 Correct 332 ms 12104 KB Output is correct
86 Correct 338 ms 9040 KB Output is correct
87 Correct 312 ms 10572 KB Output is correct
88 Correct 319 ms 12060 KB Output is correct
89 Correct 308 ms 8636 KB Output is correct
90 Correct 149 ms 16712 KB Output is correct
91 Correct 317 ms 16204 KB Output is correct
92 Correct 305 ms 12936 KB Output is correct
93 Correct 305 ms 11596 KB Output is correct
94 Correct 304 ms 8944 KB Output is correct
95 Correct 315 ms 8016 KB Output is correct
96 Correct 243 ms 6956 KB Output is correct
97 Correct 2202 ms 80604 KB Output is correct
98 Correct 1334 ms 31828 KB Output is correct
99 Correct 3202 ms 41876 KB Output is correct
100 Correct 2063 ms 71012 KB Output is correct
101 Correct 2079 ms 62468 KB Output is correct
102 Correct 3420 ms 51924 KB Output is correct
103 Correct 2852 ms 39536 KB Output is correct
104 Correct 2791 ms 39124 KB Output is correct
105 Correct 2514 ms 38192 KB Output is correct
106 Correct 2592 ms 38636 KB Output is correct
107 Correct 2047 ms 54576 KB Output is correct
108 Correct 2085 ms 61492 KB Output is correct
109 Correct 1997 ms 44628 KB Output is correct
110 Correct 1930 ms 53576 KB Output is correct
111 Correct 2005 ms 60568 KB Output is correct
112 Correct 1850 ms 44084 KB Output is correct
113 Correct 829 ms 79128 KB Output is correct
114 Correct 2150 ms 81352 KB Output is correct
115 Correct 2121 ms 64704 KB Output is correct
116 Correct 2126 ms 58212 KB Output is correct
117 Correct 2275 ms 45084 KB Output is correct
118 Correct 1959 ms 39280 KB Output is correct
119 Correct 1576 ms 35544 KB Output is correct
120 Correct 1652 ms 37124 KB Output is correct
121 Correct 1942 ms 36040 KB Output is correct
122 Correct 2037 ms 36748 KB Output is correct
123 Correct 2072 ms 37544 KB Output is correct
124 Correct 1985 ms 37284 KB Output is correct
125 Correct 2349 ms 36648 KB Output is correct
126 Correct 1900 ms 39484 KB Output is correct