Submission #671362

# Submission time Handle Problem Language Result Execution time Memory
671362 2022-12-12T22:35:02 Z rainboy Hamburg Steak (JOI20_hamburg) C
100 / 100
1863 ms 167804 KB
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define LN	18	/* N_ = pow2(ceil(log2(N))) */
#define N_	(N * (LN + 1) + 1)
#define K	4
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

long long xxl[N], xxr[N], yyl[N], yyr[N], xx[K], yy[K]; int n, k;

long long *zz;

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (zz[ii[j]] == zz[i_])
				j++;
			else if (zz[ii[j]] < zz[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

long long *xl1, *xr1, *yl1, *yr1;

int ll_[N_], rr_[N_]; long long xxl_[N_], xxr_[N_], yyl_[N_], yyr_[N_];

int update(int t, int l, int r, int i, int i_) {
	static int _ = 1;
	int t_ = _++;

	ll_[t_] = ll_[t], rr_[t_] = rr_[t], xxl_[t_] = min(xxl_[t], xxr[i_]), xxr_[t_] = max(xxr_[t], xxl[i_]), yyl_[t_] = min(yyl_[t], yyr[i_]), yyr_[t_] = max(yyr_[t], yyl[i_]);
	if (r - l > 1) {
		int m = (l + r) / 2;

		if (i < m)
			ll_[t_] = update(ll_[t_], l, m, i, i_);
		else
			rr_[t_] = update(rr_[t_], m, r, i, i_);
	}
	return t_;
}

void query(int t, int l, int r, int ql, int qr) {
	int m;

	if (qr <= l || r <= ql || t == 0)
		return;
	if (ql <= l && r <= qr) {
		*xl1 = min(*xl1, xxl_[t]), *xr1 = max(*xr1, xxr_[t]), *yl1 = min(*yl1, yyl_[t]), *yr1 = max(*yr1, yyr_[t]);
		return;
	}
	m = (l + r) / 2;
	query(ll_[t], l, m, ql, qr), query(rr_[t], m, r, ql, qr);
}

int pierced(int i, int h_) {
	int h;

	for (h = 0; h < h_; h++)
		if (xxl[i] <= xx[h] && xx[h] <= xxr[i] && yyl[i] <= yy[h] && yy[h] <= yyr[i])
			return 1;
	return 0;
}

void solve(int h_) {
	int h, i;
	long long xl, xr, yl, yr;

	xl = INF, xr = -1, yl = INF, yr = -1;
	for (i = 0; i < n; i++)
		if (!pierced(i, h_))
			xl = min(xl, xxr[i]), xr = max(xr, xxl[i]), yl = min(yl, yyr[i]), yr = max(yr, yyl[i]);
	if (xl >= xr && yl >= yr) {
		xx[h_] = xl, yy[h_] = yl;
		for (h = h_ + 1; h < k; h++)
			xx[h] = 1, yy[h] = 1;
		for (h = 0; h < k; h++)
			printf("%lld %lld\n", xx[h], yy[h]);
		exit(0);
	} else if (h_ == k - 2) {
		xx[h_] = xl, yy[h_] = yl, solve(h_ + 1);
		xx[h_] = xl, yy[h_] = yr, solve(h_ + 1);
	} else if (h_ <= k - 3) {
		xx[h_] = xl, yy[h_] = yl, solve(h_ + 1);
		xx[h_] = xl, yy[h_] = yr, solve(h_ + 1);
		xx[h_] = xr, yy[h_] = yl, solve(h_ + 1);
		xx[h_] = xr, yy[h_] = yr, solve(h_ + 1);
	}
}

long long ll[N], rr[N], rr2[3][N]; int rr1[N];
int ii[3][N], nn[3], nn_[3], tt[3][N + 1]; long long xl, xr, yl, yr, z1, z2, z3, z4;

void query_(int g, long long l, long long r) {
	int lower, upper, l_, r_;

	lower = -1, upper = nn[g];
	while (upper - lower > 1) {
		int i = (lower + upper) / 2;

		if (ll[ii[g][i]] >= l)
			upper = i;
		else
			lower = i;
	}
	l_ = upper;
	lower = -1, upper = nn_[g];
	while (upper - lower > 1) {
		int i = (lower + upper) / 2;

		if (rr2[g][i] <= r)
			lower = i;
		else
			upper = i;
	}
	r_ = upper;
	query(tt[g][l_], 0, nn_[g], 0, r_);
}

void query1(long long *xx, int n, int g) {
	static int ii_[K + 2];
	int i;

	for (i = 0; i < n; i++)
		ii_[i] = i;
	zz = xx, sort(ii_, 0, n);
	for (i = 0; i + 1 < n; i++)
		query_(g, xx[ii_[i]] + 1, xx[ii_[i + 1]] - 1);
}

long long idx(long long x, long long y) {
	if (y == yl)
		return x - xl;
	if (x == xr)
		return z1 + y - yl;
	if (y == yr)
		return z2 + xr - x;
	if (x == xl)
		return z3 + yr - y;
	return -1;
}

void get_bounds(int h_) {
	static long long xx_[K + 2];
	int h;
	long long x_;

	if (h_ == 0) {
		*xl1 = xl, *xr1 = xr, *yl1 = yl, *yr1 = yr;
		return;
	}
	*xl1 = INF, *xr1 = -1, *yl1 = INF, *yr1 = -1;
	xx_[0] = -1;
	x_ = z4;
	for (h = 0; h < h_; h++)
		x_ = min(x_, xx_[h + 1] = idx(xx[h], yy[h]));
	xx_[h_ + 1] = x_ + z4;
	query1(xx_, h_ + 2, 0);
	xx_[0] = -1;
	for (h = 0; h < h_; h++)
		xx_[h + 1] = xx[h];
	xx_[h_ + 1] = INF;
	query1(xx_, h_ + 2, 1);
	xx_[0] = -1;
	for (h = 0; h < h_; h++)
		xx_[h + 1] = yy[h];
	xx_[h_ + 1] = INF;
	query1(xx_, h_ + 2, 2);
}

void solve_(int h_) {
	int h, i;
	long long xl_, yl_, xr_, yr_;

	if (h_ > 0 && idx(xx[h_ - 1], yy[h_ - 1]) == -1)
		return;
	xl1 = &xl_, xr1 = &xr_, yl1 = &yl_, yr1 = &yr_, get_bounds(h_);
	if (xl_ >= xr_ && yl_ >= yr_) {
		xx[h_] = xl_, yy[h_] = yl_;
		for (h = h_ + 1; h < k; h++)
			xx[h] = 1, yy[h] = 1;
		for (h = 0; h < k; h++)
			printf("%lld %lld\n", xx[h], yy[h]);
		exit(0);
	} else if (h_ == 0) {
		xx[h_] = xl_;
		for (i = 0; i < n; i++)
			if (yyl[i] != yl && yyl[i] != yr)
				yy[h_] = yyl[i], solve_(h_ + 1);
	} else if (h_ == 1) {
		xx[h_] = xl_, yy[h_] = yl, solve_(h_ + 1);
		xx[h_] = xl_, yy[h_] = yr, solve_(h_ + 1);
	} else if (h_ == 2)
		xx[h_] = xl_, yy[h_] = yy[1] == yl ? yr : yl, solve_(h_ + 1);
}

int main() {
	int g, i, i_;
	long long l, r;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%lld%lld%lld%lld", &xxl[i], &yyl[i], &xxr[i], &yyr[i]);
	solve(0);
	xl = INF, xr = -1, yl = INF, yr = -1;
	for (i = 0; i < n; i++)
		xl = min(xl, xxr[i]), xr = max(xr, xxl[i]), yl = min(yl, yyr[i]), yr = max(yr, yyl[i]);
	assert(k == 4 && xl < xr && yl < yr);
	z1 = xr - xl, z2 = z1 + yr - yl, z3 = z2 + xr - xl, z4 = z3 + yr - yl;
	for (i = 0; i < n; i++)
		xxl[i] = max(xxl[i], xl), xxr[i] = min(xxr[i], xr), yyl[i] = max(yyl[i], yl), yyr[i] = min(yyr[i], yr);
	for (i = 0; i < n; i++)
		if (xl < xxl[i] && xxr[i] < xr && yyl[i] == yl && yyr[i] == yr)
			ll[i] = xxl[i], rr[i] = xxr[i], ii[1][nn[1]++] = i;
		else if (yl < yyl[i] && yyr[i] < yr && xxl[i] == xl && xxr[i] == xr)
			ll[i] = yyl[i], rr[i] = yyr[i], ii[2][nn[2]++] = i;
		else {
			l = INF, r = -1;
			if (yyl[i] == yl)
				l = min(l, xxl[i] - xl), r = max(r, xxr[i] - xl);
			if (xxr[i] == xr)
				l = min(l, z1 + yyl[i] - yl), r = max(r, z1 + yyr[i] - yl);
			if (xxl[i] == xl && yyl[i] == yl)
				l += z4, r += z4;
			if (yyr[i] == yr)
				l = min(l, z2 + xr - xxr[i]), r = max(r, z2 + xr - xxl[i]);
			if (xxl[i] == xl)
				l = min(l, z3 + yr - yyr[i]), r = max(r, z3 + yr - yyl[i]);
			if (l >= z4)
				l -= z4, r -= z4;
			ll[i] = l, rr[i] = r, ii[0][nn[0]++] = i;
		}
	xxl_[0] = INF, xxr_[0] = -1, yyl_[0] = INF, yyr_[0] = -1;
	for (g = 0; g < 3; g++) {
		zz = rr, sort(ii[g], 0, nn[g]);
		for (i = 0; i < nn[g]; i++) {
			i_ = ii[g][i];
			rr1[i_] = nn_[g];
			if (i + 1 == nn[g] || rr[ii[g][i + 1]] != rr[i_])
				rr2[g][nn_[g]++] = rr[i_];
		}
		zz = ll, sort(ii[g], 0, nn[g]);
		for (i = nn[g] - 1; i >= 0; i--) {
			i_ = ii[g][i];
			tt[g][i] = update(tt[g][i + 1], 0, nn_[g], rr1[i_], i_);
		}
	}
	solve_(0);
	return 0;
}

Compilation message

hamburg.c: In function 'main':
hamburg.c:221:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  221 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
hamburg.c:223:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |   scanf("%lld%lld%lld%lld", &xxl[i], &yyl[i], &xxr[i], &yyr[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 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 340 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 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 5 ms 1364 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 5 ms 1364 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 6 ms 1364 KB Output is correct
21 Correct 3 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 1364 KB Output is correct
24 Correct 2 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 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 4 ms 1364 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 3 ms 1364 KB Output is correct
34 Correct 3 ms 1364 KB Output is correct
35 Correct 4 ms 1364 KB Output is correct
36 Correct 5 ms 1364 KB Output is correct
37 Correct 8 ms 1364 KB Output is correct
38 Correct 4 ms 1364 KB Output is correct
39 Correct 6 ms 1364 KB Output is correct
40 Correct 6 ms 1448 KB Output is correct
41 Correct 5 ms 1364 KB Output is correct
42 Correct 6 ms 1364 KB Output is correct
43 Correct 5 ms 1364 KB Output is correct
44 Correct 5 ms 1364 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 6 ms 1364 KB Output is correct
47 Correct 5 ms 1364 KB Output is correct
48 Correct 4 ms 1364 KB Output is correct
49 Correct 6 ms 1444 KB Output is correct
50 Correct 4 ms 1364 KB Output is correct
51 Correct 6 ms 1364 KB Output is correct
52 Correct 5 ms 1364 KB Output is correct
53 Correct 4 ms 1364 KB Output is correct
54 Correct 4 ms 1364 KB Output is correct
55 Correct 5 ms 1364 KB Output is correct
56 Correct 6 ms 1364 KB Output is correct
57 Correct 5 ms 1364 KB Output is correct
58 Correct 4 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 78 ms 6412 KB Output is correct
6 Correct 75 ms 6524 KB Output is correct
7 Correct 75 ms 6476 KB Output is correct
8 Correct 73 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 78 ms 6444 KB Output is correct
6 Correct 78 ms 6472 KB Output is correct
7 Correct 75 ms 6472 KB Output is correct
8 Correct 76 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 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 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 77 ms 6416 KB Output is correct
14 Correct 76 ms 6476 KB Output is correct
15 Correct 76 ms 6476 KB Output is correct
16 Correct 76 ms 6484 KB Output is correct
17 Correct 77 ms 6476 KB Output is correct
18 Correct 76 ms 6416 KB Output is correct
19 Correct 76 ms 6504 KB Output is correct
20 Correct 79 ms 6420 KB Output is correct
21 Correct 96 ms 6424 KB Output is correct
22 Correct 86 ms 6428 KB Output is correct
23 Correct 83 ms 6468 KB Output is correct
24 Correct 85 ms 6480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 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 340 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 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 5 ms 1364 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 5 ms 1364 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 6 ms 1364 KB Output is correct
21 Correct 3 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 1364 KB Output is correct
24 Correct 2 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 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 4 ms 1364 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 3 ms 1364 KB Output is correct
34 Correct 3 ms 1364 KB Output is correct
35 Correct 4 ms 1364 KB Output is correct
36 Correct 5 ms 1364 KB Output is correct
37 Correct 8 ms 1364 KB Output is correct
38 Correct 4 ms 1364 KB Output is correct
39 Correct 6 ms 1364 KB Output is correct
40 Correct 6 ms 1448 KB Output is correct
41 Correct 5 ms 1364 KB Output is correct
42 Correct 6 ms 1364 KB Output is correct
43 Correct 5 ms 1364 KB Output is correct
44 Correct 5 ms 1364 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 6 ms 1364 KB Output is correct
47 Correct 5 ms 1364 KB Output is correct
48 Correct 4 ms 1364 KB Output is correct
49 Correct 6 ms 1444 KB Output is correct
50 Correct 4 ms 1364 KB Output is correct
51 Correct 6 ms 1364 KB Output is correct
52 Correct 5 ms 1364 KB Output is correct
53 Correct 4 ms 1364 KB Output is correct
54 Correct 4 ms 1364 KB Output is correct
55 Correct 5 ms 1364 KB Output is correct
56 Correct 6 ms 1364 KB Output is correct
57 Correct 5 ms 1364 KB Output is correct
58 Correct 4 ms 1400 KB Output is correct
59 Correct 84 ms 6492 KB Output is correct
60 Correct 82 ms 6492 KB Output is correct
61 Correct 84 ms 6440 KB Output is correct
62 Correct 78 ms 6512 KB Output is correct
63 Correct 80 ms 6476 KB Output is correct
64 Correct 81 ms 6484 KB Output is correct
65 Correct 79 ms 6520 KB Output is correct
66 Correct 82 ms 6516 KB Output is correct
67 Correct 105 ms 6476 KB Output is correct
68 Correct 97 ms 6532 KB Output is correct
69 Correct 93 ms 6528 KB Output is correct
70 Correct 94 ms 6516 KB Output is correct
71 Correct 231 ms 6416 KB Output is correct
72 Correct 1016 ms 155872 KB Output is correct
73 Correct 195 ms 6476 KB Output is correct
74 Correct 144 ms 6440 KB Output is correct
75 Correct 557 ms 155896 KB Output is correct
76 Correct 162 ms 6572 KB Output is correct
77 Correct 180 ms 6528 KB Output is correct
78 Correct 811 ms 155840 KB Output is correct
79 Correct 207 ms 6532 KB Output is correct
80 Correct 171 ms 6424 KB Output is correct
81 Correct 572 ms 155724 KB Output is correct
82 Correct 171 ms 6428 KB Output is correct
83 Correct 116 ms 6492 KB Output is correct
84 Correct 112 ms 6624 KB Output is correct
85 Correct 155 ms 6708 KB Output is correct
86 Correct 135 ms 6472 KB Output is correct
87 Correct 124 ms 6476 KB Output is correct
88 Correct 133 ms 6536 KB Output is correct
89 Correct 1649 ms 156164 KB Output is correct
90 Correct 637 ms 163532 KB Output is correct
91 Correct 1177 ms 164376 KB Output is correct
92 Correct 1010 ms 164168 KB Output is correct
93 Correct 1302 ms 164444 KB Output is correct
94 Correct 985 ms 162352 KB Output is correct
95 Correct 1079 ms 163400 KB Output is correct
96 Correct 487 ms 164648 KB Output is correct
97 Correct 468 ms 164052 KB Output is correct
98 Correct 568 ms 164684 KB Output is correct
99 Correct 436 ms 165080 KB Output is correct
100 Correct 885 ms 163796 KB Output is correct
101 Correct 461 ms 162700 KB Output is correct
102 Correct 439 ms 163556 KB Output is correct
103 Correct 1552 ms 164552 KB Output is correct
104 Correct 416 ms 163256 KB Output is correct
105 Correct 1751 ms 164572 KB Output is correct
106 Correct 653 ms 164568 KB Output is correct
107 Correct 456 ms 163444 KB Output is correct
108 Correct 461 ms 163524 KB Output is correct
109 Correct 1629 ms 163640 KB Output is correct
110 Correct 980 ms 163524 KB Output is correct
111 Correct 1702 ms 164620 KB Output is correct
112 Correct 517 ms 164540 KB Output is correct
113 Correct 1103 ms 164784 KB Output is correct
114 Correct 585 ms 164912 KB Output is correct
115 Correct 1863 ms 164804 KB Output is correct
116 Correct 512 ms 164908 KB Output is correct
117 Correct 1328 ms 167632 KB Output is correct
118 Correct 1326 ms 167744 KB Output is correct
119 Correct 1317 ms 167800 KB Output is correct
120 Correct 1339 ms 167768 KB Output is correct
121 Correct 1328 ms 167760 KB Output is correct
122 Correct 1330 ms 167740 KB Output is correct
123 Correct 1398 ms 167692 KB Output is correct
124 Correct 1333 ms 167740 KB Output is correct
125 Correct 1319 ms 167740 KB Output is correct
126 Correct 1313 ms 167804 KB Output is correct