답안 #801947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
801947 2023-08-02T08:38:09 Z GEN 이지후(#10129) Cultivation (JOI17_cultivation) C++17
100 / 100
1999 ms 984 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXT = 1050;

vector<lint> cy;

struct node {
	int cmin, cmax, gap;
	node() {
		cmin = 2e9;
		cmax = -2e9;
		gap = 0;
	}
	node(int pos) {
		cmin = cmax = pos;
		gap = 0;
	}
	node(int fuck1, int fuck2, int fuck3) {
		cmin = fuck1;
		cmax = fuck2;
		gap = fuck3;
	}
	node operator+(const node &nd) {
		if (cmax < 0)
			return nd;
		if (nd.cmax < 0)
			return *this;
		return node(cmin, nd.cmax, max({gap, nd.gap, nd.cmin - cmax - 1}));
	}
};

struct seg {
	node tree[MAXT];
	int lim;
	void init(int sz) {
		for (lim = 1; lim <= sz; lim <<= 1)
			;
		fill(tree, tree + MAXT, node());
	}
	void upd(int x, int cnt) {
		if (cnt == 0)
			tree[x + lim] = node();
		else
			tree[x + lim] = node(cy[x]);
		x += lim;
		while (x > 1) {
			x >>= 1;
			tree[x] = tree[2 * x] + tree[2 * x + 1];
		}
	}
} seg;

int cnt[696];
pi dq[3][696];
int fr[3], ed[3];

lint sumFixed(lint r, lint c, lint g, vector<pi> &a) {
	vector<array<lint, 3>> e1, e2, event;
	e1.push_back({0, -1, 0});
	for (auto &[x, y] : a) {
		e1.push_back({x, y, +1});
		e2.push_back({x + 1 + g, y, -1});
	}
	e2.push_back({r + g, -1, 0});
	event.resize(sz(e1) + sz(e2));
	merge(all(e1), all(e2), event.begin());
	vector<array<lint, 5>> chop;
	for (int i = 0; i < sz(event);) {
		int j = i;
		while (j < sz(event) && event[i][0] == event[j][0]) {
			if (event[j][2]) {
				bool prv = (cnt[event[j][1]] > 0);
				cnt[event[j][1]] += event[j][2];
				bool cur = (cnt[event[j][1]] > 0);
				if (prv && !cur) {
					seg.upd(event[j][1], cnt[event[j][1]]);
				} else if (!prv && cur) {
					seg.upd(event[j][1], cnt[event[j][1]]);
				}
			}
			j++;
		}
		if (j == sz(event))
			break;
		lint lmax = seg.tree[1].cmin, rmax = c - 1 - seg.tree[1].cmax, lrmax = seg.tree[1].gap;
		chop.push_back({event[i][0], event[j][0], lmax, rmax, lrmax});
		i = j;
	}
	int k = 0;
	memset(fr, 0, sizeof(fr));
	memset(ed, 0, sizeof(ed));
	lint ans = 1e18;
	for (int i = 0; i < sz(a); i++) {
		if (a[i][0] > g)
			break;
		while (k < sz(chop) && chop[k][0] < a[i][0] + r) {
			for (int t = 0; t < 3; t++) {
				pi val = {chop[k][t + 2], chop[k][1]};
				while (fr[t] < ed[t] && dq[t][ed[t] - 1][0] <= val[0])
					ed[t]--;
				dq[t][ed[t]++] = val;
			}
			k++;
		}
		lint dap[3] = {0, 0, 0};
		for (int t = 0; t < 3; t++) {
			while (fr[t] < ed[t] && dq[t][fr[t]][1] <= a[i][0])
				fr[t]++;
			dap[t] = dq[t][fr[t]][0];
		}
		ans = min(ans, max(dap[0] + dap[1], dap[2]));
	}
	return ans;
}

map<pi, lint> mp;
// abandon
lint solve(lint r, lint c, lint dl, lint dr, vector<pi> &a, lint jasal) {
	if (mp.count({dl, dr}))
		return mp[pi{dl, dr}];
	vector<array<lint, 3>> e1, e2, event;
	for (auto &[x, y] : a) {
		int l = max(0ll, x - dl);
		int e = min(r * 1ll, x + 1 + dr);
		e1.push_back({l, y, +1});
		e2.push_back({e, y, -1});
	}
	event.resize(sz(e1) + sz(e2));
	merge(all(e1), all(e2), event.begin());
	if (event[0][0] != 0 || event.back()[0] != r)
		return mp[pi{dl, dr}] = 1e18;

	lint lmax = 0, rmax = 0, lrmax = 0;
	memset(cnt, 0, sizeof(cnt));
	seg.init(sz(cy));
	for (int i = 0; i < sz(event);) {
		int j = i;
		while (j < sz(event) && event[i][0] == event[j][0]) {
			if (event[j][2]) {
				bool prv = (cnt[event[j][1]] > 0);
				cnt[event[j][1]] += event[j][2];
				bool cur = (cnt[event[j][1]] > 0);
				if (prv && !cur) {
					seg.upd(event[j][1], cnt[event[j][1]]);
				} else if (!prv && cur) {
					seg.upd(event[j][1], cnt[event[j][1]]);
				}
			}
			j++;
		}
		if (j == sz(event))
			break;
		lmax = max(lmax, 1ll * seg.tree[1].cmin);
		rmax = max(rmax, c - 1 - seg.tree[1].cmax);
		lrmax = max(lrmax, 1ll * seg.tree[1].gap);
		if (lmax + rmax >= jasal || lrmax >= jasal)
			return 1e18;
		i = j;
	}
	return mp[pi{dl, dr}] = max(lmax + rmax, lrmax);
}

// n^3 log n

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, r, c;
	cin >> r >> c >> n;
	vector<pi> a(n);
	for (auto &[x, y] : a)
		cin >> x >> y, x--, y--;
	sort(all(a));
	lint ans = 2ll * (r + c);
	vector<lint> gaps = {0};
	for (int i = 0; i < n; i++) {
		cy.push_back(a[i][1]);
		for (int j = i + 1; j < n; j++) {
			if (a[j][0] > a[i][0])
				gaps.push_back(a[j][0] - a[i][0] - 1);
		}
	}
	sort(all(cy));
	cy.resize(unique(all(cy)) - cy.begin());
	for (int i = 0; i < n; i++) {
		a[i][1] = lower_bound(all(cy), a[i][1]) - cy.begin();
	}
	sort(all(gaps));
	gaps.resize(unique(all(gaps)) - gaps.begin());
	seg.init(sz(cy));
	for (auto &dy : gaps) {
		if (ans > dy) {
			ans = min(ans, sumFixed(r, c, dy, a) + dy);
			if (clock() > 0.9 * CLOCKS_PER_SEC)
				break;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = n - 1; j >= 0; j--) {
			if (a[i][0] + r - 1 - a[j][0] >= ans)
				break;
			lint jsp = ans - (a[i][0] + r - 1 - a[j][0]);
			ans = min(ans, a[i][0] + r - 1 - a[j][0] + solve(r, c, a[i][0], r - 1 - a[j][0], a, jsp));
		}
		if (clock() > 1.995 * CLOCKS_PER_SEC)
			break;
	}
	cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 6 ms 340 KB Output is correct
22 Correct 8 ms 468 KB Output is correct
23 Correct 3 ms 332 KB Output is correct
24 Correct 23 ms 724 KB Output is correct
25 Correct 12 ms 564 KB Output is correct
26 Correct 75 ms 984 KB Output is correct
27 Correct 71 ms 984 KB Output is correct
28 Correct 27 ms 724 KB Output is correct
29 Correct 32 ms 984 KB Output is correct
30 Correct 32 ms 984 KB Output is correct
31 Correct 22 ms 984 KB Output is correct
32 Correct 32 ms 976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 6 ms 340 KB Output is correct
22 Correct 8 ms 468 KB Output is correct
23 Correct 3 ms 332 KB Output is correct
24 Correct 23 ms 724 KB Output is correct
25 Correct 12 ms 564 KB Output is correct
26 Correct 75 ms 984 KB Output is correct
27 Correct 71 ms 984 KB Output is correct
28 Correct 27 ms 724 KB Output is correct
29 Correct 32 ms 984 KB Output is correct
30 Correct 32 ms 984 KB Output is correct
31 Correct 22 ms 984 KB Output is correct
32 Correct 32 ms 976 KB Output is correct
33 Correct 1052 ms 724 KB Output is correct
34 Correct 1481 ms 980 KB Output is correct
35 Correct 1246 ms 984 KB Output is correct
36 Correct 1389 ms 984 KB Output is correct
37 Correct 1434 ms 984 KB Output is correct
38 Correct 1478 ms 984 KB Output is correct
39 Correct 1345 ms 984 KB Output is correct
40 Correct 1319 ms 976 KB Output is correct
41 Correct 1468 ms 724 KB Output is correct
42 Correct 1245 ms 984 KB Output is correct
43 Correct 1392 ms 984 KB Output is correct
44 Correct 1145 ms 984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 4 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 4 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 18 ms 452 KB Output is correct
22 Correct 45 ms 340 KB Output is correct
23 Correct 110 ms 424 KB Output is correct
24 Correct 101 ms 440 KB Output is correct
25 Correct 55 ms 436 KB Output is correct
26 Correct 40 ms 436 KB Output is correct
27 Correct 142 ms 432 KB Output is correct
28 Correct 51 ms 440 KB Output is correct
29 Correct 42 ms 340 KB Output is correct
30 Correct 60 ms 336 KB Output is correct
31 Correct 46 ms 432 KB Output is correct
32 Correct 52 ms 440 KB Output is correct
33 Correct 135 ms 452 KB Output is correct
34 Correct 105 ms 436 KB Output is correct
35 Correct 99 ms 340 KB Output is correct
36 Correct 62 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 6 ms 340 KB Output is correct
22 Correct 8 ms 468 KB Output is correct
23 Correct 3 ms 332 KB Output is correct
24 Correct 23 ms 724 KB Output is correct
25 Correct 12 ms 564 KB Output is correct
26 Correct 75 ms 984 KB Output is correct
27 Correct 71 ms 984 KB Output is correct
28 Correct 27 ms 724 KB Output is correct
29 Correct 32 ms 984 KB Output is correct
30 Correct 32 ms 984 KB Output is correct
31 Correct 22 ms 984 KB Output is correct
32 Correct 32 ms 976 KB Output is correct
33 Correct 1052 ms 724 KB Output is correct
34 Correct 1481 ms 980 KB Output is correct
35 Correct 1246 ms 984 KB Output is correct
36 Correct 1389 ms 984 KB Output is correct
37 Correct 1434 ms 984 KB Output is correct
38 Correct 1478 ms 984 KB Output is correct
39 Correct 1345 ms 984 KB Output is correct
40 Correct 1319 ms 976 KB Output is correct
41 Correct 1468 ms 724 KB Output is correct
42 Correct 1245 ms 984 KB Output is correct
43 Correct 1392 ms 984 KB Output is correct
44 Correct 1145 ms 984 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 4 ms 340 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 3 ms 336 KB Output is correct
49 Correct 4 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 336 KB Output is correct
53 Correct 2 ms 336 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 4 ms 340 KB Output is correct
56 Correct 2 ms 332 KB Output is correct
57 Correct 2 ms 340 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 4 ms 340 KB Output is correct
60 Correct 2 ms 340 KB Output is correct
61 Correct 2 ms 340 KB Output is correct
62 Correct 3 ms 340 KB Output is correct
63 Correct 3 ms 340 KB Output is correct
64 Correct 2 ms 340 KB Output is correct
65 Correct 18 ms 452 KB Output is correct
66 Correct 45 ms 340 KB Output is correct
67 Correct 110 ms 424 KB Output is correct
68 Correct 101 ms 440 KB Output is correct
69 Correct 55 ms 436 KB Output is correct
70 Correct 40 ms 436 KB Output is correct
71 Correct 142 ms 432 KB Output is correct
72 Correct 51 ms 440 KB Output is correct
73 Correct 42 ms 340 KB Output is correct
74 Correct 60 ms 336 KB Output is correct
75 Correct 46 ms 432 KB Output is correct
76 Correct 52 ms 440 KB Output is correct
77 Correct 135 ms 452 KB Output is correct
78 Correct 105 ms 436 KB Output is correct
79 Correct 99 ms 340 KB Output is correct
80 Correct 62 ms 440 KB Output is correct
81 Correct 416 ms 724 KB Output is correct
82 Correct 432 ms 724 KB Output is correct
83 Correct 292 ms 728 KB Output is correct
84 Correct 1783 ms 740 KB Output is correct
85 Correct 826 ms 984 KB Output is correct
86 Correct 588 ms 984 KB Output is correct
87 Correct 879 ms 984 KB Output is correct
88 Correct 1999 ms 984 KB Output is correct
89 Correct 1696 ms 984 KB Output is correct
90 Correct 1866 ms 984 KB Output is correct
91 Correct 1014 ms 984 KB Output is correct
92 Correct 982 ms 984 KB Output is correct
93 Correct 1917 ms 984 KB Output is correct
94 Correct 1997 ms 984 KB Output is correct
95 Correct 996 ms 984 KB Output is correct
96 Correct 1093 ms 984 KB Output is correct
97 Correct 1570 ms 984 KB Output is correct
98 Correct 1449 ms 984 KB Output is correct