답안 #676620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676620 2022-12-31T13:08:35 Z ymm 새 집 (APIO18_new_home) C++17
100 / 100
3373 ms 137240 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

#define int ll
#define pii pll

enum EventType {Rem, Add, Query};

struct Event {
	int time;
	EventType type;
	int store;
	int pos;
	int query_idx;
};

bool operator<(const Event &a, const Event &b) {
	return a.time < b.time || a.time == b.time && a.type < b.type;
}

vector<Event> evs;
vector<set<pii>> type_list;

struct Seg {
	int *t;
	int begin, end;
	
	void init(int b, int e) {
		t = new int[4 * (e-b)];
		fill(t, t + 4*(e-b), INT_MIN);
		begin = b;
		end = e;
	}
	void up_impl(int p, int x, int i, int b, int e) {
		if (e-b==1) {
			t[i] = x;
			return;
		}
		if (p < (b+e)/2)
			up_impl(p, x, 2*i+1, b, (b+e)/2);
		else
			up_impl(p, x, 2*i+2, (b+e)/2, e);
		t[i] = max(t[2*i+1], t[2*i+2]);
	}
	int get_impl(int l, int r, int i, int b, int e) {
		if (l <= b && e <= r)
			return t[i];
		if (r <= b || e <= l)
			return INT_MIN;
		return max(get_impl(l, r, 2*i+1, b, (b+e)/2),
		           get_impl(l, r, 2*i+2, (b+e)/2, e));
	}

	void up(int p, int x) { return up_impl(p, x, 0, begin, end); }
	int get(int l, int r) { return get_impl(l, r, 0, begin, end); }
} next_right;

int cnt_types, cnt_stores, cnt_queries;
vector<int> store_poses;

struct Store {
	int type;
	int pos;
	int b, e;
};
bool operator<(const Store &a, const Store &b) { return a.pos < b.pos; }
vector<Store> stores;

vector<int> ans;

void handle_add(Event ev)
{
	Store s = stores[ev.store];
	auto &list = type_list[s.type];
	auto it = list.upper_bound({s.pos, ev.store});
	int p = it == list.end()? INT_MAX: it->first;
	--it;
	next_right.up(it->second, s.pos);
	next_right.up(ev.store, p);
	list.insert({s.pos, ev.store});
}
void handle_rem(Event ev)
{
	Store s = stores[ev.store];
	auto &list = type_list[s.type];
	auto it = list.upper_bound({s.pos, ev.store});
	int p = it == list.end()? INT_MAX: it->first;
	--it; --it;
	next_right.up(it->second, p);
	next_right.up(ev.store, INT_MIN);
	list.erase({s.pos, ev.store});
}
void handle_query(Event ev)
{
	if (next_right.get(-cnt_types, 0) == INT_MAX) {
		ans[ev.query_idx] = -1;
		return;
	}
	int l = 0, r = INT_MAX;
	auto get = [&](int e) {
		e = lower_bound(store_poses.begin(), store_poses.end(), e) - store_poses.begin();
		return next_right.get(-cnt_stores, e);
	};
	while (l < r) {
		int m = (l + r)/2;
		int b = ev.pos - m, e = ev.pos + m + 1;
		if (get(b) < e)
			r = m;
		else
			l = m+1;
	}
	ans[ev.query_idx] = l;
}

signed main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> cnt_stores >> cnt_types >> cnt_queries;
	Loop (i,0,cnt_stores) {
		Store s;
		cin >> s.pos >> s.type >> s.b >> s.e;
		--s.pos; --s.b;
		stores.push_back(s);
		store_poses.push_back(s.pos);
	}
	sort(stores.begin(), stores.end());
	sort(store_poses.begin(), store_poses.end());
	Loop (i,0,cnt_stores) {
		evs.push_back({
			.time = stores[i].b,
			.type = Add,
			.store = i,
			.pos = stores[i].pos,
			.query_idx = -1,
		});
		evs.push_back({
			.time = stores[i].e,
			.type = Rem,
			.store = i,
			.pos = stores[i].pos,
			.query_idx = -1,
		});
	}
	Loop (i,0,cnt_queries) {
		int l, y;
		cin >> l >> y;
		--l; --y;
		evs.push_back({
			.time = y,
			.type = Query,
			.store = -1,
			.pos = l,
			.query_idx = i,
		});
	}
	sort(evs.begin(), evs.end());
	ans.resize(cnt_queries);
	type_list.resize(cnt_types+1);
	next_right.init(-cnt_types, cnt_stores);
	Loop (i,-cnt_types,0) {
		type_list[-i].insert({i, i});
		next_right.up(i, INT_MAX);
	}
	for (Event ev : evs) {
		switch (ev.type) {
		case Add:	handle_add(ev);		break;
		case Rem:	handle_rem(ev);		break;
		case Query:	handle_query(ev);	break;
		}
	}
	Loop (i,0,cnt_queries)
		cout << ans[i] << '\n';
}

Compilation message

new_home.cpp: In function 'bool operator<(const Event&, const Event&)':
new_home.cpp:23:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   23 |  return a.time < b.time || a.time == b.time && a.type < b.type;
      |                            ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 476 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 476 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 419 ms 16264 KB Output is correct
32 Correct 276 ms 14324 KB Output is correct
33 Correct 428 ms 14328 KB Output is correct
34 Correct 394 ms 14404 KB Output is correct
35 Correct 428 ms 16232 KB Output is correct
36 Correct 464 ms 16212 KB Output is correct
37 Correct 390 ms 14320 KB Output is correct
38 Correct 402 ms 14328 KB Output is correct
39 Correct 352 ms 14380 KB Output is correct
40 Correct 376 ms 14332 KB Output is correct
41 Correct 303 ms 14336 KB Output is correct
42 Correct 202 ms 14312 KB Output is correct
43 Correct 250 ms 16544 KB Output is correct
44 Correct 298 ms 14328 KB Output is correct
45 Correct 342 ms 14384 KB Output is correct
46 Correct 327 ms 14316 KB Output is correct
47 Correct 207 ms 14196 KB Output is correct
48 Correct 256 ms 14232 KB Output is correct
49 Correct 259 ms 14324 KB Output is correct
50 Correct 213 ms 14336 KB Output is correct
51 Correct 290 ms 14244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1737 ms 88980 KB Output is correct
2 Correct 2497 ms 79340 KB Output is correct
3 Correct 1338 ms 122788 KB Output is correct
4 Correct 1389 ms 94608 KB Output is correct
5 Correct 2528 ms 78864 KB Output is correct
6 Correct 2603 ms 79252 KB Output is correct
7 Correct 1334 ms 122824 KB Output is correct
8 Correct 1867 ms 94724 KB Output is correct
9 Correct 1961 ms 84772 KB Output is correct
10 Correct 2553 ms 79928 KB Output is correct
11 Correct 2029 ms 78240 KB Output is correct
12 Correct 2061 ms 79748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2303 ms 81068 KB Output is correct
2 Correct 1643 ms 78456 KB Output is correct
3 Correct 3042 ms 79532 KB Output is correct
4 Correct 902 ms 121004 KB Output is correct
5 Correct 917 ms 87264 KB Output is correct
6 Correct 900 ms 92896 KB Output is correct
7 Correct 3003 ms 78904 KB Output is correct
8 Correct 2956 ms 79332 KB Output is correct
9 Correct 1260 ms 122256 KB Output is correct
10 Correct 1706 ms 90564 KB Output is correct
11 Correct 2147 ms 82604 KB Output is correct
12 Correct 2978 ms 79988 KB Output is correct
13 Correct 1946 ms 77036 KB Output is correct
14 Correct 1952 ms 76216 KB Output is correct
15 Correct 2107 ms 78096 KB Output is correct
16 Correct 2221 ms 79320 KB Output is correct
17 Correct 2221 ms 77904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 476 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 419 ms 16264 KB Output is correct
32 Correct 276 ms 14324 KB Output is correct
33 Correct 428 ms 14328 KB Output is correct
34 Correct 394 ms 14404 KB Output is correct
35 Correct 428 ms 16232 KB Output is correct
36 Correct 464 ms 16212 KB Output is correct
37 Correct 390 ms 14320 KB Output is correct
38 Correct 402 ms 14328 KB Output is correct
39 Correct 352 ms 14380 KB Output is correct
40 Correct 376 ms 14332 KB Output is correct
41 Correct 303 ms 14336 KB Output is correct
42 Correct 202 ms 14312 KB Output is correct
43 Correct 250 ms 16544 KB Output is correct
44 Correct 298 ms 14328 KB Output is correct
45 Correct 342 ms 14384 KB Output is correct
46 Correct 327 ms 14316 KB Output is correct
47 Correct 207 ms 14196 KB Output is correct
48 Correct 256 ms 14232 KB Output is correct
49 Correct 259 ms 14324 KB Output is correct
50 Correct 213 ms 14336 KB Output is correct
51 Correct 290 ms 14244 KB Output is correct
52 Correct 158 ms 24412 KB Output is correct
53 Correct 151 ms 22080 KB Output is correct
54 Correct 155 ms 18792 KB Output is correct
55 Correct 314 ms 16832 KB Output is correct
56 Correct 302 ms 18656 KB Output is correct
57 Correct 304 ms 14328 KB Output is correct
58 Correct 254 ms 16532 KB Output is correct
59 Correct 226 ms 18608 KB Output is correct
60 Correct 230 ms 14328 KB Output is correct
61 Correct 135 ms 24792 KB Output is correct
62 Correct 200 ms 24640 KB Output is correct
63 Correct 220 ms 22696 KB Output is correct
64 Correct 237 ms 20796 KB Output is correct
65 Correct 267 ms 17288 KB Output is correct
66 Correct 301 ms 16628 KB Output is correct
67 Correct 190 ms 15480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 476 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 419 ms 16264 KB Output is correct
32 Correct 276 ms 14324 KB Output is correct
33 Correct 428 ms 14328 KB Output is correct
34 Correct 394 ms 14404 KB Output is correct
35 Correct 428 ms 16232 KB Output is correct
36 Correct 464 ms 16212 KB Output is correct
37 Correct 390 ms 14320 KB Output is correct
38 Correct 402 ms 14328 KB Output is correct
39 Correct 352 ms 14380 KB Output is correct
40 Correct 376 ms 14332 KB Output is correct
41 Correct 303 ms 14336 KB Output is correct
42 Correct 202 ms 14312 KB Output is correct
43 Correct 250 ms 16544 KB Output is correct
44 Correct 298 ms 14328 KB Output is correct
45 Correct 342 ms 14384 KB Output is correct
46 Correct 327 ms 14316 KB Output is correct
47 Correct 207 ms 14196 KB Output is correct
48 Correct 256 ms 14232 KB Output is correct
49 Correct 259 ms 14324 KB Output is correct
50 Correct 213 ms 14336 KB Output is correct
51 Correct 290 ms 14244 KB Output is correct
52 Correct 1737 ms 88980 KB Output is correct
53 Correct 2497 ms 79340 KB Output is correct
54 Correct 1338 ms 122788 KB Output is correct
55 Correct 1389 ms 94608 KB Output is correct
56 Correct 2528 ms 78864 KB Output is correct
57 Correct 2603 ms 79252 KB Output is correct
58 Correct 1334 ms 122824 KB Output is correct
59 Correct 1867 ms 94724 KB Output is correct
60 Correct 1961 ms 84772 KB Output is correct
61 Correct 2553 ms 79928 KB Output is correct
62 Correct 2029 ms 78240 KB Output is correct
63 Correct 2061 ms 79748 KB Output is correct
64 Correct 2303 ms 81068 KB Output is correct
65 Correct 1643 ms 78456 KB Output is correct
66 Correct 3042 ms 79532 KB Output is correct
67 Correct 902 ms 121004 KB Output is correct
68 Correct 917 ms 87264 KB Output is correct
69 Correct 900 ms 92896 KB Output is correct
70 Correct 3003 ms 78904 KB Output is correct
71 Correct 2956 ms 79332 KB Output is correct
72 Correct 1260 ms 122256 KB Output is correct
73 Correct 1706 ms 90564 KB Output is correct
74 Correct 2147 ms 82604 KB Output is correct
75 Correct 2978 ms 79988 KB Output is correct
76 Correct 1946 ms 77036 KB Output is correct
77 Correct 1952 ms 76216 KB Output is correct
78 Correct 2107 ms 78096 KB Output is correct
79 Correct 2221 ms 79320 KB Output is correct
80 Correct 2221 ms 77904 KB Output is correct
81 Correct 158 ms 24412 KB Output is correct
82 Correct 151 ms 22080 KB Output is correct
83 Correct 155 ms 18792 KB Output is correct
84 Correct 314 ms 16832 KB Output is correct
85 Correct 302 ms 18656 KB Output is correct
86 Correct 304 ms 14328 KB Output is correct
87 Correct 254 ms 16532 KB Output is correct
88 Correct 226 ms 18608 KB Output is correct
89 Correct 230 ms 14328 KB Output is correct
90 Correct 135 ms 24792 KB Output is correct
91 Correct 200 ms 24640 KB Output is correct
92 Correct 220 ms 22696 KB Output is correct
93 Correct 237 ms 20796 KB Output is correct
94 Correct 267 ms 17288 KB Output is correct
95 Correct 301 ms 16628 KB Output is correct
96 Correct 190 ms 15480 KB Output is correct
97 Correct 1018 ms 136644 KB Output is correct
98 Correct 1794 ms 72260 KB Output is correct
99 Correct 3281 ms 81892 KB Output is correct
100 Correct 988 ms 124736 KB Output is correct
101 Correct 997 ms 108268 KB Output is correct
102 Correct 3373 ms 93776 KB Output is correct
103 Correct 2547 ms 77016 KB Output is correct
104 Correct 2599 ms 76472 KB Output is correct
105 Correct 2170 ms 76596 KB Output is correct
106 Correct 2266 ms 76436 KB Output is correct
107 Correct 1877 ms 97560 KB Output is correct
108 Correct 1947 ms 107612 KB Output is correct
109 Correct 1803 ms 83284 KB Output is correct
110 Correct 1387 ms 96964 KB Output is correct
111 Correct 1361 ms 107012 KB Output is correct
112 Correct 1349 ms 82676 KB Output is correct
113 Correct 728 ms 135420 KB Output is correct
114 Correct 1162 ms 137240 KB Output is correct
115 Correct 1339 ms 112888 KB Output is correct
116 Correct 1499 ms 102824 KB Output is correct
117 Correct 1669 ms 85572 KB Output is correct
118 Correct 1793 ms 78456 KB Output is correct
119 Correct 1220 ms 74744 KB Output is correct
120 Correct 1212 ms 71888 KB Output is correct
121 Correct 1537 ms 74464 KB Output is correct
122 Correct 1678 ms 74056 KB Output is correct
123 Correct 1542 ms 75580 KB Output is correct
124 Correct 1319 ms 76860 KB Output is correct
125 Correct 1841 ms 75996 KB Output is correct
126 Correct 1196 ms 76968 KB Output is correct