답안 #308286

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
308286 2020-09-30T19:55:37 Z cki86201 함박 스테이크 (JOI20_hamburg) C++17
100 / 100
640 ms 47860 KB
#include <bits/stdc++.h>
using namespace std;

#define Fi first
#define Se second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<(n);i++)
typedef tuple<int, int, int> t3;

const int INF = 1e9 + 10, M = 4e5 + 5;
pii operator+(pii a, pii b) { return {max(a.Fi, b.Fi), min(a.Se, b.Se)}; }
int N, K;
vector <int> vx, vy;
struct rect {
	int x1, y1, x2, y2;
	bool In(int x, int y) {
		return x1 <= x && x <= x2 && y1 <= y && y <= y2;
	}
} R[200020];

void trial(vector <pii> ans, int flp = 0) {
	if(szz(ans) == 0 || szz(ans) > K) return;
	while(szz(ans) < K) ans.pb(ans[0]);
	for(int i=1;i<=N;i++) {
		int r = 0;
		for(auto &[x, y] : ans) if(R[i].In(x, y)) {
			r = 1; break;
		}
		if(r == 0) return;
	}
	for(auto [x, y] : ans) {
		if(flp) y = szz(vy) + 1 - y;
		printf("%d %d\n", vx[x-1], vy[y-1]);
	}
	exit(0);
}

int arr[4*M+5];
vector <int> solve_linear(vector <pii> R) {
	for(int i=1;i<=M;i++) arr[i] = INF;
	for(auto [x, y] : R) arr[x] = min(arr[x], y);
	for(int i=M-1;i;i--) arr[i] = min(arr[i], arr[i+1]);
	vector <int> res;
	for(int t=1;arr[t]!=INF;t=arr[t]+1) res.pb(arr[t]);
	return res;
}

pii seg[6][2][M+5];

void solve(vector <int> rst, int k, vector <pii> ans) {
	if(szz(rst) == 0) trial(ans);
	if(k == 0) return;
	int lx = INF, rx = -INF, ly = INF, ry = -INF;
	for(int i : rst) {
		lx = min(lx, R[i].x2);
		rx = max(rx, R[i].x1);
		ly = min(ly, R[i].y2);
		ry = max(ry, R[i].y1);
	}
	if(k == 1) {
		if(lx >= rx && ly >= ry) {
			ans.pb({rx, ry});
			trial(ans);
		}
		return;
	}
	if(lx >= rx) {
		vector <pii> V;
		for(int i : rst) V.pb({R[i].y1, R[i].y2});
		auto ys = solve_linear(V);
		for(int y : ys) ans.pb({rx, y});
		trial(ans);
		return;
	}
	if(ly >= ry) {
		vector <pii> V;
		for(int i : rst) V.pb({R[i].x1, R[i].x2});
		auto xs = solve_linear(V);
		for(int x : xs) ans.pb({x, ry});
		trial(ans);
		return;
	}
	vector <pii> vert;
	for(int x : {lx, rx}) for(int y : {ly, ry}) vert.pb({x, y});
	for(auto [x, y] : vert) {
		vector <int> nst;
		for(int i : rst) if(!R[i].In(x, y)) nst.pb(i);
		auto ans2 = ans; ans2.pb({x, y});
		solve(nst, k - 1, ans2);
	}

	if(k != 4) return;
	auto fy = [&](int &y) { y = szz(vy) + 1 - y; };
	auto Intersect = [&](const rect &a, const rect &b) {
		pii x = pii(a.x1, a.x2) + pii(b.x1, b.x2);
		if(x.Fi > x.Se) return 0;
		pii y = pii(a.y1, a.y2) + pii(b.y1, b.y2);
		if(y.Fi > y.Se) return 0;
		return 1;
	};
	auto solve4 = [&](int flp) {
		rect Side[4];
		Side[0] = {lx, ly, lx, ry};
		Side[1] = {lx, ry, rx, ry};
		Side[2] = {rx, ly, rx, ry};
		Side[3] = {lx, ly, rx, ly};
		auto type = [&](int t) {
			int res = 0;
			rep(i, 4) if(Intersect(R[t], Side[i])) res |= 1<<i;
			return res;
		};
		pii Seg[4];
		Seg[0] = Seg[2] = {ly + 1, ry - 1};
		Seg[1] = Seg[3] = {lx + 1, rx - 1};
		auto upd = [&](pii &a, pii b) { a = a + b; };
		rep(i, 6) rep(j, 2) rep(k, M+1) seg[i][j][k] = pii(-INF, INF);
		int nrx = INF;
		for(int i=1;i<=N;i++) {
			int t = type(i);
			pii wx = {R[i].x1, R[i].x2}, wy = {R[i].y1, R[i].y2};
			if(t == 1) upd(Seg[0], wy);
			else if(t == 2) upd(Seg[1], wx);
			else if(t == 4) upd(Seg[2], wy);
			else if(t == 8) upd(Seg[3], wx);
			else if(t == 3) upd(seg[0][0][R[i].y1-1], wx);
			else if(t == 5) {
				upd(seg[1][0][R[i].y1-1], wy);
				upd(seg[1][1][R[i].y2+1], wy);
			}
			else if(t == 9) upd(seg[2][1][R[i].y2+1], wx);
			else if(t == 6) upd(seg[3][0][R[i].x1-1], wy);
			else if(t == 10) {
				upd(seg[4][0][R[i].x1-1], wx);
				upd(seg[4][1][R[i].x2+1], wx);
				nrx = min(nrx, R[i].x2);
			}
			else if(t == 12) upd(seg[5][1][R[i].y2+1], wx);
		}
		rep(i, 6) {
			for(int j=M-1;j;j--) upd(seg[i][0][j], seg[i][0][j+1]);
			for(int j=2;j<=M;j++) upd(seg[i][1][j], seg[i][1][j-1]);
			for(int j=1;j<=M;j++) upd(seg[i][0][j], seg[i][1][j]);
		}
		vector <pii> ans(4);
		for(int i=Seg[0].Fi;i<=Seg[0].Se;i++) {
			ans[0] = {lx, i};
			pii NS[3];
			rep(j, 3) NS[j] = Seg[j+1] + seg[j][0][i];
			int ri = min({nrx, NS[0].Se, NS[2].Se});
			if(NS[0].Fi > ri) continue;
			ans[1] = {ri, ry};
			upd(NS[1], seg[3][0][ri]);
			upd(NS[2], seg[4][0][ri]);
			if(NS[1].Fi > NS[1].Se) continue;
			int rj = NS[1].Fi;
			ans[2] = {rx, rj};
			upd(NS[2], seg[5][0][rj]);
			if(NS[2].Fi > NS[2].Se) continue;
			ans[3] = {NS[2].Se, ly};
			trial(ans, flp);
		}
	};

	solve4(0);
	for(int i=1;i<=N;i++) fy(R[i].y1), fy(R[i].y2), swap(R[i].y1, R[i].y2);
	swap(ly, ry); fy(ly), fy(ry);
	solve4(1);
}

int main() {
	scanf("%d%d", &N, &K);
	for(int i=1;i<=N;i++) {
		scanf("%d%d%d%d", &R[i].x1, &R[i].y1, &R[i].x2, &R[i].y2);
		vx.pb(R[i].x1), vx.pb(R[i].x2);
		vy.pb(R[i].y1), vy.pb(R[i].y2);
	}
	sort(all(vx)); vx.resize(unique(all(vx)) - vx.begin());
	sort(all(vy)); vy.resize(unique(all(vy)) - vy.begin());
	for(int i=1;i<=N;i++) {
		auto &[x1, y1, x2, y2] = R[i];
		x1 = (int)(lower_bound(all(vx), x1) - vx.begin()) + 1;
		y1 = (int)(lower_bound(all(vy), y1) - vy.begin()) + 1;
		x2 = (int)(lower_bound(all(vx), x2) - vx.begin()) + 1;
		y2 = (int)(lower_bound(all(vy), y2) - vy.begin()) + 1;
	}
	vector <int> rst;
	for(int i=1;i<=N;i++) rst.pb(i);
	solve(rst, K, {});
	return 0;
}

Compilation message

hamburg.cpp: In function 'int main()':
hamburg.cpp:176:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  176 |  scanf("%d%d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~
hamburg.cpp:178:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  178 |   scanf("%d%d%d%d", &R[i].x1, &R[i].y1, &R[i].x2, &R[i].y2);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2048 KB Output is correct
2 Correct 6 ms 2048 KB Output is correct
3 Correct 6 ms 2048 KB Output is correct
4 Correct 6 ms 2048 KB Output is correct
5 Correct 6 ms 2048 KB Output is correct
6 Correct 6 ms 2048 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2048 KB Output is correct
2 Correct 6 ms 2048 KB Output is correct
3 Correct 5 ms 2048 KB Output is correct
4 Correct 6 ms 2048 KB Output is correct
5 Correct 6 ms 2048 KB Output is correct
6 Correct 6 ms 2048 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 81 ms 38008 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 52 ms 38008 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 82 ms 38012 KB Output is correct
21 Correct 5 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 52 ms 38144 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 4 ms 512 KB Output is correct
28 Correct 4 ms 384 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 51 ms 38008 KB Output is correct
32 Correct 81 ms 38020 KB Output is correct
33 Correct 52 ms 38016 KB Output is correct
34 Correct 80 ms 38016 KB Output is correct
35 Correct 51 ms 38008 KB Output is correct
36 Correct 79 ms 38016 KB Output is correct
37 Correct 51 ms 38016 KB Output is correct
38 Correct 81 ms 38016 KB Output is correct
39 Correct 52 ms 38016 KB Output is correct
40 Correct 80 ms 38016 KB Output is correct
41 Correct 52 ms 38016 KB Output is correct
42 Correct 82 ms 38136 KB Output is correct
43 Correct 52 ms 38016 KB Output is correct
44 Correct 80 ms 38016 KB Output is correct
45 Correct 4 ms 512 KB Output is correct
46 Correct 81 ms 38016 KB Output is correct
47 Correct 51 ms 38008 KB Output is correct
48 Correct 83 ms 38008 KB Output is correct
49 Correct 51 ms 38016 KB Output is correct
50 Correct 80 ms 38008 KB Output is correct
51 Correct 52 ms 38012 KB Output is correct
52 Correct 81 ms 38080 KB Output is correct
53 Correct 51 ms 38016 KB Output is correct
54 Correct 80 ms 38136 KB Output is correct
55 Correct 52 ms 38008 KB Output is correct
56 Correct 51 ms 38008 KB Output is correct
57 Correct 51 ms 38016 KB Output is correct
58 Correct 52 ms 38008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 353 ms 8400 KB Output is correct
6 Correct 413 ms 8408 KB Output is correct
7 Correct 358 ms 8404 KB Output is correct
8 Correct 357 ms 8408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 366 ms 9044 KB Output is correct
6 Correct 364 ms 9052 KB Output is correct
7 Correct 359 ms 9052 KB Output is correct
8 Correct 366 ms 10048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2048 KB Output is correct
2 Correct 6 ms 2048 KB Output is correct
3 Correct 6 ms 2048 KB Output is correct
4 Correct 6 ms 2048 KB Output is correct
5 Correct 6 ms 2048 KB Output is correct
6 Correct 6 ms 2048 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 372 ms 13528 KB Output is correct
14 Correct 366 ms 13500 KB Output is correct
15 Correct 363 ms 13524 KB Output is correct
16 Correct 366 ms 13528 KB Output is correct
17 Correct 363 ms 13532 KB Output is correct
18 Correct 362 ms 13528 KB Output is correct
19 Correct 362 ms 9808 KB Output is correct
20 Correct 412 ms 9956 KB Output is correct
21 Correct 391 ms 11712 KB Output is correct
22 Correct 374 ms 10440 KB Output is correct
23 Correct 373 ms 11220 KB Output is correct
24 Correct 371 ms 11472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2048 KB Output is correct
2 Correct 6 ms 2048 KB Output is correct
3 Correct 5 ms 2048 KB Output is correct
4 Correct 6 ms 2048 KB Output is correct
5 Correct 6 ms 2048 KB Output is correct
6 Correct 6 ms 2048 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 81 ms 38008 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 52 ms 38008 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 82 ms 38012 KB Output is correct
21 Correct 5 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 52 ms 38144 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 4 ms 512 KB Output is correct
28 Correct 4 ms 384 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 51 ms 38008 KB Output is correct
32 Correct 81 ms 38020 KB Output is correct
33 Correct 52 ms 38016 KB Output is correct
34 Correct 80 ms 38016 KB Output is correct
35 Correct 51 ms 38008 KB Output is correct
36 Correct 79 ms 38016 KB Output is correct
37 Correct 51 ms 38016 KB Output is correct
38 Correct 81 ms 38016 KB Output is correct
39 Correct 52 ms 38016 KB Output is correct
40 Correct 80 ms 38016 KB Output is correct
41 Correct 52 ms 38016 KB Output is correct
42 Correct 82 ms 38136 KB Output is correct
43 Correct 52 ms 38016 KB Output is correct
44 Correct 80 ms 38016 KB Output is correct
45 Correct 4 ms 512 KB Output is correct
46 Correct 81 ms 38016 KB Output is correct
47 Correct 51 ms 38008 KB Output is correct
48 Correct 83 ms 38008 KB Output is correct
49 Correct 51 ms 38016 KB Output is correct
50 Correct 80 ms 38008 KB Output is correct
51 Correct 52 ms 38012 KB Output is correct
52 Correct 81 ms 38080 KB Output is correct
53 Correct 51 ms 38016 KB Output is correct
54 Correct 80 ms 38136 KB Output is correct
55 Correct 52 ms 38008 KB Output is correct
56 Correct 51 ms 38008 KB Output is correct
57 Correct 51 ms 38016 KB Output is correct
58 Correct 52 ms 38008 KB Output is correct
59 Correct 367 ms 13400 KB Output is correct
60 Correct 368 ms 13652 KB Output is correct
61 Correct 360 ms 13684 KB Output is correct
62 Correct 366 ms 13536 KB Output is correct
63 Correct 365 ms 13536 KB Output is correct
64 Correct 360 ms 13392 KB Output is correct
65 Correct 360 ms 9820 KB Output is correct
66 Correct 370 ms 9812 KB Output is correct
67 Correct 388 ms 12288 KB Output is correct
68 Correct 370 ms 11216 KB Output is correct
69 Correct 363 ms 10408 KB Output is correct
70 Correct 376 ms 11992 KB Output is correct
71 Correct 530 ms 13236 KB Output is correct
72 Correct 620 ms 46268 KB Output is correct
73 Correct 491 ms 13096 KB Output is correct
74 Correct 452 ms 13084 KB Output is correct
75 Correct 519 ms 46968 KB Output is correct
76 Correct 439 ms 12972 KB Output is correct
77 Correct 471 ms 13220 KB Output is correct
78 Correct 640 ms 46052 KB Output is correct
79 Correct 489 ms 12956 KB Output is correct
80 Correct 459 ms 13156 KB Output is correct
81 Correct 578 ms 46124 KB Output is correct
82 Correct 432 ms 13144 KB Output is correct
83 Correct 402 ms 12888 KB Output is correct
84 Correct 399 ms 11344 KB Output is correct
85 Correct 440 ms 13312 KB Output is correct
86 Correct 406 ms 12768 KB Output is correct
87 Correct 418 ms 13008 KB Output is correct
88 Correct 437 ms 13016 KB Output is correct
89 Correct 529 ms 46952 KB Output is correct
90 Correct 634 ms 46092 KB Output is correct
91 Correct 533 ms 47016 KB Output is correct
92 Correct 613 ms 47468 KB Output is correct
93 Correct 563 ms 46208 KB Output is correct
94 Correct 630 ms 46144 KB Output is correct
95 Correct 572 ms 46364 KB Output is correct
96 Correct 591 ms 47772 KB Output is correct
97 Correct 559 ms 47728 KB Output is correct
98 Correct 596 ms 46084 KB Output is correct
99 Correct 519 ms 46180 KB Output is correct
100 Correct 637 ms 47752 KB Output is correct
101 Correct 572 ms 46356 KB Output is correct
102 Correct 545 ms 47216 KB Output is correct
103 Correct 614 ms 47832 KB Output is correct
104 Correct 562 ms 47628 KB Output is correct
105 Correct 592 ms 47860 KB Output is correct
106 Correct 631 ms 47672 KB Output is correct
107 Correct 544 ms 47700 KB Output is correct
108 Correct 626 ms 47196 KB Output is correct
109 Correct 599 ms 46120 KB Output is correct
110 Correct 600 ms 46248 KB Output is correct
111 Correct 565 ms 46148 KB Output is correct
112 Correct 633 ms 46256 KB Output is correct
113 Correct 524 ms 47564 KB Output is correct
114 Correct 527 ms 47668 KB Output is correct
115 Correct 522 ms 47652 KB Output is correct
116 Correct 521 ms 47672 KB Output is correct
117 Correct 460 ms 47812 KB Output is correct
118 Correct 465 ms 47760 KB Output is correct
119 Correct 458 ms 47636 KB Output is correct
120 Correct 464 ms 47816 KB Output is correct
121 Correct 474 ms 47656 KB Output is correct
122 Correct 476 ms 47624 KB Output is correct
123 Correct 470 ms 47744 KB Output is correct
124 Correct 474 ms 47632 KB Output is correct
125 Correct 461 ms 47644 KB Output is correct
126 Correct 471 ms 47792 KB Output is correct