답안 #676616

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676616 2022-12-31T12:58:00 Z ymm 새 집 (APIO18_new_home) C++17
45 / 100
2627 ms 92396 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;

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 = store_poses.back();
	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;
}

int 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:20:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   20 |  return a.time < b.time || a.time == b.time && a.type < b.type;
      |                            ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:134:13: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
  134 |    .store = i,
      |             ^
new_home.cpp:141:13: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
  141 |    .store = i,
      |             ^
new_home.cpp:155:17: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
  155 |    .query_idx = i,
      |                 ^
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 320 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 340 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 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 320 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 398 ms 12384 KB Output is correct
32 Correct 148 ms 8328 KB Output is correct
33 Correct 412 ms 10580 KB Output is correct
34 Correct 394 ms 10732 KB Output is correct
35 Correct 466 ms 12324 KB Output is correct
36 Correct 423 ms 12208 KB Output is correct
37 Correct 369 ms 9644 KB Output is correct
38 Correct 387 ms 9504 KB Output is correct
39 Correct 332 ms 9468 KB Output is correct
40 Correct 347 ms 9536 KB Output is correct
41 Correct 291 ms 9672 KB Output is correct
42 Correct 193 ms 9620 KB Output is correct
43 Correct 193 ms 11860 KB Output is correct
44 Correct 276 ms 9656 KB Output is correct
45 Correct 281 ms 9636 KB Output is correct
46 Correct 313 ms 9576 KB Output is correct
47 Correct 199 ms 9468 KB Output is correct
48 Correct 239 ms 9420 KB Output is correct
49 Correct 243 ms 9468 KB Output is correct
50 Correct 227 ms 9424 KB Output is correct
51 Correct 273 ms 9472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1577 ms 53096 KB Output is correct
2 Correct 2284 ms 45192 KB Output is correct
3 Correct 1200 ms 79448 KB Output is correct
4 Correct 1295 ms 57364 KB Output is correct
5 Correct 2259 ms 44792 KB Output is correct
6 Correct 2201 ms 45184 KB Output is correct
7 Correct 1170 ms 79320 KB Output is correct
8 Correct 1695 ms 57448 KB Output is correct
9 Correct 1744 ms 49688 KB Output is correct
10 Correct 2215 ms 45864 KB Output is correct
11 Correct 1822 ms 44704 KB Output is correct
12 Correct 1863 ms 45684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2032 ms 46744 KB Output is correct
2 Correct 787 ms 48564 KB Output is correct
3 Correct 2627 ms 57680 KB Output is correct
4 Correct 812 ms 91124 KB Output is correct
5 Correct 851 ms 64532 KB Output is correct
6 Correct 808 ms 68936 KB Output is correct
7 Correct 2571 ms 57152 KB Output is correct
8 Correct 2616 ms 57520 KB Output is correct
9 Correct 1132 ms 92396 KB Output is correct
10 Correct 1550 ms 67536 KB Output is correct
11 Correct 1941 ms 61180 KB Output is correct
12 Correct 2618 ms 58436 KB Output is correct
13 Correct 1718 ms 56040 KB Output is correct
14 Correct 1707 ms 55344 KB Output is correct
15 Correct 1903 ms 56848 KB Output is correct
16 Correct 1983 ms 58288 KB Output is correct
17 Correct 1997 ms 56536 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 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 320 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 398 ms 12384 KB Output is correct
32 Correct 148 ms 8328 KB Output is correct
33 Correct 412 ms 10580 KB Output is correct
34 Correct 394 ms 10732 KB Output is correct
35 Correct 466 ms 12324 KB Output is correct
36 Correct 423 ms 12208 KB Output is correct
37 Correct 369 ms 9644 KB Output is correct
38 Correct 387 ms 9504 KB Output is correct
39 Correct 332 ms 9468 KB Output is correct
40 Correct 347 ms 9536 KB Output is correct
41 Correct 291 ms 9672 KB Output is correct
42 Correct 193 ms 9620 KB Output is correct
43 Correct 193 ms 11860 KB Output is correct
44 Correct 276 ms 9656 KB Output is correct
45 Correct 281 ms 9636 KB Output is correct
46 Correct 313 ms 9576 KB Output is correct
47 Correct 199 ms 9468 KB Output is correct
48 Correct 239 ms 9420 KB Output is correct
49 Correct 243 ms 9468 KB Output is correct
50 Correct 227 ms 9424 KB Output is correct
51 Correct 273 ms 9472 KB Output is correct
52 Correct 136 ms 18836 KB Output is correct
53 Correct 133 ms 17012 KB Output is correct
54 Correct 137 ms 14436 KB Output is correct
55 Correct 280 ms 12996 KB Output is correct
56 Correct 273 ms 14452 KB Output is correct
57 Correct 273 ms 10616 KB Output is correct
58 Correct 219 ms 12832 KB Output is correct
59 Correct 203 ms 14264 KB Output is correct
60 Correct 209 ms 10608 KB Output is correct
61 Correct 108 ms 18536 KB Output is correct
62 Incorrect 161 ms 18920 KB Output isn't correct
63 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 320 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 398 ms 12384 KB Output is correct
32 Correct 148 ms 8328 KB Output is correct
33 Correct 412 ms 10580 KB Output is correct
34 Correct 394 ms 10732 KB Output is correct
35 Correct 466 ms 12324 KB Output is correct
36 Correct 423 ms 12208 KB Output is correct
37 Correct 369 ms 9644 KB Output is correct
38 Correct 387 ms 9504 KB Output is correct
39 Correct 332 ms 9468 KB Output is correct
40 Correct 347 ms 9536 KB Output is correct
41 Correct 291 ms 9672 KB Output is correct
42 Correct 193 ms 9620 KB Output is correct
43 Correct 193 ms 11860 KB Output is correct
44 Correct 276 ms 9656 KB Output is correct
45 Correct 281 ms 9636 KB Output is correct
46 Correct 313 ms 9576 KB Output is correct
47 Correct 199 ms 9468 KB Output is correct
48 Correct 239 ms 9420 KB Output is correct
49 Correct 243 ms 9468 KB Output is correct
50 Correct 227 ms 9424 KB Output is correct
51 Correct 273 ms 9472 KB Output is correct
52 Correct 1577 ms 53096 KB Output is correct
53 Correct 2284 ms 45192 KB Output is correct
54 Correct 1200 ms 79448 KB Output is correct
55 Correct 1295 ms 57364 KB Output is correct
56 Correct 2259 ms 44792 KB Output is correct
57 Correct 2201 ms 45184 KB Output is correct
58 Correct 1170 ms 79320 KB Output is correct
59 Correct 1695 ms 57448 KB Output is correct
60 Correct 1744 ms 49688 KB Output is correct
61 Correct 2215 ms 45864 KB Output is correct
62 Correct 1822 ms 44704 KB Output is correct
63 Correct 1863 ms 45684 KB Output is correct
64 Correct 2032 ms 46744 KB Output is correct
65 Correct 787 ms 48564 KB Output is correct
66 Correct 2627 ms 57680 KB Output is correct
67 Correct 812 ms 91124 KB Output is correct
68 Correct 851 ms 64532 KB Output is correct
69 Correct 808 ms 68936 KB Output is correct
70 Correct 2571 ms 57152 KB Output is correct
71 Correct 2616 ms 57520 KB Output is correct
72 Correct 1132 ms 92396 KB Output is correct
73 Correct 1550 ms 67536 KB Output is correct
74 Correct 1941 ms 61180 KB Output is correct
75 Correct 2618 ms 58436 KB Output is correct
76 Correct 1718 ms 56040 KB Output is correct
77 Correct 1707 ms 55344 KB Output is correct
78 Correct 1903 ms 56848 KB Output is correct
79 Correct 1983 ms 58288 KB Output is correct
80 Correct 1997 ms 56536 KB Output is correct
81 Correct 136 ms 18836 KB Output is correct
82 Correct 133 ms 17012 KB Output is correct
83 Correct 137 ms 14436 KB Output is correct
84 Correct 280 ms 12996 KB Output is correct
85 Correct 273 ms 14452 KB Output is correct
86 Correct 273 ms 10616 KB Output is correct
87 Correct 219 ms 12832 KB Output is correct
88 Correct 203 ms 14264 KB Output is correct
89 Correct 209 ms 10608 KB Output is correct
90 Correct 108 ms 18536 KB Output is correct
91 Incorrect 161 ms 18920 KB Output isn't correct
92 Halted 0 ms 0 KB -